proto-static.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. struct interface *iface = state->proto.iface;
  32. struct device *dev = iface->main_dev.dev;
  33. interface_set_l3_dev(iface, dev);
  34. return proto_apply_static_ip_settings(state->proto.iface, state->config) == 0;
  35. }
  36. static int
  37. static_handler(struct interface_proto_state *proto,
  38. enum interface_proto_cmd cmd, bool force)
  39. {
  40. struct static_proto_state *state;
  41. int ret = 0;
  42. state = container_of(proto, struct static_proto_state, proto);
  43. switch (cmd) {
  44. case PROTO_CMD_SETUP:
  45. if (!static_proto_setup(state))
  46. return -1;
  47. break;
  48. case PROTO_CMD_TEARDOWN:
  49. case PROTO_CMD_RENEW:
  50. break;
  51. }
  52. return ret;
  53. }
  54. static void
  55. static_free(struct interface_proto_state *proto)
  56. {
  57. struct static_proto_state *state;
  58. state = container_of(proto, struct static_proto_state, proto);
  59. free(state->config);
  60. free(state);
  61. }
  62. static struct interface_proto_state *
  63. static_attach(const struct proto_handler *h, struct interface *iface,
  64. struct blob_attr *attr)
  65. {
  66. struct static_proto_state *state;
  67. state = calloc(1, sizeof(*state));
  68. if (!state)
  69. return NULL;
  70. state->config = malloc(blob_pad_len(attr));
  71. if (!state->config)
  72. goto error;
  73. memcpy(state->config, attr, blob_pad_len(attr));
  74. state->proto.free = static_free;
  75. state->proto.cb = static_handler;
  76. return &state->proto;
  77. error:
  78. free(state);
  79. return NULL;
  80. }
  81. static struct proto_handler static_proto = {
  82. .name = "static",
  83. .flags = PROTO_FLAG_IMMEDIATE |
  84. PROTO_FLAG_FORCE_LINK_DEFAULT,
  85. .config_params = &proto_ip_attr,
  86. .attach = static_attach,
  87. };
  88. static void __init
  89. static_proto_init(void)
  90. {
  91. add_proto_handler(&static_proto);
  92. }