connected.c 13 KB

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