proto-static.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <arpa/inet.h>
  18. #include <netinet/in.h>
  19. #include "netifd.h"
  20. #include "interface.h"
  21. #include "interface-ip.h"
  22. #include "proto.h"
  23. #include "system.h"
  24. struct static_proto_state {
  25. struct interface_proto_state proto;
  26. struct blob_attr *config;
  27. };
  28. static bool
  29. static_proto_setup(struct static_proto_state *state)
  30. {
  31. return proto_apply_static_ip_settings(state->proto.iface, state->config) == 0;
  32. }
  33. static int
  34. static_handler(struct interface_proto_state *proto,
  35. enum interface_proto_cmd cmd, bool force)
  36. {
  37. struct static_proto_state *state;
  38. int ret = 0;
  39. state = container_of(proto, struct static_proto_state, proto);
  40. switch (cmd) {
  41. case PROTO_CMD_SETUP:
  42. if (!static_proto_setup(state))
  43. return -1;
  44. break;
  45. case PROTO_CMD_TEARDOWN:
  46. break;
  47. }
  48. return ret;
  49. }
  50. static void
  51. static_free(struct interface_proto_state *proto)
  52. {
  53. struct static_proto_state *state;
  54. state = container_of(proto, struct static_proto_state, proto);
  55. free(state->config);
  56. free(state);
  57. }
  58. static struct interface_proto_state *
  59. static_attach(const struct proto_handler *h, struct interface *iface,
  60. struct blob_attr *attr)
  61. {
  62. struct static_proto_state *state;
  63. state = calloc(1, sizeof(*state));
  64. if (!state)
  65. return NULL;
  66. state->config = malloc(blob_pad_len(attr));
  67. if (!state->config)
  68. goto error;
  69. memcpy(state->config, attr, blob_pad_len(attr));
  70. state->proto.free = static_free;
  71. state->proto.cb = static_handler;
  72. return &state->proto;
  73. error:
  74. free(state);
  75. return NULL;
  76. }
  77. static struct proto_handler static_proto = {
  78. .name = "static",
  79. .flags = PROTO_FLAG_IMMEDIATE,
  80. .config_params = &proto_ip_attr,
  81. .attach = static_attach,
  82. };
  83. static void __init
  84. static_proto_init(void)
  85. {
  86. add_proto_handler(&static_proto);
  87. }