connected.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Address linked list routine.
  3. * Copyright (C) 1997, 98 Kunihiro Ishiguro
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "prefix.h"
  24. #include "linklist.h"
  25. #include "if.h"
  26. #include "table.h"
  27. #include "rib.h"
  28. #include "table.h"
  29. #include "log.h"
  30. #include "zebra/zserv.h"
  31. #include "zebra/redistribute.h"
  32. /* If same interface address is already exist... */
  33. struct connected *
  34. connected_check_ipv4 (struct interface *ifp, struct prefix *p)
  35. {
  36. struct connected *ifc;
  37. listnode node;
  38. for (node = listhead (ifp->connected); node; node = nextnode (node))
  39. {
  40. ifc = getdata (node);
  41. if (prefix_same (ifc->address, p))
  42. return ifc;
  43. }
  44. return NULL;
  45. }
  46. /* Called from if_up(). */
  47. void
  48. connected_up_ipv4 (struct interface *ifp, struct connected *ifc)
  49. {
  50. struct prefix_ipv4 p;
  51. struct prefix_ipv4 *addr;
  52. struct prefix_ipv4 *dest;
  53. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  54. return;
  55. addr = (struct prefix_ipv4 *) ifc->address;
  56. dest = (struct prefix_ipv4 *) ifc->destination;
  57. memset (&p, 0, sizeof (struct prefix_ipv4));
  58. p.family = AF_INET;
  59. p.prefixlen = addr->prefixlen;
  60. /* Point-to-point check. */
  61. if (if_is_pointopoint (ifp) && dest)
  62. p.prefix = dest->prefix;
  63. else
  64. p.prefix = addr->prefix;
  65. /* Apply mask to the network. */
  66. apply_mask_ipv4 (&p);
  67. /* In case of connected address is 0.0.0.0/0 we treat it tunnel
  68. address. */
  69. if (prefix_ipv4_any (&p))
  70. return;
  71. rib_add_ipv4 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0, 0, 0);
  72. rib_update ();
  73. }
  74. /* Add connected IPv4 route to the interface. */
  75. void
  76. connected_add_ipv4 (struct interface *ifp, int flags, struct in_addr *addr,
  77. int prefixlen, struct in_addr *broad, char *label)
  78. {
  79. struct prefix_ipv4 *p;
  80. struct connected *ifc;
  81. struct connected *current;
  82. /* Make connected structure. */
  83. ifc = connected_new ();
  84. ifc->ifp = ifp;
  85. ifc->flags = flags;
  86. /* Allocate new connected address. */
  87. p = prefix_ipv4_new ();
  88. p->family = AF_INET;
  89. p->prefix = *addr;
  90. p->prefixlen = prefixlen;
  91. ifc->address = (struct prefix *) p;
  92. /* If there is broadcast or pointopoint address. */
  93. if (broad)
  94. {
  95. p = prefix_ipv4_new ();
  96. p->family = AF_INET;
  97. p->prefix = *broad;
  98. ifc->destination = (struct prefix *) p;
  99. }
  100. /* Label of this address. */
  101. if (label)
  102. ifc->label = strdup (label);
  103. /* Check same connected route. */
  104. current = connected_check_ipv4 (ifp, (struct prefix *) ifc->address);
  105. if (current)
  106. {
  107. connected_free (ifc);
  108. ifc = current;
  109. }
  110. else
  111. {
  112. listnode_add (ifp->connected, ifc);
  113. }
  114. /* Update interface address information to protocol daemon. */
  115. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  116. {
  117. SET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
  118. zebra_interface_address_add_update (ifp, ifc);
  119. if (if_is_up(ifp))
  120. connected_up_ipv4 (ifp, ifc);
  121. }
  122. }
  123. void
  124. connected_down_ipv4 (struct interface *ifp, struct connected *ifc)
  125. {
  126. struct prefix_ipv4 p;
  127. struct prefix_ipv4 *addr;
  128. struct prefix_ipv4 *dest;
  129. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  130. return;
  131. addr = (struct prefix_ipv4 *)ifc->address;
  132. dest = (struct prefix_ipv4 *)ifc->destination;
  133. memset (&p, 0, sizeof (struct prefix_ipv4));
  134. p.family = AF_INET;
  135. p.prefixlen = addr->prefixlen;
  136. /* Point-to-point check. */
  137. if (dest && if_is_pointopoint (ifp))
  138. p.prefix = dest->prefix;
  139. else
  140. p.prefix = addr->prefix;
  141. /* Apply mask to the network. */
  142. apply_mask_ipv4 (&p);
  143. /* In case of connected address is 0.0.0.0/0 we treat it tunnel
  144. address. */
  145. if (prefix_ipv4_any (&p))
  146. return;
  147. rib_delete_ipv4 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0);
  148. rib_update ();
  149. }
  150. /* Delete connected IPv4 route to the interface. */
  151. void
  152. connected_delete_ipv4 (struct interface *ifp, int flags, struct in_addr *addr,
  153. int prefixlen, struct in_addr *broad, char *label)
  154. {
  155. struct prefix_ipv4 p;
  156. struct connected *ifc;
  157. memset (&p, 0, sizeof (struct prefix_ipv4));
  158. p.family = AF_INET;
  159. p.prefix = *addr;
  160. p.prefixlen = prefixlen;
  161. ifc = connected_check_ipv4 (ifp, (struct prefix *) &p);
  162. if (! ifc)
  163. return;
  164. /* Update interface address information to protocol daemon. */
  165. if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  166. {
  167. zebra_interface_address_delete_update (ifp, ifc);
  168. connected_down_ipv4 (ifp, ifc);
  169. UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
  170. }
  171. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  172. {
  173. listnode_delete (ifp->connected, ifc);
  174. connected_free (ifc);
  175. }
  176. }
  177. #ifdef HAVE_IPV6
  178. /* If same interface address is already exist... */
  179. struct connected *
  180. connected_check_ipv6 (struct interface *ifp, struct prefix *p)
  181. {
  182. struct connected *ifc;
  183. listnode node;
  184. for (node = listhead (ifp->connected); node; node = nextnode (node))
  185. {
  186. ifc = getdata (node);
  187. if (prefix_same (ifc->address, p))
  188. return ifc;
  189. }
  190. return 0;
  191. }
  192. void
  193. connected_up_ipv6 (struct interface *ifp, struct connected *ifc)
  194. {
  195. struct prefix_ipv6 p;
  196. struct prefix_ipv6 *addr;
  197. struct prefix_ipv6 *dest;
  198. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  199. return;
  200. addr = (struct prefix_ipv6 *) ifc->address;
  201. dest = (struct prefix_ipv6 *) ifc->destination;
  202. memset (&p, 0, sizeof (struct prefix_ipv6));
  203. p.family = AF_INET6;
  204. p.prefixlen = addr->prefixlen;
  205. if (if_is_pointopoint (ifp) && dest)
  206. {
  207. if (IN6_IS_ADDR_UNSPECIFIED (&dest->prefix))
  208. p.prefix = addr->prefix;
  209. else
  210. p.prefix = dest->prefix;
  211. }
  212. else
  213. p.prefix = addr->prefix;
  214. /* Apply mask to the network. */
  215. apply_mask_ipv6 (&p);
  216. #if ! defined (MUSICA) && ! defined (LINUX)
  217. /* XXX: It is already done by rib_bogus_ipv6 within rib_add_ipv6 */
  218. if (IN6_IS_ADDR_UNSPECIFIED (&p.prefix))
  219. return;
  220. #endif
  221. rib_add_ipv6 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0);
  222. rib_update ();
  223. }
  224. /* Add connected IPv6 route to the interface. */
  225. void
  226. connected_add_ipv6 (struct interface *ifp, struct in6_addr *addr,
  227. int prefixlen, struct in6_addr *broad)
  228. {
  229. struct prefix_ipv6 *p;
  230. struct connected *ifc;
  231. struct connected *current;
  232. /* Make connected structure. */
  233. ifc = connected_new ();
  234. ifc->ifp = ifp;
  235. /* Allocate new connected address. */
  236. p = prefix_ipv6_new ();
  237. p->family = AF_INET6;
  238. IPV6_ADDR_COPY (&p->prefix, addr);
  239. p->prefixlen = prefixlen;
  240. ifc->address = (struct prefix *) p;
  241. /* If there is broadcast or pointopoint address. */
  242. if (broad)
  243. {
  244. p = prefix_ipv6_new ();
  245. p->family = AF_INET6;
  246. IPV6_ADDR_COPY (&p->prefix, broad);
  247. ifc->destination = (struct prefix *) p;
  248. }
  249. current = connected_check_ipv6 (ifp, (struct prefix *) ifc->address);
  250. if (current)
  251. {
  252. connected_free (ifc);
  253. ifc = current;
  254. }
  255. else
  256. {
  257. listnode_add (ifp->connected, ifc);
  258. }
  259. /* Update interface address information to protocol daemon. */
  260. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  261. {
  262. SET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
  263. zebra_interface_address_add_update (ifp, ifc);
  264. if (if_is_up(ifp))
  265. connected_up_ipv6 (ifp, ifc);
  266. }
  267. }
  268. void
  269. connected_down_ipv6 (struct interface *ifp, struct connected *ifc)
  270. {
  271. struct prefix_ipv6 p;
  272. struct prefix_ipv6 *addr;
  273. struct prefix_ipv6 *dest;
  274. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  275. return;
  276. addr = (struct prefix_ipv6 *) ifc->address;
  277. dest = (struct prefix_ipv6 *) ifc->destination;
  278. memset (&p, 0, sizeof (struct prefix_ipv6));
  279. p.family = AF_INET6;
  280. p.prefixlen = addr->prefixlen;
  281. if (if_is_pointopoint (ifp) && dest)
  282. {
  283. if (IN6_IS_ADDR_UNSPECIFIED (&dest->prefix))
  284. p.prefix = addr->prefix;
  285. else
  286. p.prefix = dest->prefix;
  287. }
  288. else
  289. p.prefix = addr->prefix;
  290. apply_mask_ipv6 (&p);
  291. if (IN6_IS_ADDR_UNSPECIFIED (&p.prefix))
  292. return;
  293. rib_delete_ipv6 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0);
  294. rib_update ();
  295. }
  296. void
  297. connected_delete_ipv6 (struct interface *ifp, struct in6_addr *address,
  298. int prefixlen, struct in6_addr *broad)
  299. {
  300. struct prefix_ipv6 p;
  301. struct connected *ifc;
  302. memset (&p, 0, sizeof (struct prefix_ipv6));
  303. p.family = AF_INET6;
  304. memcpy (&p.prefix, address, sizeof (struct in6_addr));
  305. p.prefixlen = prefixlen;
  306. ifc = connected_check_ipv6 (ifp, (struct prefix *) &p);
  307. if (! ifc)
  308. return;
  309. /* Update interface address information to protocol daemon. */
  310. if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  311. {
  312. zebra_interface_address_delete_update (ifp, ifc);
  313. connected_down_ipv6 (ifp, ifc);
  314. UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
  315. }
  316. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  317. {
  318. listnode_delete (ifp->connected, ifc);
  319. connected_free (ifc);
  320. }
  321. }
  322. #endif /* HAVE_IPV6 */