pim_zlookup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. int nbytes;
  127. struct in_addr raddr;
  128. uint8_t distance;
  129. uint32_t metric;
  130. int nexthop_num;
  131. int i, err;
  132. if (PIM_DEBUG_ZEBRA) {
  133. char addr_str[100];
  134. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  135. zlog_debug("%s: addr=%s",
  136. __PRETTY_FUNCTION__,
  137. addr_str);
  138. }
  139. s = zlookup->ibuf;
  140. stream_reset(s);
  141. err = zclient_read_header (s, zlookup->sock, &length, &marker, &version,
  142. &vrf_id, &command);
  143. if (err < 0) {
  144. zlog_err("%s %s: zclient_read_header() failed",
  145. __FILE__, __PRETTY_FUNCTION__);
  146. zclient_lookup_failed(zlookup);
  147. return -1;
  148. }
  149. if (length < MIN_LEN) {
  150. zlog_err("%s %s: failure reading zclient lookup socket: len=%d < MIN_LEN=%d",
  151. __FILE__, __PRETTY_FUNCTION__, length, MIN_LEN);
  152. zclient_lookup_failed(zlookup);
  153. return -2;
  154. }
  155. nbytes = stream_read(s, zlookup->sock, length);
  156. if (nbytes < length) {
  157. zlog_err("%s %s: failure reading zclient lookup socket: nbytes=%d < len=%d",
  158. __FILE__, __PRETTY_FUNCTION__, nbytes, length);
  159. zclient_lookup_failed(zlookup);
  160. return -3;
  161. }
  162. if (command != ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB) {
  163. zlog_err("%s: socket %d command mismatch: %d",
  164. __func__, zlookup->sock, command);
  165. return -5;
  166. }
  167. raddr.s_addr = stream_get_ipv4(s);
  168. if (raddr.s_addr != addr.s_addr) {
  169. char addr_str[100];
  170. char raddr_str[100];
  171. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  172. pim_inet4_dump("<raddr?>", raddr, raddr_str, sizeof(raddr_str));
  173. zlog_warn("%s: address mismatch: addr=%s raddr=%s",
  174. __PRETTY_FUNCTION__,
  175. addr_str, raddr_str);
  176. /* warning only */
  177. }
  178. distance = stream_getc(s);
  179. metric = stream_getl(s);
  180. nexthop_num = stream_getc(s);
  181. if (nexthop_num < 1) {
  182. zlog_err("%s: socket %d bad nexthop_num=%d",
  183. __func__, zlookup->sock, nexthop_num);
  184. return -6;
  185. }
  186. length -= MIN_LEN;
  187. for (i = 0; i < nexthop_num; ++i) {
  188. enum nexthop_types_t nexthop_type;
  189. if (length < 1) {
  190. zlog_err("%s: socket %d empty input expecting nexthop_type: len=%d",
  191. __func__, zlookup->sock, length);
  192. return -7;
  193. }
  194. nexthop_type = stream_getc(s);
  195. --length;
  196. switch (nexthop_type) {
  197. case ZEBRA_NEXTHOP_IFINDEX:
  198. case ZEBRA_NEXTHOP_IFNAME:
  199. case ZEBRA_NEXTHOP_IPV4_IFINDEX:
  200. if (num_ifindex >= tab_size) {
  201. char addr_str[100];
  202. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  203. zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s",
  204. __FILE__, __PRETTY_FUNCTION__,
  205. (num_ifindex + 1), tab_size, addr_str);
  206. return num_ifindex;
  207. }
  208. if (nexthop_type == ZEBRA_NEXTHOP_IPV4_IFINDEX) {
  209. if (length < 4) {
  210. zlog_err("%s: socket %d short input expecting nexthop IPv4-addr: len=%d",
  211. __func__, zlookup->sock, length);
  212. return -8;
  213. }
  214. nexthop_tab[num_ifindex].nexthop_addr.s_addr = stream_get_ipv4(s);
  215. length -= 4;
  216. }
  217. else {
  218. nexthop_tab[num_ifindex].nexthop_addr.s_addr = PIM_NET_INADDR_ANY;
  219. }
  220. nexthop_tab[num_ifindex].ifindex = stream_getl(s);
  221. nexthop_tab[num_ifindex].protocol_distance = distance;
  222. nexthop_tab[num_ifindex].route_metric = metric;
  223. ++num_ifindex;
  224. break;
  225. case ZEBRA_NEXTHOP_IPV4:
  226. if (num_ifindex >= tab_size) {
  227. char addr_str[100];
  228. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  229. zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s",
  230. __FILE__, __PRETTY_FUNCTION__,
  231. (num_ifindex + 1), tab_size, addr_str);
  232. return num_ifindex;
  233. }
  234. nexthop_tab[num_ifindex].nexthop_addr.s_addr = stream_get_ipv4(s);
  235. length -= 4;
  236. nexthop_tab[num_ifindex].ifindex = 0;
  237. nexthop_tab[num_ifindex].protocol_distance = distance;
  238. nexthop_tab[num_ifindex].route_metric = metric;
  239. {
  240. char addr_str[100];
  241. char nexthop_str[100];
  242. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  243. pim_inet4_dump("<nexthop?>", nexthop_tab[num_ifindex].nexthop_addr, nexthop_str, sizeof(nexthop_str));
  244. zlog_warn("%s %s: zebra returned recursive nexthop %s for address %s",
  245. __FILE__, __PRETTY_FUNCTION__,
  246. nexthop_str, addr_str);
  247. }
  248. ++num_ifindex;
  249. break;
  250. default:
  251. /* do nothing */
  252. {
  253. char addr_str[100];
  254. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  255. zlog_warn("%s %s: found non-ifindex nexthop type=%d for address %s",
  256. __FILE__, __PRETTY_FUNCTION__,
  257. nexthop_type, addr_str);
  258. }
  259. break;
  260. }
  261. }
  262. return num_ifindex;
  263. }
  264. static int zclient_lookup_nexthop_once(struct zclient *zlookup,
  265. struct pim_zlookup_nexthop nexthop_tab[],
  266. const int tab_size,
  267. struct in_addr addr)
  268. {
  269. struct stream *s;
  270. int ret;
  271. if (PIM_DEBUG_ZEBRA) {
  272. char addr_str[100];
  273. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  274. zlog_debug("%s: addr=%s",
  275. __PRETTY_FUNCTION__,
  276. addr_str);
  277. }
  278. /* Check socket. */
  279. if (zlookup->sock < 0) {
  280. zlog_err("%s %s: zclient lookup socket is not connected",
  281. __FILE__, __PRETTY_FUNCTION__);
  282. zclient_lookup_failed(zlookup);
  283. return -1;
  284. }
  285. s = zlookup->obuf;
  286. stream_reset(s);
  287. zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, VRF_DEFAULT);
  288. stream_put_in_addr(s, &addr);
  289. stream_putw_at(s, 0, stream_get_endp(s));
  290. ret = writen(zlookup->sock, s->data, stream_get_endp(s));
  291. if (ret < 0) {
  292. zlog_err("%s %s: writen() failure writing to zclient lookup socket",
  293. __FILE__, __PRETTY_FUNCTION__);
  294. zclient_lookup_failed(zlookup);
  295. return -2;
  296. }
  297. if (ret == 0) {
  298. zlog_err("%s %s: connection closed on zclient lookup socket",
  299. __FILE__, __PRETTY_FUNCTION__);
  300. zclient_lookup_failed(zlookup);
  301. return -3;
  302. }
  303. return zclient_read_nexthop(zlookup, nexthop_tab,
  304. tab_size, addr);
  305. }
  306. int zclient_lookup_nexthop(struct zclient *zlookup,
  307. struct pim_zlookup_nexthop nexthop_tab[],
  308. const int tab_size,
  309. struct in_addr addr,
  310. int max_lookup)
  311. {
  312. int lookup;
  313. uint32_t route_metric = 0xFFFFFFFF;
  314. uint8_t protocol_distance = 0xFF;
  315. for (lookup = 0; lookup < max_lookup; ++lookup) {
  316. int num_ifindex;
  317. int first_ifindex;
  318. struct in_addr nexthop_addr;
  319. num_ifindex = zclient_lookup_nexthop_once(qpim_zclient_lookup, nexthop_tab,
  320. PIM_NEXTHOP_IFINDEX_TAB_SIZE, addr);
  321. if (num_ifindex < 1) {
  322. char addr_str[100];
  323. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  324. zlog_warn("%s %s: lookup=%d/%d: could not find nexthop ifindex for address %s",
  325. __FILE__, __PRETTY_FUNCTION__,
  326. lookup, max_lookup, addr_str);
  327. return -1;
  328. }
  329. if (lookup < 1) {
  330. /* this is the non-recursive lookup - save original metric/distance */
  331. route_metric = nexthop_tab[0].route_metric;
  332. protocol_distance = nexthop_tab[0].protocol_distance;
  333. }
  334. /*
  335. FIXME: Non-recursive nexthop ensured only for first ifindex.
  336. However, recursive route lookup should really be fixed in zebra daemon.
  337. See also TODO T24.
  338. */
  339. first_ifindex = nexthop_tab[0].ifindex;
  340. nexthop_addr = nexthop_tab[0].nexthop_addr;
  341. if (first_ifindex > 0) {
  342. /* found: first ifindex is non-recursive nexthop */
  343. if (lookup > 0) {
  344. /* Report non-recursive success after first lookup */
  345. char addr_str[100];
  346. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  347. zlog_info("%s %s: lookup=%d/%d: found non-recursive ifindex=%d for address %s dist=%d met=%d",
  348. __FILE__, __PRETTY_FUNCTION__,
  349. lookup, max_lookup, first_ifindex, addr_str,
  350. nexthop_tab[0].protocol_distance,
  351. nexthop_tab[0].route_metric);
  352. /* use last address as nexthop address */
  353. nexthop_tab[0].nexthop_addr = addr;
  354. /* report original route metric/distance */
  355. nexthop_tab[0].route_metric = route_metric;
  356. nexthop_tab[0].protocol_distance = protocol_distance;
  357. }
  358. return num_ifindex;
  359. }
  360. {
  361. char addr_str[100];
  362. char nexthop_str[100];
  363. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  364. pim_inet4_dump("<nexthop?>", nexthop_addr, nexthop_str, sizeof(nexthop_str));
  365. zlog_warn("%s %s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s dist=%d met=%d",
  366. __FILE__, __PRETTY_FUNCTION__,
  367. lookup, max_lookup, nexthop_str, addr_str,
  368. nexthop_tab[0].protocol_distance,
  369. nexthop_tab[0].route_metric);
  370. }
  371. addr = nexthop_addr; /* use nexthop addr for recursive lookup */
  372. } /* for (max_lookup) */
  373. char addr_str[100];
  374. pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
  375. zlog_warn("%s %s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s",
  376. __FILE__, __PRETTY_FUNCTION__,
  377. lookup, max_lookup, addr_str);
  378. return -2;
  379. }