pim_zlookup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. PIM for Quagga
  3. Copyright (C) 2008 Everton da Silva Marques
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING; if not, write to the
  14. Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  15. MA 02110-1301 USA
  16. $QuaggaId: $Format:%an, %ai, %h$ $
  17. */
  18. #include <zebra.h>
  19. #include "zebra/rib.h"
  20. #include "log.h"
  21. #include "prefix.h"
  22. #include "zclient.h"
  23. #include "stream.h"
  24. #include "network.h"
  25. #include "thread.h"
  26. #include "pimd.h"
  27. #include "pim_pim.h"
  28. #include "pim_str.h"
  29. #include "pim_zlookup.h"
  30. extern int zclient_debug;
  31. static void zclient_lookup_sched(struct zclient *zlookup, int delay);
  32. /* Connect to zebra for nexthop lookup. */
  33. static int zclient_lookup_connect(struct thread *t)
  34. {
  35. struct zclient *zlookup;
  36. zlookup = THREAD_ARG(t);
  37. zlookup->t_connect = NULL;
  38. if (zlookup->sock >= 0) {
  39. return 0;
  40. }
  41. if (zclient_socket_connect(zlookup) < 0) {
  42. ++zlookup->fail;
  43. zlog_warn("%s: failure connecting zclient socket: failures=%d",
  44. __PRETTY_FUNCTION__, zlookup->fail);
  45. }
  46. else {
  47. zlookup->fail = 0; /* reset counter on connection */
  48. }
  49. zassert(!zlookup->t_connect);
  50. if (zlookup->sock < 0) {
  51. /* Since last connect failed, retry within 10 secs */
  52. zclient_lookup_sched(zlookup, 10);
  53. return -1;
  54. }
  55. return 0;
  56. }
  57. /* Schedule connection with delay. */
  58. static void zclient_lookup_sched(struct zclient *zlookup, int delay)
  59. {
  60. zassert(!zlookup->t_connect);
  61. THREAD_TIMER_ON(master, zlookup->t_connect,
  62. zclient_lookup_connect,
  63. zlookup, delay);
  64. zlog_notice("%s: zclient lookup connection scheduled for %d seconds",
  65. __PRETTY_FUNCTION__, delay);
  66. }
  67. /* Schedule connection for now. */
  68. static void zclient_lookup_sched_now(struct zclient *zlookup)
  69. {
  70. zassert(!zlookup->t_connect);
  71. zlookup->t_connect = thread_add_event(master, zclient_lookup_connect,
  72. zlookup, 0);
  73. zlog_notice("%s: zclient lookup immediate connection scheduled",
  74. __PRETTY_FUNCTION__);
  75. }
  76. /* Schedule reconnection, if needed. */
  77. static void zclient_lookup_reconnect(struct zclient *zlookup)
  78. {
  79. if (zlookup->t_connect) {
  80. return;
  81. }
  82. zclient_lookup_sched_now(zlookup);
  83. }
  84. static void zclient_lookup_failed(struct zclient *zlookup)
  85. {
  86. if (zlookup->sock >= 0) {
  87. if (close(zlookup->sock)) {
  88. zlog_warn("%s: closing fd=%d: errno=%d %s", __func__, zlookup->sock,
  89. errno, safe_strerror(errno));
  90. }
  91. zlookup->sock = -1;
  92. }
  93. zclient_lookup_reconnect(zlookup);
  94. }
  95. struct zclient *zclient_lookup_new()
  96. {
  97. struct zclient *zlookup;
  98. zlookup = zclient_new (master);
  99. if (!zlookup) {
  100. zlog_err("%s: zclient_new() failure",
  101. __PRETTY_FUNCTION__);
  102. return 0;
  103. }
  104. zlookup->sock = -1;
  105. zlookup->ibuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
  106. zlookup->obuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
  107. zlookup->t_connect = 0;
  108. zclient_lookup_sched_now(zlookup);
  109. zlog_notice("%s: zclient lookup socket initialized",
  110. __PRETTY_FUNCTION__);
  111. return zlookup;
  112. }
  113. static int zclient_read_nexthop(struct zclient *zlookup,
  114. struct pim_zlookup_nexthop nexthop_tab[],
  115. const int tab_size,
  116. struct in_addr addr)
  117. {
  118. int num_ifindex = 0;
  119. struct stream *s;
  120. const uint16_t MIN_LEN = 10; /* getipv4=4 getc=1 getl=4 getc=1 */
  121. uint16_t length;
  122. u_char marker;
  123. u_char version;
  124. uint16_t vrf_id;
  125. uint16_t command;
  126. struct in_addr raddr;
  127. uint8_t distance;
  128. uint32_t metric;
  129. int nexthop_num;
  130. int i, err;
  131. if (PIM_DEBUG_ZEBRA) {
  132. char addr_str[100];
  133. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  134. zlog_debug("%s: addr=%s",
  135. __PRETTY_FUNCTION__,
  136. addr_str);
  137. }
  138. s = zlookup->ibuf;
  139. stream_reset(s);
  140. err = zclient_read_header (s, zlookup->sock, &length, &marker, &version,
  141. &vrf_id, &command);
  142. if (err < 0) {
  143. zlog_err("%s %s: zclient_read_header() failed",
  144. __FILE__, __PRETTY_FUNCTION__);
  145. zclient_lookup_failed(zlookup);
  146. return -1;
  147. }
  148. if (length < MIN_LEN) {
  149. zlog_err("%s %s: failure reading zclient lookup socket: len=%d < MIN_LEN=%d",
  150. __FILE__, __PRETTY_FUNCTION__, length, MIN_LEN);
  151. zclient_lookup_failed(zlookup);
  152. return -2;
  153. }
  154. if (command != ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB) {
  155. zlog_err("%s: socket %d command mismatch: %d",
  156. __func__, zlookup->sock, command);
  157. return -5;
  158. }
  159. raddr.s_addr = stream_get_ipv4(s);
  160. if (raddr.s_addr != addr.s_addr) {
  161. char addr_str[100];
  162. char raddr_str[100];
  163. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  164. pim_inet4_dump("<raddr?>", raddr, raddr_str, sizeof(raddr_str));
  165. zlog_warn("%s: address mismatch: addr=%s raddr=%s",
  166. __PRETTY_FUNCTION__,
  167. addr_str, raddr_str);
  168. /* warning only */
  169. }
  170. distance = stream_getc(s);
  171. metric = stream_getl(s);
  172. nexthop_num = stream_getc(s);
  173. if (nexthop_num < 1) {
  174. zlog_err("%s: socket %d bad nexthop_num=%d",
  175. __func__, zlookup->sock, nexthop_num);
  176. return -6;
  177. }
  178. length -= MIN_LEN;
  179. for (i = 0; i < nexthop_num; ++i) {
  180. enum nexthop_types_t nexthop_type;
  181. if (length < 1) {
  182. zlog_err("%s: socket %d empty input expecting nexthop_type: len=%d",
  183. __func__, zlookup->sock, length);
  184. return -7;
  185. }
  186. nexthop_type = stream_getc(s);
  187. --length;
  188. switch (nexthop_type) {
  189. case ZEBRA_NEXTHOP_IFINDEX:
  190. case ZEBRA_NEXTHOP_IFNAME:
  191. case ZEBRA_NEXTHOP_IPV4_IFINDEX:
  192. if (num_ifindex >= tab_size) {
  193. char addr_str[100];
  194. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  195. zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s",
  196. __FILE__, __PRETTY_FUNCTION__,
  197. (num_ifindex + 1), tab_size, addr_str);
  198. return num_ifindex;
  199. }
  200. if (nexthop_type == ZEBRA_NEXTHOP_IPV4_IFINDEX) {
  201. if (length < 4) {
  202. zlog_err("%s: socket %d short input expecting nexthop IPv4-addr: len=%d",
  203. __func__, zlookup->sock, length);
  204. return -8;
  205. }
  206. nexthop_tab[num_ifindex].nexthop_addr.s_addr = stream_get_ipv4(s);
  207. length -= 4;
  208. }
  209. else {
  210. nexthop_tab[num_ifindex].nexthop_addr.s_addr = PIM_NET_INADDR_ANY;
  211. }
  212. nexthop_tab[num_ifindex].ifindex = stream_getl(s);
  213. nexthop_tab[num_ifindex].protocol_distance = distance;
  214. nexthop_tab[num_ifindex].route_metric = metric;
  215. ++num_ifindex;
  216. break;
  217. case ZEBRA_NEXTHOP_IPV4:
  218. if (num_ifindex >= tab_size) {
  219. char addr_str[100];
  220. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  221. zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s",
  222. __FILE__, __PRETTY_FUNCTION__,
  223. (num_ifindex + 1), tab_size, addr_str);
  224. return num_ifindex;
  225. }
  226. nexthop_tab[num_ifindex].nexthop_addr.s_addr = stream_get_ipv4(s);
  227. length -= 4;
  228. nexthop_tab[num_ifindex].ifindex = 0;
  229. nexthop_tab[num_ifindex].protocol_distance = distance;
  230. nexthop_tab[num_ifindex].route_metric = metric;
  231. if (PIM_DEBUG_ZEBRA) {
  232. char addr_str[100];
  233. char nexthop_str[100];
  234. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  235. pim_inet4_dump("<nexthop?>", nexthop_tab[num_ifindex].nexthop_addr, nexthop_str, sizeof(nexthop_str));
  236. zlog_debug("%s %s: zebra returned recursive nexthop %s for address %s",
  237. __FILE__, __PRETTY_FUNCTION__,
  238. nexthop_str, addr_str);
  239. }
  240. ++num_ifindex;
  241. break;
  242. default:
  243. /* do nothing */
  244. {
  245. char addr_str[100];
  246. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  247. zlog_warn("%s %s: found non-ifindex nexthop type=%d for address %s",
  248. __FILE__, __PRETTY_FUNCTION__,
  249. nexthop_type, addr_str);
  250. }
  251. break;
  252. }
  253. }
  254. return num_ifindex;
  255. }
  256. static int zclient_lookup_nexthop_once(struct zclient *zlookup,
  257. struct pim_zlookup_nexthop nexthop_tab[],
  258. const int tab_size,
  259. struct in_addr addr)
  260. {
  261. struct stream *s;
  262. int ret;
  263. if (PIM_DEBUG_ZEBRA) {
  264. char addr_str[100];
  265. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  266. zlog_debug("%s: addr=%s",
  267. __PRETTY_FUNCTION__,
  268. addr_str);
  269. }
  270. /* Check socket. */
  271. if (zlookup->sock < 0) {
  272. zlog_err("%s %s: zclient lookup socket is not connected",
  273. __FILE__, __PRETTY_FUNCTION__);
  274. zclient_lookup_failed(zlookup);
  275. return -1;
  276. }
  277. s = zlookup->obuf;
  278. stream_reset(s);
  279. zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, VRF_DEFAULT);
  280. stream_put_in_addr(s, &addr);
  281. stream_putw_at(s, 0, stream_get_endp(s));
  282. ret = writen(zlookup->sock, s->data, stream_get_endp(s));
  283. if (ret < 0) {
  284. zlog_err("%s %s: writen() failure writing to zclient lookup socket",
  285. __FILE__, __PRETTY_FUNCTION__);
  286. zclient_lookup_failed(zlookup);
  287. return -2;
  288. }
  289. if (ret == 0) {
  290. zlog_err("%s %s: connection closed on zclient lookup socket",
  291. __FILE__, __PRETTY_FUNCTION__);
  292. zclient_lookup_failed(zlookup);
  293. return -3;
  294. }
  295. return zclient_read_nexthop(zlookup, nexthop_tab,
  296. tab_size, addr);
  297. }
  298. int zclient_lookup_nexthop(struct zclient *zlookup,
  299. struct pim_zlookup_nexthop nexthop_tab[],
  300. const int tab_size,
  301. struct in_addr addr,
  302. int max_lookup)
  303. {
  304. int lookup;
  305. uint32_t route_metric = 0xFFFFFFFF;
  306. uint8_t protocol_distance = 0xFF;
  307. for (lookup = 0; lookup < max_lookup; ++lookup) {
  308. int num_ifindex;
  309. int first_ifindex;
  310. struct in_addr nexthop_addr;
  311. num_ifindex = zclient_lookup_nexthop_once(qpim_zclient_lookup, nexthop_tab,
  312. PIM_NEXTHOP_IFINDEX_TAB_SIZE, addr);
  313. if ((num_ifindex < 1) && PIM_DEBUG_ZEBRA) {
  314. char addr_str[100];
  315. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  316. zlog_warn("%s %s: lookup=%d/%d: could not find nexthop ifindex for address %s",
  317. __FILE__, __PRETTY_FUNCTION__,
  318. lookup, max_lookup, addr_str);
  319. return -1;
  320. }
  321. if (lookup < 1) {
  322. /* this is the non-recursive lookup - save original metric/distance */
  323. route_metric = nexthop_tab[0].route_metric;
  324. protocol_distance = nexthop_tab[0].protocol_distance;
  325. }
  326. /*
  327. FIXME: Non-recursive nexthop ensured only for first ifindex.
  328. However, recursive route lookup should really be fixed in zebra daemon.
  329. See also TODO T24.
  330. */
  331. first_ifindex = nexthop_tab[0].ifindex;
  332. nexthop_addr = nexthop_tab[0].nexthop_addr;
  333. if (first_ifindex > 0) {
  334. /* found: first ifindex is non-recursive nexthop */
  335. if ((lookup > 0) && PIM_DEBUG_ZEBRA) {
  336. /* Report non-recursive success after first lookup */
  337. char addr_str[100];
  338. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  339. zlog_debug("%s %s: lookup=%d/%d: found non-recursive ifindex=%d for address %s dist=%d met=%d",
  340. __FILE__, __PRETTY_FUNCTION__,
  341. lookup, max_lookup, first_ifindex, addr_str,
  342. nexthop_tab[0].protocol_distance,
  343. nexthop_tab[0].route_metric);
  344. /* use last address as nexthop address */
  345. nexthop_tab[0].nexthop_addr = addr;
  346. /* report original route metric/distance */
  347. nexthop_tab[0].route_metric = route_metric;
  348. nexthop_tab[0].protocol_distance = protocol_distance;
  349. }
  350. return num_ifindex;
  351. }
  352. if (PIM_DEBUG_ZEBRA) {
  353. char addr_str[100];
  354. char nexthop_str[100];
  355. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  356. pim_inet4_dump("<nexthop?>", nexthop_addr, nexthop_str, sizeof(nexthop_str));
  357. zlog_debug("%s %s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s dist=%d met=%d",
  358. __FILE__, __PRETTY_FUNCTION__,
  359. lookup, max_lookup, nexthop_str, addr_str,
  360. nexthop_tab[0].protocol_distance,
  361. nexthop_tab[0].route_metric);
  362. }
  363. addr = nexthop_addr; /* use nexthop addr for recursive lookup */
  364. } /* for (max_lookup) */
  365. if (PIM_DEBUG_ZEBRA) {
  366. char addr_str[100];
  367. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  368. zlog_warn("%s %s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s",
  369. __FILE__, __PRETTY_FUNCTION__,
  370. lookup, max_lookup, addr_str);
  371. }
  372. return -2;
  373. }