vlan.c 6.5 KB

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