connected.c 12 KB

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