vlandev.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 "netifd.h"
  16. #include "device.h"
  17. #include "interface.h"
  18. #include "system.h"
  19. enum {
  20. VLANDEV_ATTR_IFNAME,
  21. VLANDEV_ATTR_VID,
  22. __VLANDEV_ATTR_MAX
  23. };
  24. static const struct blobmsg_policy vlandev_attrs[__VLANDEV_ATTR_MAX] = {
  25. [VLANDEV_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_STRING },
  26. [VLANDEV_ATTR_VID] = { "vid", BLOBMSG_TYPE_INT32 },
  27. };
  28. static const struct uci_blob_param_list vlandev_attr_list = {
  29. .n_params = __VLANDEV_ATTR_MAX,
  30. .params = vlandev_attrs,
  31. .n_next = 1,
  32. .next = { &device_attr_list },
  33. };
  34. static struct device_type vlan8021q_device_type;
  35. struct vlandev_device {
  36. struct device dev;
  37. struct device_user parent;
  38. device_state_cb set_state;
  39. struct blob_attr *config_data;
  40. struct blob_attr *ifname;
  41. struct vlandev_config config;
  42. };
  43. static void
  44. vlandev_base_cb(struct device_user *dev, enum device_event ev)
  45. {
  46. struct vlandev_device *mvdev = container_of(dev, struct vlandev_device, parent);
  47. switch (ev) {
  48. case DEV_EVENT_ADD:
  49. device_set_present(&mvdev->dev, true);
  50. break;
  51. case DEV_EVENT_REMOVE:
  52. device_set_present(&mvdev->dev, false);
  53. break;
  54. default:
  55. return;
  56. }
  57. }
  58. static int
  59. vlandev_set_down(struct vlandev_device *mvdev)
  60. {
  61. mvdev->set_state(&mvdev->dev, false);
  62. system_vlandev_del(&mvdev->dev);
  63. device_release(&mvdev->parent);
  64. return 0;
  65. }
  66. static int
  67. vlandev_set_up(struct vlandev_device *mvdev)
  68. {
  69. int ret;
  70. ret = device_claim(&mvdev->parent);
  71. if (ret < 0)
  72. return ret;
  73. ret = system_vlandev_add(&mvdev->dev, mvdev->parent.dev, &mvdev->config);
  74. if (ret < 0)
  75. goto release;
  76. ret = mvdev->set_state(&mvdev->dev, true);
  77. if (ret)
  78. goto delete;
  79. return 0;
  80. delete:
  81. system_vlandev_del(&mvdev->dev);
  82. release:
  83. device_release(&mvdev->parent);
  84. return ret;
  85. }
  86. static int
  87. vlandev_set_state(struct device *dev, bool up)
  88. {
  89. struct vlandev_device *mvdev;
  90. D(SYSTEM, "vlandev_set_state(%s, %u)\n", dev->ifname, up);
  91. mvdev = container_of(dev, struct vlandev_device, dev);
  92. if (up)
  93. return vlandev_set_up(mvdev);
  94. else
  95. return vlandev_set_down(mvdev);
  96. }
  97. static void
  98. vlandev_free(struct device *dev)
  99. {
  100. struct vlandev_device *mvdev;
  101. mvdev = container_of(dev, struct vlandev_device, dev);
  102. device_remove_user(&mvdev->parent);
  103. free(mvdev->config_data);
  104. free(mvdev);
  105. }
  106. static void
  107. vlandev_dump_info(struct device *dev, struct blob_buf *b)
  108. {
  109. struct vlandev_device *mvdev;
  110. mvdev = container_of(dev, struct vlandev_device, dev);
  111. blobmsg_add_string(b, "parent", mvdev->parent.dev->ifname);
  112. system_if_dump_info(dev, b);
  113. }
  114. static void
  115. vlandev_config_init(struct device *dev)
  116. {
  117. struct vlandev_device *mvdev;
  118. struct device *basedev = NULL;
  119. mvdev = container_of(dev, struct vlandev_device, dev);
  120. if (mvdev->ifname)
  121. basedev = device_get(blobmsg_data(mvdev->ifname), true);
  122. device_add_user(&mvdev->parent, basedev);
  123. }
  124. static void
  125. vlandev_apply_settings(struct vlandev_device *mvdev, struct blob_attr **tb)
  126. {
  127. struct vlandev_config *cfg = &mvdev->config;
  128. struct blob_attr *cur;
  129. cfg->proto = (mvdev->dev.type == &vlan8021q_device_type) ?
  130. VLAN_PROTO_8021Q : VLAN_PROTO_8021AD;
  131. cfg->vid = 1;
  132. if ((cur = tb[VLANDEV_ATTR_VID]))
  133. cfg->vid = (uint16_t) blobmsg_get_u32(cur);
  134. }
  135. static enum dev_change_type
  136. vlandev_reload(struct device *dev, struct blob_attr *attr)
  137. {
  138. struct blob_attr *tb_dev[__DEV_ATTR_MAX];
  139. struct blob_attr *tb_mv[__VLANDEV_ATTR_MAX];
  140. enum dev_change_type ret = DEV_CONFIG_APPLIED;
  141. struct vlandev_device *mvdev;
  142. mvdev = container_of(dev, struct vlandev_device, dev);
  143. attr = blob_memdup(attr);
  144. blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
  145. blob_data(attr), blob_len(attr));
  146. blobmsg_parse(vlandev_attrs, __VLANDEV_ATTR_MAX, tb_mv,
  147. blob_data(attr), blob_len(attr));
  148. device_init_settings(dev, tb_dev);
  149. vlandev_apply_settings(mvdev, tb_mv);
  150. mvdev->ifname = tb_mv[VLANDEV_ATTR_IFNAME];
  151. if (mvdev->config_data) {
  152. struct blob_attr *otb_dev[__DEV_ATTR_MAX];
  153. struct blob_attr *otb_mv[__VLANDEV_ATTR_MAX];
  154. blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
  155. blob_data(mvdev->config_data), blob_len(mvdev->config_data));
  156. if (uci_blob_diff(tb_dev, otb_dev, &device_attr_list, NULL))
  157. ret = DEV_CONFIG_RESTART;
  158. blobmsg_parse(vlandev_attrs, __VLANDEV_ATTR_MAX, otb_mv,
  159. blob_data(mvdev->config_data), blob_len(mvdev->config_data));
  160. if (uci_blob_diff(tb_mv, otb_mv, &vlandev_attr_list, NULL))
  161. ret = DEV_CONFIG_RESTART;
  162. vlandev_config_init(dev);
  163. }
  164. free(mvdev->config_data);
  165. mvdev->config_data = attr;
  166. return ret;
  167. }
  168. static struct device *
  169. vlandev_create(const char *name, struct device_type *devtype,
  170. struct blob_attr *attr)
  171. {
  172. struct vlandev_device *mvdev;
  173. struct device *dev = NULL;
  174. mvdev = calloc(1, sizeof(*mvdev));
  175. if (!mvdev)
  176. return NULL;
  177. dev = &mvdev->dev;
  178. if (device_init(dev, devtype, name) < 0) {
  179. device_cleanup(dev);
  180. free(mvdev);
  181. return NULL;
  182. }
  183. dev->config_pending = true;
  184. mvdev->set_state = dev->set_state;
  185. dev->set_state = vlandev_set_state;
  186. dev->hotplug_ops = NULL;
  187. mvdev->parent.cb = vlandev_base_cb;
  188. vlandev_reload(dev, attr);
  189. return dev;
  190. }
  191. static struct device_type vlan8021ad_device_type = {
  192. .name = "8021ad",
  193. .config_params = &vlandev_attr_list,
  194. .create = vlandev_create,
  195. .config_init = vlandev_config_init,
  196. .reload = vlandev_reload,
  197. .free = vlandev_free,
  198. .dump_info = vlandev_dump_info,
  199. };
  200. static struct device_type vlan8021q_device_type = {
  201. .name = "8021q",
  202. .config_params = &vlandev_attr_list,
  203. .create = vlandev_create,
  204. .config_init = vlandev_config_init,
  205. .reload = vlandev_reload,
  206. .free = vlandev_free,
  207. .dump_info = vlandev_dump_info,
  208. };
  209. static void __init vlandev_device_type_init(void)
  210. {
  211. device_type_add(&vlan8021ad_device_type);
  212. device_type_add(&vlan8021q_device_type);
  213. }