connected.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 "memory.h"
  31. #include "zebra/zserv.h"
  32. #include "zebra/redistribute.h"
  33. #include "zebra/interface.h"
  34. #include "zebra/connected.h"
  35. /* withdraw a connected address */
  36. static void
  37. connected_withdraw (struct connected *ifc)
  38. {
  39. if (! ifc)
  40. return;
  41. /* Update interface address information to protocol daemon. */
  42. if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  43. {
  44. zebra_interface_address_delete_update (ifc->ifp, ifc);
  45. if_subnet_delete (ifc->ifp, ifc);
  46. if (ifc->address->family == AF_INET)
  47. connected_down_ipv4 (ifc->ifp, ifc);
  48. #ifdef HAVE_IPV6
  49. else
  50. connected_down_ipv6 (ifc->ifp, ifc);
  51. #endif
  52. UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
  53. }
  54. if (!CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  55. {
  56. listnode_delete (ifc->ifp->connected, ifc);
  57. connected_free (ifc);
  58. }
  59. }
  60. static void
  61. connected_announce (struct interface *ifp, struct connected *ifc)
  62. {
  63. if (!ifc)
  64. return;
  65. listnode_add (ifp->connected, ifc);
  66. /* Update interface address information to protocol daemon. */
  67. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  68. {
  69. if (ifc->address->family == AF_INET)
  70. if_subnet_add (ifp, ifc);
  71. SET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
  72. zebra_interface_address_add_update (ifp, ifc);
  73. if (if_is_up(ifp))
  74. {
  75. if (ifc->address->family == AF_INET)
  76. connected_up_ipv4 (ifp, ifc);
  77. #ifdef HAVE_IPV6
  78. else
  79. connected_up_ipv6 (ifp, ifc);
  80. #endif
  81. }
  82. }
  83. }
  84. /* If same interface address is already exist... */
  85. struct connected *
  86. connected_check (struct interface *ifp, struct prefix *p)
  87. {
  88. struct connected *ifc;
  89. struct listnode *node;
  90. for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
  91. if (prefix_same (ifc->address, p))
  92. return ifc;
  93. return NULL;
  94. }
  95. /* Check if two ifc's describe the same address */
  96. static int
  97. connected_same (struct connected *ifc1, struct connected *ifc2)
  98. {
  99. if (ifc1->ifp != ifc2->ifp)
  100. return 0;
  101. if (ifc1->destination)
  102. if (!ifc2->destination)
  103. return 0;
  104. if (ifc2->destination)
  105. if (!ifc1->destination)
  106. return 0;
  107. if (ifc1->destination && ifc2->destination)
  108. if (!prefix_same (ifc1->destination, ifc2->destination))
  109. return 0;
  110. if (ifc1->flags != ifc2->flags)
  111. return 0;
  112. return 1;
  113. }
  114. /* Handle implicit withdrawals of addresses, where a system ADDs an address
  115. * to an interface which already has the same address configured.
  116. *
  117. * Returns the struct connected which must be announced to clients,
  118. * or NULL if nothing to do.
  119. */
  120. static struct connected *
  121. connected_implicit_withdraw (struct interface *ifp, struct connected *ifc)
  122. {
  123. struct connected *current;
  124. /* Check same connected route. */
  125. if ((current = connected_check (ifp, (struct prefix *) ifc->address)))
  126. {
  127. if (CHECK_FLAG(current->conf, ZEBRA_IFC_CONFIGURED))
  128. SET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);
  129. /* Avoid spurious withdraws, this might be just the kernel 'reflecting'
  130. * back an address we have already added.
  131. */
  132. if (connected_same (current, ifc))
  133. {
  134. /* nothing to do */
  135. connected_free (ifc);
  136. return NULL;
  137. }
  138. UNSET_FLAG(current->conf, ZEBRA_IFC_CONFIGURED);
  139. connected_withdraw (current); /* implicit withdraw - freebsd does this */
  140. }
  141. return ifc;
  142. }
  143. /* Called from if_up(). */
  144. void
  145. connected_up_ipv4 (struct interface *ifp, struct connected *ifc)
  146. {
  147. struct prefix_ipv4 p;
  148. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  149. return;
  150. PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
  151. /* Apply mask to the network. */
  152. apply_mask_ipv4 (&p);
  153. /* In case of connected address is 0.0.0.0/0 we treat it tunnel
  154. address. */
  155. if (prefix_ipv4_any (&p))
  156. return;
  157. rib_add_ipv4 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, RT_TABLE_MAIN,
  158. ifp->metric, 0);
  159. rib_update ();
  160. }
  161. /* Add connected IPv4 route to the interface. */
  162. void
  163. connected_add_ipv4 (struct interface *ifp, int flags, struct in_addr *addr,
  164. u_char prefixlen, struct in_addr *broad,
  165. const char *label)
  166. {
  167. struct prefix_ipv4 *p;
  168. struct connected *ifc;
  169. /* Make connected structure. */
  170. ifc = connected_new ();
  171. ifc->ifp = ifp;
  172. ifc->flags = flags;
  173. /* Allocate new connected address. */
  174. p = prefix_ipv4_new ();
  175. p->family = AF_INET;
  176. p->prefix = *addr;
  177. p->prefixlen = prefixlen;
  178. ifc->address = (struct prefix *) p;
  179. /* If there is broadcast or peer address. */
  180. if (broad)
  181. {
  182. p = prefix_ipv4_new ();
  183. p->family = AF_INET;
  184. p->prefix = *broad;
  185. p->prefixlen = prefixlen;
  186. ifc->destination = (struct prefix *) p;
  187. /* validate the destination address */
  188. if (CONNECTED_PEER(ifc))
  189. {
  190. if (IPV4_ADDR_SAME(addr,broad))
  191. zlog_warn("warning: interface %s has same local and peer "
  192. "address %s, routing protocols may malfunction",
  193. ifp->name,inet_ntoa(*addr));
  194. }
  195. else
  196. {
  197. if (broad->s_addr != ipv4_broadcast_addr(addr->s_addr,prefixlen))
  198. {
  199. char buf[2][INET_ADDRSTRLEN];
  200. struct in_addr bcalc;
  201. bcalc.s_addr = ipv4_broadcast_addr(addr->s_addr,prefixlen);
  202. zlog_warn("warning: interface %s broadcast addr %s/%d != "
  203. "calculated %s, routing protocols may malfunction",
  204. ifp->name,
  205. inet_ntop (AF_INET, broad, buf[0], sizeof(buf[0])),
  206. prefixlen,
  207. inet_ntop (AF_INET, &bcalc, buf[1], sizeof(buf[1])));
  208. }
  209. }
  210. }
  211. else
  212. {
  213. if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER))
  214. {
  215. zlog_warn("warning: %s called for interface %s "
  216. "with peer flag set, but no peer address supplied",
  217. __func__, ifp->name);
  218. UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
  219. }
  220. /* no broadcast or destination address was supplied */
  221. if ((prefixlen == IPV4_MAX_PREFIXLEN) && if_is_pointopoint(ifp))
  222. zlog_warn("warning: PtP interface %s with addr %s/%d needs a "
  223. "peer address",ifp->name,inet_ntoa(*addr),prefixlen);
  224. }
  225. /* Label of this address. */
  226. if (label)
  227. ifc->label = XSTRDUP (MTYPE_CONNECTED_LABEL, label);
  228. /* nothing to do? */
  229. if ((ifc = connected_implicit_withdraw (ifp, ifc)) == NULL)
  230. return;
  231. connected_announce (ifp, ifc);
  232. }
  233. void
  234. connected_down_ipv4 (struct interface *ifp, struct connected *ifc)
  235. {
  236. struct prefix_ipv4 p;
  237. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  238. return;
  239. PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
  240. /* Apply mask to the network. */
  241. apply_mask_ipv4 (&p);
  242. /* In case of connected address is 0.0.0.0/0 we treat it tunnel
  243. address. */
  244. if (prefix_ipv4_any (&p))
  245. return;
  246. rib_delete_ipv4 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0);
  247. rib_update ();
  248. }
  249. /* Delete connected IPv4 route to the interface. */
  250. void
  251. connected_delete_ipv4 (struct interface *ifp, int flags, struct in_addr *addr,
  252. u_char prefixlen, struct in_addr *broad)
  253. {
  254. struct prefix_ipv4 p;
  255. struct connected *ifc;
  256. memset (&p, 0, sizeof (struct prefix_ipv4));
  257. p.family = AF_INET;
  258. p.prefix = *addr;
  259. p.prefixlen = prefixlen;
  260. ifc = connected_check (ifp, (struct prefix *) &p);
  261. if (! ifc)
  262. return;
  263. connected_withdraw (ifc);
  264. }
  265. #ifdef HAVE_IPV6
  266. void
  267. connected_up_ipv6 (struct interface *ifp, struct connected *ifc)
  268. {
  269. struct prefix_ipv6 p;
  270. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  271. return;
  272. PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
  273. /* Apply mask to the network. */
  274. apply_mask_ipv6 (&p);
  275. #if ! defined (MUSICA) && ! defined (LINUX)
  276. /* XXX: It is already done by rib_bogus_ipv6 within rib_add_ipv6 */
  277. if (IN6_IS_ADDR_UNSPECIFIED (&p.prefix))
  278. return;
  279. #endif
  280. rib_add_ipv6 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0,
  281. ifp->metric, 0);
  282. rib_update ();
  283. }
  284. /* Add connected IPv6 route to the interface. */
  285. void
  286. connected_add_ipv6 (struct interface *ifp, int flags, struct in6_addr *addr,
  287. u_char prefixlen, struct in6_addr *broad,
  288. const char *label)
  289. {
  290. struct prefix_ipv6 *p;
  291. struct connected *ifc;
  292. /* Make connected structure. */
  293. ifc = connected_new ();
  294. ifc->ifp = ifp;
  295. ifc->flags = flags;
  296. /* Allocate new connected address. */
  297. p = prefix_ipv6_new ();
  298. p->family = AF_INET6;
  299. IPV6_ADDR_COPY (&p->prefix, addr);
  300. p->prefixlen = prefixlen;
  301. ifc->address = (struct prefix *) p;
  302. /* If there is broadcast or peer address. */
  303. if (broad)
  304. {
  305. if (IN6_IS_ADDR_UNSPECIFIED(broad))
  306. zlog_warn("warning: %s called for interface %s with unspecified "
  307. "destination address; ignoring!", __func__, ifp->name);
  308. else
  309. {
  310. p = prefix_ipv6_new ();
  311. p->family = AF_INET6;
  312. IPV6_ADDR_COPY (&p->prefix, broad);
  313. p->prefixlen = prefixlen;
  314. ifc->destination = (struct prefix *) p;
  315. }
  316. }
  317. if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER) && !ifc->destination)
  318. {
  319. zlog_warn("warning: %s called for interface %s "
  320. "with peer flag set, but no peer address supplied",
  321. __func__, ifp->name);
  322. UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
  323. }
  324. /* Label of this address. */
  325. if (label)
  326. ifc->label = XSTRDUP (MTYPE_CONNECTED_LABEL, label);
  327. if ((ifc = connected_implicit_withdraw (ifp, ifc)) == NULL)
  328. return;
  329. connected_announce (ifp, ifc);
  330. }
  331. void
  332. connected_down_ipv6 (struct interface *ifp, struct connected *ifc)
  333. {
  334. struct prefix_ipv6 p;
  335. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  336. return;
  337. PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
  338. apply_mask_ipv6 (&p);
  339. if (IN6_IS_ADDR_UNSPECIFIED (&p.prefix))
  340. return;
  341. rib_delete_ipv6 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0);
  342. rib_update ();
  343. }
  344. void
  345. connected_delete_ipv6 (struct interface *ifp, struct in6_addr *address,
  346. u_char prefixlen, struct in6_addr *broad)
  347. {
  348. struct prefix_ipv6 p;
  349. struct connected *ifc;
  350. memset (&p, 0, sizeof (struct prefix_ipv6));
  351. p.family = AF_INET6;
  352. memcpy (&p.prefix, address, sizeof (struct in6_addr));
  353. p.prefixlen = prefixlen;
  354. ifc = connected_check (ifp, (struct prefix *) &p);
  355. if (! ifc)
  356. return;
  357. connected_withdraw (ifc);
  358. }
  359. #endif /* HAVE_IPV6 */