connected.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. extern struct zebra_t zebrad;
  36. /* communicate the withdrawal of a connected address */
  37. static void
  38. connected_withdraw (struct connected *ifc)
  39. {
  40. if (! ifc)
  41. return;
  42. /* Update interface address information to protocol daemon. */
  43. if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  44. {
  45. zebra_interface_address_delete_update (ifc->ifp, ifc);
  46. if (ifc->address->family == AF_INET)
  47. if_subnet_delete (ifc->ifp, ifc);
  48. if (ifc->address->family == AF_INET)
  49. connected_down_ipv4 (ifc->ifp, ifc);
  50. #ifdef HAVE_IPV6
  51. else
  52. connected_down_ipv6 (ifc->ifp, ifc);
  53. #endif
  54. UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
  55. }
  56. /* The address is not in the kernel anymore, so clear the flag */
  57. UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
  58. if (!CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  59. {
  60. listnode_delete (ifc->ifp->connected, ifc);
  61. connected_free (ifc);
  62. }
  63. }
  64. static void
  65. connected_announce (struct interface *ifp, struct connected *ifc)
  66. {
  67. if (!ifc)
  68. return;
  69. listnode_add (ifp->connected, ifc);
  70. /* Update interface address information to protocol daemon. */
  71. if (ifc->address->family == AF_INET)
  72. if_subnet_add (ifp, ifc);
  73. zebra_interface_address_add_update (ifp, ifc);
  74. if (if_is_operative(ifp))
  75. {
  76. if (ifc->address->family == AF_INET)
  77. connected_up_ipv4 (ifp, ifc);
  78. #ifdef HAVE_IPV6
  79. else
  80. connected_up_ipv6 (ifp, ifc);
  81. #endif
  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 in the same state */
  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. if (ifc1->conf != ifc2->conf)
  113. return 0;
  114. return 1;
  115. }
  116. /* Handle changes to addresses and send the neccesary announcements
  117. * to clients. */
  118. static void
  119. connected_update(struct interface *ifp, struct connected *ifc)
  120. {
  121. struct connected *current;
  122. /* Check same connected route. */
  123. if ((current = connected_check (ifp, (struct prefix *) ifc->address)))
  124. {
  125. if (CHECK_FLAG(current->conf, ZEBRA_IFC_CONFIGURED))
  126. SET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);
  127. /* Avoid spurious withdraws, this might be just the kernel 'reflecting'
  128. * back an address we have already added.
  129. */
  130. if (connected_same (current, ifc))
  131. {
  132. /* nothing to do */
  133. connected_free (ifc);
  134. return;
  135. }
  136. /* Clear the configured flag on the old ifc, so it will be freed by
  137. * connected withdraw. */
  138. UNSET_FLAG(current->conf, ZEBRA_IFC_CONFIGURED);
  139. connected_withdraw (current); /* implicit withdraw - freebsd does this */
  140. }
  141. /* If the connected is new or has changed, announce it, if it is usable */
  142. if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
  143. connected_announce(ifp, ifc);
  144. }
  145. /* Called from if_up(). */
  146. void
  147. connected_up_ipv4 (struct interface *ifp, struct connected *ifc)
  148. {
  149. struct prefix_ipv4 p;
  150. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  151. return;
  152. PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
  153. /* Apply mask to the network. */
  154. apply_mask_ipv4 (&p);
  155. /* In case of connected address is 0.0.0.0/0 we treat it tunnel
  156. address. */
  157. if (prefix_ipv4_any (&p))
  158. return;
  159. rib_add_ipv4 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, NULL, ifp->ifindex,
  160. RT_TABLE_MAIN, ifp->metric, 0, SAFI_UNICAST);
  161. rib_add_ipv4 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, NULL, ifp->ifindex,
  162. RT_TABLE_MAIN, ifp->metric, 0, SAFI_MULTICAST);
  163. rib_update ();
  164. }
  165. /* Add connected IPv4 route to the interface. */
  166. void
  167. connected_add_ipv4 (struct interface *ifp, int flags, struct in_addr *addr,
  168. u_char prefixlen, struct in_addr *broad,
  169. const char *label)
  170. {
  171. struct prefix_ipv4 *p;
  172. struct connected *ifc;
  173. /* Make connected structure. */
  174. ifc = connected_new ();
  175. ifc->ifp = ifp;
  176. ifc->flags = flags;
  177. /* If we get a notification from the kernel,
  178. * we can safely assume the address is known to the kernel */
  179. SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
  180. /* Allocate new connected address. */
  181. p = prefix_ipv4_new ();
  182. p->family = AF_INET;
  183. p->prefix = *addr;
  184. p->prefixlen = prefixlen;
  185. ifc->address = (struct prefix *) p;
  186. /* If there is broadcast or peer address. */
  187. if (broad)
  188. {
  189. p = prefix_ipv4_new ();
  190. p->family = AF_INET;
  191. p->prefix = *broad;
  192. p->prefixlen = prefixlen;
  193. ifc->destination = (struct prefix *) p;
  194. /* validate the destination address */
  195. if (CONNECTED_PEER(ifc))
  196. {
  197. if (IPV4_ADDR_SAME(addr,broad))
  198. zlog_warn("warning: interface %s has same local and peer "
  199. "address %s, routing protocols may malfunction",
  200. ifp->name,inet_ntoa(*addr));
  201. }
  202. else
  203. {
  204. if (broad->s_addr != ipv4_broadcast_addr(addr->s_addr,prefixlen))
  205. {
  206. char buf[2][INET_ADDRSTRLEN];
  207. struct in_addr bcalc;
  208. bcalc.s_addr = ipv4_broadcast_addr(addr->s_addr,prefixlen);
  209. zlog_warn("warning: interface %s broadcast addr %s/%d != "
  210. "calculated %s, routing protocols may malfunction",
  211. ifp->name,
  212. inet_ntop (AF_INET, broad, buf[0], sizeof(buf[0])),
  213. prefixlen,
  214. inet_ntop (AF_INET, &bcalc, buf[1], sizeof(buf[1])));
  215. }
  216. }
  217. }
  218. else
  219. {
  220. if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER))
  221. {
  222. zlog_warn("warning: %s called for interface %s "
  223. "with peer flag set, but no peer address supplied",
  224. __func__, ifp->name);
  225. UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
  226. }
  227. /* no broadcast or destination address was supplied */
  228. if ((prefixlen == IPV4_MAX_PREFIXLEN) && if_is_pointopoint(ifp))
  229. zlog_warn("warning: PtP interface %s with addr %s/%d needs a "
  230. "peer address",ifp->name,inet_ntoa(*addr),prefixlen);
  231. }
  232. /* Label of this address. */
  233. if (label)
  234. ifc->label = XSTRDUP (MTYPE_CONNECTED_LABEL, label);
  235. /* For all that I know an IPv4 address is always ready when we receive
  236. * the notification. So it should be safe to set the REAL flag here. */
  237. SET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
  238. connected_update(ifp, ifc);
  239. }
  240. void
  241. connected_down_ipv4 (struct interface *ifp, struct connected *ifc)
  242. {
  243. struct prefix_ipv4 p;
  244. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  245. return;
  246. PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
  247. /* Apply mask to the network. */
  248. apply_mask_ipv4 (&p);
  249. /* In case of connected address is 0.0.0.0/0 we treat it tunnel
  250. address. */
  251. if (prefix_ipv4_any (&p))
  252. return;
  253. /* Same logic as for connected_up_ipv4(): push the changes into the head. */
  254. rib_delete_ipv4 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0, SAFI_UNICAST);
  255. rib_delete_ipv4 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0, SAFI_MULTICAST);
  256. rib_update ();
  257. }
  258. /* Delete connected IPv4 route to the interface. */
  259. void
  260. connected_delete_ipv4 (struct interface *ifp, int flags, struct in_addr *addr,
  261. u_char prefixlen, struct in_addr *broad)
  262. {
  263. struct prefix_ipv4 p;
  264. struct connected *ifc;
  265. memset (&p, 0, sizeof (struct prefix_ipv4));
  266. p.family = AF_INET;
  267. p.prefix = *addr;
  268. p.prefixlen = prefixlen;
  269. ifc = connected_check (ifp, (struct prefix *) &p);
  270. if (! ifc)
  271. return;
  272. connected_withdraw (ifc);
  273. rib_update();
  274. }
  275. #ifdef HAVE_IPV6
  276. void
  277. connected_up_ipv6 (struct interface *ifp, struct connected *ifc)
  278. {
  279. struct prefix_ipv6 p;
  280. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  281. return;
  282. PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
  283. /* Apply mask to the network. */
  284. apply_mask_ipv6 (&p);
  285. #if ! defined (MUSICA) && ! defined (LINUX)
  286. /* XXX: It is already done by rib_bogus_ipv6 within rib_add_ipv6 */
  287. if (IN6_IS_ADDR_UNSPECIFIED (&p.prefix))
  288. return;
  289. #endif
  290. rib_add_ipv6 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, RT_TABLE_MAIN,
  291. ifp->metric, 0, SAFI_UNICAST);
  292. rib_update ();
  293. }
  294. /* Add connected IPv6 route to the interface. */
  295. void
  296. connected_add_ipv6 (struct interface *ifp, int flags, struct in6_addr *addr,
  297. u_char prefixlen, struct in6_addr *broad,
  298. const char *label)
  299. {
  300. struct prefix_ipv6 *p;
  301. struct connected *ifc;
  302. /* Make connected structure. */
  303. ifc = connected_new ();
  304. ifc->ifp = ifp;
  305. ifc->flags = flags;
  306. /* If we get a notification from the kernel,
  307. * we can safely assume the address is known to the kernel */
  308. SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
  309. /* Allocate new connected address. */
  310. p = prefix_ipv6_new ();
  311. p->family = AF_INET6;
  312. IPV6_ADDR_COPY (&p->prefix, addr);
  313. p->prefixlen = prefixlen;
  314. ifc->address = (struct prefix *) p;
  315. /* If there is broadcast or peer address. */
  316. if (broad)
  317. {
  318. if (IN6_IS_ADDR_UNSPECIFIED(broad))
  319. zlog_warn("warning: %s called for interface %s with unspecified "
  320. "destination address; ignoring!", __func__, ifp->name);
  321. else
  322. {
  323. p = prefix_ipv6_new ();
  324. p->family = AF_INET6;
  325. IPV6_ADDR_COPY (&p->prefix, broad);
  326. p->prefixlen = prefixlen;
  327. ifc->destination = (struct prefix *) p;
  328. }
  329. }
  330. if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER) && !ifc->destination)
  331. {
  332. zlog_warn("warning: %s called for interface %s "
  333. "with peer flag set, but no peer address supplied",
  334. __func__, ifp->name);
  335. UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
  336. }
  337. /* Label of this address. */
  338. if (label)
  339. ifc->label = XSTRDUP (MTYPE_CONNECTED_LABEL, label);
  340. /* On Linux, we only get here when DAD is complete, therefore we can set
  341. * ZEBRA_IFC_REAL.
  342. *
  343. * On BSD, there currently doesn't seem to be a way to check for completion of
  344. * DAD, so we replicate the old behaviour and set ZEBRA_IFC_REAL, although DAD
  345. * might still be running.
  346. */
  347. SET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
  348. connected_update(ifp, ifc);
  349. }
  350. void
  351. connected_down_ipv6 (struct interface *ifp, struct connected *ifc)
  352. {
  353. struct prefix_ipv6 p;
  354. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
  355. return;
  356. PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
  357. apply_mask_ipv6 (&p);
  358. if (IN6_IS_ADDR_UNSPECIFIED (&p.prefix))
  359. return;
  360. rib_delete_ipv6 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0, SAFI_UNICAST);
  361. rib_update ();
  362. }
  363. void
  364. connected_delete_ipv6 (struct interface *ifp, struct in6_addr *address,
  365. u_char prefixlen, struct in6_addr *broad)
  366. {
  367. struct prefix_ipv6 p;
  368. struct connected *ifc;
  369. memset (&p, 0, sizeof (struct prefix_ipv6));
  370. p.family = AF_INET6;
  371. memcpy (&p.prefix, address, sizeof (struct in6_addr));
  372. p.prefixlen = prefixlen;
  373. ifc = connected_check (ifp, (struct prefix *) &p);
  374. if (! ifc)
  375. return;
  376. connected_withdraw (ifc);
  377. rib_update();
  378. }
  379. #endif /* HAVE_IPV6 */