macvlan.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  4. * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <assert.h>
  19. #include <errno.h>
  20. #include <net/ethernet.h>
  21. #ifdef linux
  22. #include <netinet/ether.h>
  23. #endif
  24. #include "netifd.h"
  25. #include "device.h"
  26. #include "interface.h"
  27. #include "system.h"
  28. enum {
  29. MACVLAN_ATTR_IFNAME,
  30. MACVLAN_ATTR_MACADDR,
  31. MACVLAN_ATTR_MODE,
  32. __MACVLAN_ATTR_MAX
  33. };
  34. static const struct blobmsg_policy macvlan_attrs[__MACVLAN_ATTR_MAX] = {
  35. [MACVLAN_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_STRING },
  36. [MACVLAN_ATTR_MACADDR] = { "macaddr", BLOBMSG_TYPE_STRING },
  37. [MACVLAN_ATTR_MODE] = { "mode", BLOBMSG_TYPE_STRING },
  38. };
  39. static const struct uci_blob_param_list macvlan_attr_list = {
  40. .n_params = __MACVLAN_ATTR_MAX,
  41. .params = macvlan_attrs,
  42. .n_next = 1,
  43. .next = { &device_attr_list },
  44. };
  45. struct macvlan_device {
  46. struct device dev;
  47. struct device_user parent;
  48. device_state_cb set_state;
  49. struct blob_attr *config_data;
  50. struct blob_attr *ifname;
  51. struct macvlan_config config;
  52. };
  53. static void
  54. macvlan_base_cb(struct device_user *dev, enum device_event ev)
  55. {
  56. struct macvlan_device *mvdev = container_of(dev, struct macvlan_device, parent);
  57. switch (ev) {
  58. case DEV_EVENT_ADD:
  59. device_set_present(&mvdev->dev, true);
  60. break;
  61. case DEV_EVENT_REMOVE:
  62. device_set_present(&mvdev->dev, false);
  63. break;
  64. default:
  65. return;
  66. }
  67. }
  68. static int
  69. macvlan_set_down(struct macvlan_device *mvdev)
  70. {
  71. mvdev->set_state(&mvdev->dev, false);
  72. system_macvlan_del(&mvdev->dev);
  73. device_release(&mvdev->parent);
  74. return 0;
  75. }
  76. static int
  77. macvlan_set_up(struct macvlan_device *mvdev)
  78. {
  79. int ret;
  80. ret = device_claim(&mvdev->parent);
  81. if (ret < 0)
  82. return ret;
  83. ret = system_macvlan_add(&mvdev->dev, mvdev->parent.dev, &mvdev->config);
  84. if (ret < 0)
  85. goto release;
  86. ret = mvdev->set_state(&mvdev->dev, true);
  87. if (ret)
  88. goto delete;
  89. return 0;
  90. delete:
  91. system_macvlan_del(&mvdev->dev);
  92. release:
  93. device_release(&mvdev->parent);
  94. return ret;
  95. }
  96. static int
  97. macvlan_set_state(struct device *dev, bool up)
  98. {
  99. struct macvlan_device *mvdev;
  100. D(SYSTEM, "macvlan_set_state(%s, %u)\n", dev->ifname, up);
  101. mvdev = container_of(dev, struct macvlan_device, dev);
  102. if (up)
  103. return macvlan_set_up(mvdev);
  104. else
  105. return macvlan_set_down(mvdev);
  106. }
  107. static void
  108. macvlan_free(struct device *dev)
  109. {
  110. struct macvlan_device *mvdev;
  111. mvdev = container_of(dev, struct macvlan_device, dev);
  112. device_remove_user(&mvdev->parent);
  113. free(mvdev->config_data);
  114. free(mvdev);
  115. }
  116. static void
  117. macvlan_dump_info(struct device *dev, struct blob_buf *b)
  118. {
  119. struct macvlan_device *mvdev;
  120. mvdev = container_of(dev, struct macvlan_device, dev);
  121. blobmsg_add_string(b, "parent", mvdev->parent.dev->ifname);
  122. system_if_dump_info(dev, b);
  123. }
  124. static void
  125. macvlan_config_init(struct device *dev)
  126. {
  127. struct macvlan_device *mvdev;
  128. struct device *basedev = NULL;
  129. mvdev = container_of(dev, struct macvlan_device, dev);
  130. if (mvdev->ifname)
  131. basedev = device_get(blobmsg_data(mvdev->ifname), true);
  132. device_add_user(&mvdev->parent, basedev);
  133. }
  134. static void
  135. macvlan_apply_settings(struct macvlan_device *mvdev, struct blob_attr **tb)
  136. {
  137. struct macvlan_config *cfg = &mvdev->config;
  138. struct blob_attr *cur;
  139. struct ether_addr *ea;
  140. cfg->flags = 0;
  141. cfg->mode = NULL;
  142. if ((cur = tb[MACVLAN_ATTR_MACADDR])) {
  143. ea = ether_aton(blobmsg_data(cur));
  144. if (ea) {
  145. memcpy(cfg->macaddr, ea, 6);
  146. cfg->flags |= MACVLAN_OPT_MACADDR;
  147. }
  148. }
  149. if ((cur = tb[MACVLAN_ATTR_MODE]))
  150. cfg->mode = blobmsg_data(cur);
  151. }
  152. static enum dev_change_type
  153. macvlan_reload(struct device *dev, struct blob_attr *attr)
  154. {
  155. struct blob_attr *tb_dev[__DEV_ATTR_MAX];
  156. struct blob_attr *tb_mv[__MACVLAN_ATTR_MAX];
  157. enum dev_change_type ret = DEV_CONFIG_APPLIED;
  158. struct macvlan_device *mvdev;
  159. mvdev = container_of(dev, struct macvlan_device, dev);
  160. attr = blob_memdup(attr);
  161. blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
  162. blob_data(attr), blob_len(attr));
  163. blobmsg_parse(macvlan_attrs, __MACVLAN_ATTR_MAX, tb_mv,
  164. blob_data(attr), blob_len(attr));
  165. device_init_settings(dev, tb_dev);
  166. macvlan_apply_settings(mvdev, tb_mv);
  167. mvdev->ifname = tb_mv[MACVLAN_ATTR_IFNAME];
  168. if (mvdev->config_data) {
  169. struct blob_attr *otb_dev[__DEV_ATTR_MAX];
  170. struct blob_attr *otb_mv[__MACVLAN_ATTR_MAX];
  171. blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
  172. blob_data(mvdev->config_data), blob_len(mvdev->config_data));
  173. if (uci_blob_diff(tb_dev, otb_dev, &device_attr_list, NULL))
  174. ret = DEV_CONFIG_RESTART;
  175. blobmsg_parse(macvlan_attrs, __MACVLAN_ATTR_MAX, otb_mv,
  176. blob_data(mvdev->config_data), blob_len(mvdev->config_data));
  177. if (uci_blob_diff(tb_mv, otb_mv, &macvlan_attr_list, NULL))
  178. ret = DEV_CONFIG_RESTART;
  179. macvlan_config_init(dev);
  180. }
  181. free(mvdev->config_data);
  182. mvdev->config_data = attr;
  183. return ret;
  184. }
  185. static struct device *
  186. macvlan_create(const char *name, struct device_type *devtype,
  187. struct blob_attr *attr)
  188. {
  189. struct macvlan_device *mvdev;
  190. struct device *dev = NULL;
  191. mvdev = calloc(1, sizeof(*mvdev));
  192. if (!mvdev)
  193. return NULL;
  194. dev = &mvdev->dev;
  195. if (device_init(dev, devtype, name) < 0) {
  196. device_cleanup(dev);
  197. free(mvdev);
  198. return NULL;
  199. }
  200. dev->config_pending = true;
  201. mvdev->set_state = dev->set_state;
  202. dev->set_state = macvlan_set_state;
  203. dev->hotplug_ops = NULL;
  204. mvdev->parent.cb = macvlan_base_cb;
  205. macvlan_reload(dev, attr);
  206. return dev;
  207. }
  208. static struct device_type macvlan_device_type = {
  209. .name = "macvlan",
  210. .config_params = &macvlan_attr_list,
  211. .create = macvlan_create,
  212. .config_init = macvlan_config_init,
  213. .reload = macvlan_reload,
  214. .free = macvlan_free,
  215. .dump_info = macvlan_dump_info,
  216. };
  217. static void __init macvlan_device_type_init(void)
  218. {
  219. device_type_add(&macvlan_device_type);
  220. }