vlan.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.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 <stdlib.h>
  16. #include <stdio.h>
  17. #include <ctype.h>
  18. #include "netifd.h"
  19. #include "system.h"
  20. static struct blob_buf b;
  21. struct vlan_device {
  22. struct device dev;
  23. struct device_user dep;
  24. device_state_cb set_state;
  25. int id;
  26. };
  27. static void free_vlan_if(struct device *iface)
  28. {
  29. struct vlan_device *vldev;
  30. vldev = container_of(iface, struct vlan_device, dev);
  31. device_remove_user(&vldev->dep);
  32. device_cleanup(&vldev->dev);
  33. free(vldev);
  34. }
  35. static int
  36. __vlan_hotplug_op(struct device *dev, struct device *member, struct blob_attr *vlan, bool add)
  37. {
  38. struct vlan_device *vldev = container_of(dev, struct vlan_device, dev);
  39. void *a;
  40. dev = vldev->dep.dev;
  41. if (!dev || !dev->hotplug_ops)
  42. return UBUS_STATUS_NOT_SUPPORTED;
  43. blob_buf_init(&b, 0);
  44. a = blobmsg_open_array(&b, "vlans");
  45. blobmsg_printf(&b, NULL, "%d", vldev->id);
  46. blobmsg_close_array(&b, a);
  47. if (add)
  48. return dev->hotplug_ops->add(dev, member, blobmsg_data(b.head));
  49. else
  50. return dev->hotplug_ops->del(dev, member, blobmsg_data(b.head));
  51. }
  52. static int
  53. vlan_hotplug_add(struct device *dev, struct device *member, struct blob_attr *vlan)
  54. {
  55. return __vlan_hotplug_op(dev, member, vlan, true);
  56. }
  57. static int
  58. vlan_hotplug_del(struct device *dev, struct device *member, struct blob_attr *vlan)
  59. {
  60. return __vlan_hotplug_op(dev, member, vlan, false);
  61. }
  62. static int
  63. vlan_hotplug_prepare(struct device *dev, struct device **bridge_dev)
  64. {
  65. struct vlan_device *vldev = container_of(dev, struct vlan_device, dev);
  66. dev = vldev->dep.dev;
  67. if (!dev || !dev->hotplug_ops)
  68. return UBUS_STATUS_NOT_SUPPORTED;
  69. return dev->hotplug_ops->prepare(dev, bridge_dev);
  70. }
  71. static void vlan_hotplug_check(struct vlan_device *vldev, struct device *dev)
  72. {
  73. static const struct device_hotplug_ops hotplug_ops = {
  74. .prepare = vlan_hotplug_prepare,
  75. .add = vlan_hotplug_add,
  76. .del = vlan_hotplug_del
  77. };
  78. if (!dev || !dev->hotplug_ops || avl_is_empty(&dev->vlans.avl)) {
  79. vldev->dev.hotplug_ops = NULL;
  80. return;
  81. }
  82. vldev->dev.hotplug_ops = &hotplug_ops;
  83. }
  84. static int vlan_set_device_state(struct device *dev, bool up)
  85. {
  86. struct vlan_device *vldev;
  87. int ret = 0;
  88. vldev = container_of(dev, struct vlan_device, dev);
  89. if (!up) {
  90. vldev->set_state(dev, false);
  91. system_vlan_del(dev);
  92. device_release(&vldev->dep);
  93. return 0;
  94. }
  95. ret = device_claim(&vldev->dep);
  96. if (ret < 0)
  97. return ret;
  98. system_vlan_add(vldev->dep.dev, vldev->id);
  99. ret = vldev->set_state(dev, true);
  100. if (ret)
  101. device_release(&vldev->dep);
  102. return ret;
  103. }
  104. static void vlan_dev_cb(struct device_user *dep, enum device_event ev)
  105. {
  106. char name[IFNAMSIZ];
  107. struct vlan_device *vldev;
  108. vldev = container_of(dep, struct vlan_device, dep);
  109. switch(ev) {
  110. case DEV_EVENT_ADD:
  111. device_set_present(&vldev->dev, true);
  112. break;
  113. case DEV_EVENT_REMOVE:
  114. device_set_present(&vldev->dev, false);
  115. break;
  116. case DEV_EVENT_UPDATE_IFNAME:
  117. vlan_hotplug_check(vldev, dep->dev);
  118. vldev->dev.hidden = dep->dev->hidden;
  119. if (snprintf(name, sizeof(name), "%s.%d", dep->dev->ifname,
  120. vldev->id) >= (int)sizeof(name) - 1 ||
  121. device_set_ifname(&vldev->dev, name))
  122. free_vlan_if(&vldev->dev);
  123. break;
  124. case DEV_EVENT_TOPO_CHANGE:
  125. /* Propagate topo changes */
  126. device_broadcast_event(&vldev->dev, DEV_EVENT_TOPO_CHANGE);
  127. break;
  128. default:
  129. break;
  130. }
  131. }
  132. static void
  133. vlan_config_init(struct device *dev)
  134. {
  135. struct vlan_device *vldev;
  136. vldev = container_of(dev, struct vlan_device, dev);
  137. vlan_hotplug_check(vldev, vldev->dep.dev);
  138. }
  139. static struct device *get_vlan_device(struct device *dev, char *id_str, bool create)
  140. {
  141. static struct device_type vlan_type = {
  142. .name = "VLAN",
  143. .config_params = &device_attr_list,
  144. .config_init = vlan_config_init,
  145. .free = free_vlan_if,
  146. };
  147. struct vlan_device *vldev;
  148. struct device_user *dep;
  149. char name[IFNAMSIZ];
  150. char *err = NULL;
  151. int id, *alias_id;
  152. id = strtoul(id_str, &err, 10);
  153. if (err && *err) {
  154. alias_id = kvlist_get(&dev->vlan_aliases, id_str);
  155. if (!alias_id)
  156. return NULL;
  157. id = *alias_id;
  158. }
  159. /* look for an existing interface before creating a new one */
  160. list_for_each_entry(dep, &dev->users.list, list.list) {
  161. if (dep->cb != vlan_dev_cb)
  162. continue;
  163. vldev = container_of(dep, struct vlan_device, dep);
  164. if (vldev->id != id)
  165. continue;
  166. return &vldev->dev;
  167. }
  168. if (!create)
  169. return NULL;
  170. if (snprintf(name, sizeof(name), "%s.%d", dev->ifname, id) >= (int)sizeof(name) - 1)
  171. return NULL;
  172. D(DEVICE, "Create vlan device '%s'\n", name);
  173. vldev = calloc(1, sizeof(*vldev));
  174. if (!vldev)
  175. return NULL;
  176. vldev->id = id;
  177. vldev->dev.hidden = dev->hidden;
  178. strcpy(vldev->dev.ifname, name);
  179. if (device_init(&vldev->dev, &vlan_type, NULL) < 0)
  180. goto error;
  181. vldev->dev.default_config = true;
  182. vldev->dev.config_pending = true;
  183. vldev->set_state = vldev->dev.set_state;
  184. vldev->dev.set_state = vlan_set_device_state;
  185. vldev->dep.cb = vlan_dev_cb;
  186. vlan_hotplug_check(vldev, vldev->dep.dev);
  187. device_add_user(&vldev->dep, dev);
  188. return &vldev->dev;
  189. error:
  190. device_cleanup(&vldev->dev);
  191. free(vldev);
  192. return NULL;
  193. }
  194. static char *split_vlan(char *s)
  195. {
  196. s = strchr(s, '.');
  197. if (!s)
  198. return NULL;
  199. *s = 0;
  200. s++;
  201. return s;
  202. }
  203. struct device *get_vlan_device_chain(const char *ifname, int create)
  204. {
  205. struct device *dev = NULL, *vldev;
  206. char *buf, *s, *next;
  207. buf = strdup(ifname);
  208. if (!buf)
  209. return NULL;
  210. s = split_vlan(buf);
  211. dev = __device_get(buf, create, false);
  212. if (!dev || !s)
  213. goto out;
  214. /* for the first split, we need to check if we're using an alias or
  215. * if the . separator isn't being used as a vlan separator (e.g. for
  216. * AP WDS VLANs */
  217. if (!isdigit(s[0])) {
  218. next = split_vlan(s);
  219. vldev = get_vlan_device(dev, s, create);
  220. if (!vldev) {
  221. s[-1] = '.';
  222. dev = __device_get(buf, create, false);
  223. if (!dev)
  224. goto out;
  225. if (next)
  226. next[-1] = '.';
  227. } else {
  228. dev = vldev;
  229. s = next;
  230. }
  231. }
  232. while (s) {
  233. next = split_vlan(s);
  234. dev = get_vlan_device(dev, s, create);
  235. if (!dev)
  236. break;
  237. s = next;
  238. }
  239. out:
  240. free(buf);
  241. return dev;
  242. }