tunnel.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "netifd.h"
  15. #include "device.h"
  16. #include "config.h"
  17. #include "system.h"
  18. struct tunnel {
  19. struct device dev;
  20. device_state_cb set_state;
  21. };
  22. static int
  23. tunnel_set_state(struct device *dev, bool up)
  24. {
  25. struct tunnel *tun = container_of(dev, struct tunnel, dev);
  26. int ret;
  27. if (up) {
  28. ret = system_add_ip_tunnel(dev, dev->config);
  29. if (ret != 0)
  30. return ret;
  31. }
  32. ret = tun->set_state(dev, up);
  33. if (ret || !up)
  34. system_del_ip_tunnel(dev);
  35. return ret;
  36. }
  37. static enum dev_change_type
  38. tunnel_reload(struct device *dev, struct blob_attr *attr)
  39. {
  40. struct blob_attr *tb_dev[__DEV_ATTR_MAX];
  41. const struct uci_blob_param_list *cfg = dev->type->config_params;
  42. if (uci_blob_check_equal(dev->config, attr, cfg))
  43. return DEV_CONFIG_NO_CHANGE;
  44. memset(tb_dev, 0, sizeof(tb_dev));
  45. if (attr)
  46. blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
  47. blob_data(attr), blob_len(attr));
  48. device_init_settings(dev, tb_dev);
  49. return DEV_CONFIG_RESTART;
  50. }
  51. static struct device *
  52. tunnel_create(const char *name, struct device_type *devtype,
  53. struct blob_attr *attr)
  54. {
  55. struct tunnel *tun;
  56. struct device *dev;
  57. tun = calloc(1, sizeof(*tun));
  58. if (!tun)
  59. return NULL;
  60. dev = &tun->dev;
  61. if (device_init(dev, devtype, name) < 0) {
  62. device_cleanup(dev);
  63. free(tun);
  64. return NULL;
  65. }
  66. tun->set_state = dev->set_state;
  67. dev->set_state = tunnel_set_state;
  68. device_apply_config(dev, devtype, attr);
  69. device_set_present(dev, true);
  70. return dev;
  71. }
  72. static void
  73. tunnel_free(struct device *dev)
  74. {
  75. struct tunnel *tun = container_of(dev, struct tunnel, dev);
  76. free(tun);
  77. }
  78. struct device_type tunnel_device_type = {
  79. .name = "tunnel",
  80. .config_params = &tunnel_attr_list,
  81. .reload = tunnel_reload,
  82. .create = tunnel_create,
  83. .free = tunnel_free,
  84. };
  85. static void __init tunnel_device_type_init(void)
  86. {
  87. device_type_add(&tunnel_device_type);
  88. }