protocol_subnet.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. protocol_subnet.c -- handle the meta-protocol, subnets
  3. Copyright (C) 1999-2005 Ivo Timmermans,
  4. 2000-2009 Guus Sliepen <guus@tinc-vpn.org>
  5. 2009 Michael Tokarev <mjt@tls.msk.ru>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License along
  15. with this program; if not, write to the Free Software Foundation, Inc.,
  16. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "system.h"
  19. #include "conf.h"
  20. #include "connection.h"
  21. #include "logger.h"
  22. #include "net.h"
  23. #include "netutl.h"
  24. #include "node.h"
  25. #include "protocol.h"
  26. #include "subnet.h"
  27. #include "utils.h"
  28. #include "xalloc.h"
  29. bool send_add_subnet(connection_t *c, const subnet_t *subnet) {
  30. char netstr[MAXNETSTR];
  31. if(!net2str(netstr, sizeof(netstr), subnet)) {
  32. return false;
  33. }
  34. return send_request(c, "%d %x %s %s", ADD_SUBNET, rand(), subnet->owner->name, netstr);
  35. }
  36. bool add_subnet_h(connection_t *c) {
  37. char subnetstr[MAX_STRING_SIZE];
  38. char name[MAX_STRING_SIZE];
  39. node_t *owner;
  40. subnet_t s = {0}, *new, *old;
  41. if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
  42. logger(LOG_ERR, "Got bad %s from %s (%s)", "ADD_SUBNET", c->name,
  43. c->hostname);
  44. return false;
  45. }
  46. /* Check if owner name is valid */
  47. if(!check_id(name)) {
  48. logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_SUBNET", c->name,
  49. c->hostname, "invalid name");
  50. return false;
  51. }
  52. /* Check if subnet string is valid */
  53. if(!str2net(&s, subnetstr)) {
  54. logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ADD_SUBNET", c->name,
  55. c->hostname, "invalid subnet string");
  56. return false;
  57. }
  58. if(seen_request(c->buffer)) {
  59. return true;
  60. }
  61. /* Check if the owner of the new subnet is in the connection list */
  62. owner = lookup_node(name);
  63. if(tunnelserver && owner != myself && owner != c->node) {
  64. /* in case of tunnelserver, ignore indirect subnet registrations */
  65. ifdebug(PROTOCOL) logger(LOG_WARNING, "Ignoring indirect %s from %s (%s) for %s",
  66. "ADD_SUBNET", c->name, c->hostname, subnetstr);
  67. return true;
  68. }
  69. if(!owner) {
  70. owner = new_node();
  71. owner->name = xstrdup(name);
  72. node_add(owner);
  73. }
  74. /* Check if we already know this subnet */
  75. if(lookup_subnet(owner, &s)) {
  76. return true;
  77. }
  78. /* If we don't know this subnet, but we are the owner, retaliate with a DEL_SUBNET */
  79. if(owner == myself) {
  80. ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for ourself",
  81. "ADD_SUBNET", c->name, c->hostname);
  82. s.owner = myself;
  83. send_del_subnet(c, &s);
  84. return true;
  85. }
  86. /* In tunnel server mode, we should already know all allowed subnets */
  87. if(tunnelserver) {
  88. logger(LOG_WARNING, "Ignoring unauthorized %s from %s (%s): %s",
  89. "ADD_SUBNET", c->name, c->hostname, subnetstr);
  90. return true;
  91. }
  92. /* Ignore if strictsubnets is true, but forward it to others */
  93. if(strictsubnets) {
  94. logger(LOG_WARNING, "Ignoring unauthorized %s from %s (%s): %s",
  95. "ADD_SUBNET", c->name, c->hostname, subnetstr);
  96. forward_request(c);
  97. return true;
  98. }
  99. /* If everything is correct, add the subnet to the list of the owner */
  100. *(new = new_subnet()) = s;
  101. subnet_add(owner, new);
  102. if(owner->status.reachable) {
  103. subnet_update(owner, new, true);
  104. }
  105. /* Tell the rest */
  106. forward_request(c);
  107. /* Fast handoff of roaming MAC addresses */
  108. if(s.type == SUBNET_MAC && owner != myself && (old = lookup_subnet(myself, &s)) && old->expires) {
  109. old->expires = now;
  110. }
  111. return true;
  112. }
  113. bool send_del_subnet(connection_t *c, const subnet_t *s) {
  114. char netstr[MAXNETSTR];
  115. if(!net2str(netstr, sizeof(netstr), s)) {
  116. return false;
  117. }
  118. return send_request(c, "%d %x %s %s", DEL_SUBNET, rand(), s->owner->name, netstr);
  119. }
  120. bool del_subnet_h(connection_t *c) {
  121. char subnetstr[MAX_STRING_SIZE];
  122. char name[MAX_STRING_SIZE];
  123. node_t *owner;
  124. subnet_t s = {0}, *find;
  125. if(sscanf(c->buffer, "%*d %*x " MAX_STRING " " MAX_STRING, name, subnetstr) != 2) {
  126. logger(LOG_ERR, "Got bad %s from %s (%s)", "DEL_SUBNET", c->name,
  127. c->hostname);
  128. return false;
  129. }
  130. /* Check if owner name is valid */
  131. if(!check_id(name)) {
  132. logger(LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_SUBNET", c->name,
  133. c->hostname, "invalid name");
  134. return false;
  135. }
  136. /* Check if subnet string is valid */
  137. if(!str2net(&s, subnetstr)) {
  138. logger(LOG_ERR, "Got bad %s from %s (%s): %s", "DEL_SUBNET", c->name,
  139. c->hostname, "invalid subnet string");
  140. return false;
  141. }
  142. if(seen_request(c->buffer)) {
  143. return true;
  144. }
  145. /* Check if the owner of the subnet being deleted is in the connection list */
  146. owner = lookup_node(name);
  147. if(tunnelserver && owner != myself && owner != c->node) {
  148. /* in case of tunnelserver, ignore indirect subnet deletion */
  149. ifdebug(PROTOCOL) logger(LOG_WARNING, "Ignoring indirect %s from %s (%s) for %s",
  150. "DEL_SUBNET", c->name, c->hostname, subnetstr);
  151. return true;
  152. }
  153. if(!owner) {
  154. ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for %s which is not in our node tree",
  155. "DEL_SUBNET", c->name, c->hostname, name);
  156. return true;
  157. }
  158. /* If everything is correct, delete the subnet from the list of the owner */
  159. s.owner = owner;
  160. find = lookup_subnet(owner, &s);
  161. if(!find) {
  162. ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for %s which does not appear in his subnet tree",
  163. "DEL_SUBNET", c->name, c->hostname, name);
  164. if(strictsubnets) {
  165. forward_request(c);
  166. }
  167. return true;
  168. }
  169. /* If we are the owner of this subnet, retaliate with an ADD_SUBNET */
  170. if(owner == myself) {
  171. ifdebug(PROTOCOL) logger(LOG_WARNING, "Got %s from %s (%s) for ourself",
  172. "DEL_SUBNET", c->name, c->hostname);
  173. send_add_subnet(c, find);
  174. return true;
  175. }
  176. if(tunnelserver) {
  177. return true;
  178. }
  179. /* Tell the rest */
  180. forward_request(c);
  181. if(strictsubnets) {
  182. return true;
  183. }
  184. /* Finally, delete it. */
  185. if(owner->status.reachable) {
  186. subnet_update(owner, find, false);
  187. }
  188. subnet_del(owner, find);
  189. return true;
  190. }