tunnel.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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->ifname, 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->ifname);
  35. return ret;
  36. }
  37. static struct device *
  38. tunnel_create(const char *name, struct blob_attr *attr)
  39. {
  40. struct tunnel *tun;
  41. struct device *dev;
  42. tun = calloc(1, sizeof(*tun));
  43. dev = &tun->dev;
  44. device_init(dev, &tunnel_device_type, name);
  45. tun->set_state = dev->set_state;
  46. dev->set_state = tunnel_set_state;
  47. device_set_present(dev, true);
  48. return dev;
  49. }
  50. static void
  51. tunnel_free(struct device *dev)
  52. {
  53. struct tunnel *tun = container_of(dev, struct tunnel, dev);
  54. free(tun);
  55. }
  56. const struct device_type tunnel_device_type = {
  57. .name = "IP tunnel",
  58. .config_params = &tunnel_attr_list,
  59. .create = tunnel_create,
  60. .free = tunnel_free,
  61. };