zebra_fpm_netlink.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * Code for encoding/decoding FPM messages that are in netlink format.
  3. *
  4. * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
  5. * Copyright (C) 2012 by Open Source Routing.
  6. * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
  7. *
  8. * This file is part of GNU Zebra.
  9. *
  10. * GNU Zebra is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2, or (at your option) any
  13. * later version.
  14. *
  15. * GNU Zebra is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  22. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  23. * 02111-1307, USA.
  24. */
  25. #include <zebra.h>
  26. #include "log.h"
  27. #include "rib.h"
  28. #include "rt_netlink.h"
  29. #include "nexthop.h"
  30. #include "zebra_fpm_private.h"
  31. /*
  32. * addr_to_a
  33. *
  34. * Returns string representation of an address of the given AF.
  35. */
  36. static inline const char *
  37. addr_to_a (u_char af, void *addr)
  38. {
  39. if (!addr)
  40. return "<No address>";
  41. switch (af)
  42. {
  43. case AF_INET:
  44. return inet_ntoa (*((struct in_addr *) addr));
  45. #ifdef HAVE_IPV6
  46. case AF_INET6:
  47. return inet6_ntoa (*((struct in6_addr *) addr));
  48. #endif
  49. default:
  50. return "<Addr in unknown AF>";
  51. }
  52. }
  53. /*
  54. * prefix_addr_to_a
  55. *
  56. * Convience wrapper that returns a human-readable string for the
  57. * address in a prefix.
  58. */
  59. static const char *
  60. prefix_addr_to_a (struct prefix *prefix)
  61. {
  62. if (!prefix)
  63. return "<No address>";
  64. return addr_to_a (prefix->family, &prefix->u.prefix);
  65. }
  66. /*
  67. * af_addr_size
  68. *
  69. * The size of an address in a given address family.
  70. */
  71. static size_t
  72. af_addr_size (u_char af)
  73. {
  74. switch (af)
  75. {
  76. case AF_INET:
  77. return 4;
  78. #ifdef HAVE_IPV6
  79. case AF_INET6:
  80. return 16;
  81. #endif
  82. default:
  83. assert(0);
  84. return 16;
  85. }
  86. }
  87. /*
  88. * netlink_nh_info_t
  89. *
  90. * Holds information about a single nexthop for netlink. These info
  91. * structures are transient and may contain pointers into rib
  92. * data structures for convenience.
  93. */
  94. typedef struct netlink_nh_info_t_
  95. {
  96. uint32_t if_index;
  97. union g_addr *gateway;
  98. /*
  99. * Information from the struct nexthop from which this nh was
  100. * derived. For debug purposes only.
  101. */
  102. int recursive;
  103. enum nexthop_types_t type;
  104. } netlink_nh_info_t;
  105. /*
  106. * netlink_route_info_t
  107. *
  108. * A structure for holding information for a netlink route message.
  109. */
  110. typedef struct netlink_route_info_t_
  111. {
  112. uint16_t nlmsg_type;
  113. u_char rtm_type;
  114. uint32_t rtm_table;
  115. u_char rtm_protocol;
  116. u_char af;
  117. struct prefix *prefix;
  118. uint32_t *metric;
  119. int num_nhs;
  120. /*
  121. * Nexthop structures
  122. */
  123. netlink_nh_info_t nhs[MULTIPATH_NUM];
  124. union g_addr *pref_src;
  125. } netlink_route_info_t;
  126. /*
  127. * netlink_route_info_add_nh
  128. *
  129. * Add information about the given nexthop to the given route info
  130. * structure.
  131. *
  132. * Returns TRUE if a nexthop was added, FALSE otherwise.
  133. */
  134. static int
  135. netlink_route_info_add_nh (netlink_route_info_t *ri, struct nexthop *nexthop,
  136. int recursive)
  137. {
  138. netlink_nh_info_t nhi;
  139. union g_addr *src;
  140. memset (&nhi, 0, sizeof (nhi));
  141. src = NULL;
  142. if (ri->num_nhs >= (int) ZEBRA_NUM_OF (ri->nhs))
  143. return 0;
  144. nhi.recursive = recursive;
  145. nhi.type = nexthop->type;
  146. nhi.if_index = nexthop->ifindex;
  147. if (nexthop->type == NEXTHOP_TYPE_IPV4
  148. || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX)
  149. {
  150. nhi.gateway = &nexthop->gate;
  151. if (nexthop->src.ipv4.s_addr)
  152. src = &nexthop->src;
  153. }
  154. #ifdef HAVE_IPV6
  155. if (nexthop->type == NEXTHOP_TYPE_IPV6
  156. || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
  157. || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX)
  158. {
  159. nhi.gateway = &nexthop->gate;
  160. }
  161. #endif /* HAVE_IPV6 */
  162. if (nexthop->type == NEXTHOP_TYPE_IFINDEX
  163. || nexthop->type == NEXTHOP_TYPE_IFNAME)
  164. {
  165. if (nexthop->src.ipv4.s_addr)
  166. src = &nexthop->src;
  167. }
  168. if (!nhi.gateway && nhi.if_index == 0)
  169. return 0;
  170. /*
  171. * We have a valid nhi. Copy the structure over to the route_info.
  172. */
  173. ri->nhs[ri->num_nhs] = nhi;
  174. ri->num_nhs++;
  175. if (src && !ri->pref_src)
  176. ri->pref_src = src;
  177. return 1;
  178. }
  179. /*
  180. * netlink_proto_from_route_type
  181. */
  182. static u_char
  183. netlink_proto_from_route_type (int type)
  184. {
  185. switch (type)
  186. {
  187. case ZEBRA_ROUTE_KERNEL:
  188. case ZEBRA_ROUTE_CONNECT:
  189. return RTPROT_KERNEL;
  190. default:
  191. return RTPROT_ZEBRA;
  192. }
  193. }
  194. /*
  195. * netlink_route_info_fill
  196. *
  197. * Fill out the route information object from the given route.
  198. *
  199. * Returns TRUE on success and FALSE on failure.
  200. */
  201. static int
  202. netlink_route_info_fill (netlink_route_info_t *ri, int cmd,
  203. rib_dest_t *dest, struct rib *rib)
  204. {
  205. struct nexthop *nexthop, *tnexthop;
  206. int recursing;
  207. int discard;
  208. memset (ri, 0, sizeof (*ri));
  209. ri->prefix = rib_dest_prefix (dest);
  210. ri->af = rib_dest_af (dest);
  211. ri->nlmsg_type = cmd;
  212. ri->rtm_table = rib_dest_vrf (dest)->vrf_id;
  213. ri->rtm_protocol = RTPROT_UNSPEC;
  214. /*
  215. * An RTM_DELROUTE need not be accompanied by any nexthops,
  216. * particularly in our communication with the FPM.
  217. */
  218. if (cmd == RTM_DELROUTE)
  219. goto skip;
  220. if (!rib)
  221. {
  222. zlog_err("netlink_route_info_fill RTM_ADDROUTE called without rib info");
  223. return 0;
  224. }
  225. ri->rtm_protocol = netlink_proto_from_route_type (rib->type);
  226. if ((rib->flags & ZEBRA_FLAG_BLACKHOLE) || (rib->flags & ZEBRA_FLAG_REJECT))
  227. discard = 1;
  228. else
  229. discard = 0;
  230. if (cmd == RTM_NEWROUTE)
  231. {
  232. if (discard)
  233. {
  234. if (rib->flags & ZEBRA_FLAG_BLACKHOLE)
  235. ri->rtm_type = RTN_BLACKHOLE;
  236. else if (rib->flags & ZEBRA_FLAG_REJECT)
  237. ri->rtm_type = RTN_UNREACHABLE;
  238. else
  239. assert (0);
  240. }
  241. else
  242. ri->rtm_type = RTN_UNICAST;
  243. }
  244. ri->metric = &rib->metric;
  245. if (discard)
  246. {
  247. goto skip;
  248. }
  249. for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
  250. {
  251. if (ri->num_nhs >= MULTIPATH_NUM)
  252. break;
  253. if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
  254. continue;
  255. if ((cmd == RTM_NEWROUTE
  256. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  257. || (cmd == RTM_DELROUTE
  258. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
  259. {
  260. netlink_route_info_add_nh (ri, nexthop, recursing);
  261. }
  262. }
  263. /* If there is no useful nexthop then return. */
  264. if (ri->num_nhs == 0)
  265. {
  266. zfpm_debug ("netlink_encode_route(): No useful nexthop.");
  267. return 0;
  268. }
  269. skip:
  270. return 1;
  271. }
  272. /*
  273. * netlink_route_info_encode
  274. *
  275. * Returns the number of bytes written to the buffer. 0 or a negative
  276. * value indicates an error.
  277. */
  278. static int
  279. netlink_route_info_encode (netlink_route_info_t *ri, char *in_buf,
  280. size_t in_buf_len)
  281. {
  282. size_t bytelen;
  283. int nexthop_num = 0;
  284. size_t buf_offset;
  285. netlink_nh_info_t *nhi;
  286. struct
  287. {
  288. struct nlmsghdr n;
  289. struct rtmsg r;
  290. char buf[1];
  291. } *req;
  292. req = (void *) in_buf;
  293. buf_offset = ((char *) req->buf) - ((char *) req);
  294. if (in_buf_len < buf_offset) {
  295. assert(0);
  296. return 0;
  297. }
  298. memset (req, 0, buf_offset);
  299. bytelen = af_addr_size (ri->af);
  300. req->n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
  301. req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
  302. req->n.nlmsg_type = ri->nlmsg_type;
  303. req->r.rtm_family = ri->af;
  304. req->r.rtm_table = ri->rtm_table;
  305. req->r.rtm_dst_len = ri->prefix->prefixlen;
  306. req->r.rtm_protocol = ri->rtm_protocol;
  307. req->r.rtm_scope = RT_SCOPE_UNIVERSE;
  308. addattr_l (&req->n, in_buf_len, RTA_DST, &ri->prefix->u.prefix, bytelen);
  309. req->r.rtm_type = ri->rtm_type;
  310. /* Metric. */
  311. if (ri->metric)
  312. addattr32 (&req->n, in_buf_len, RTA_PRIORITY, *ri->metric);
  313. if (ri->num_nhs == 0)
  314. goto done;
  315. if (ri->num_nhs == 1)
  316. {
  317. nhi = &ri->nhs[0];
  318. if (nhi->gateway)
  319. {
  320. addattr_l (&req->n, in_buf_len, RTA_GATEWAY, nhi->gateway,
  321. bytelen);
  322. }
  323. if (nhi->if_index)
  324. {
  325. addattr32 (&req->n, in_buf_len, RTA_OIF, nhi->if_index);
  326. }
  327. goto done;
  328. }
  329. /*
  330. * Multipath case.
  331. */
  332. char buf[NL_PKT_BUF_SIZE];
  333. struct rtattr *rta = (void *) buf;
  334. struct rtnexthop *rtnh;
  335. rta->rta_type = RTA_MULTIPATH;
  336. rta->rta_len = RTA_LENGTH (0);
  337. rtnh = RTA_DATA (rta);
  338. for (nexthop_num = 0; nexthop_num < ri->num_nhs; nexthop_num++)
  339. {
  340. nhi = &ri->nhs[nexthop_num];
  341. rtnh->rtnh_len = sizeof (*rtnh);
  342. rtnh->rtnh_flags = 0;
  343. rtnh->rtnh_hops = 0;
  344. rtnh->rtnh_ifindex = 0;
  345. rta->rta_len += rtnh->rtnh_len;
  346. if (nhi->gateway)
  347. {
  348. rta_addattr_l (rta, sizeof (buf), RTA_GATEWAY, nhi->gateway, bytelen);
  349. rtnh->rtnh_len += sizeof (struct rtattr) + bytelen;
  350. }
  351. if (nhi->if_index)
  352. {
  353. rtnh->rtnh_ifindex = nhi->if_index;
  354. }
  355. rtnh = RTNH_NEXT (rtnh);
  356. }
  357. assert (rta->rta_len > RTA_LENGTH (0));
  358. addattr_l (&req->n, in_buf_len, RTA_MULTIPATH, RTA_DATA (rta),
  359. RTA_PAYLOAD (rta));
  360. done:
  361. if (ri->pref_src)
  362. {
  363. addattr_l (&req->n, in_buf_len, RTA_PREFSRC, &ri->pref_src, bytelen);
  364. }
  365. assert (req->n.nlmsg_len < in_buf_len);
  366. return req->n.nlmsg_len;
  367. }
  368. /*
  369. * zfpm_log_route_info
  370. *
  371. * Helper function to log the information in a route_info structure.
  372. */
  373. static void
  374. zfpm_log_route_info (netlink_route_info_t *ri, const char *label)
  375. {
  376. netlink_nh_info_t *nhi;
  377. int i;
  378. zfpm_debug ("%s : %s %s/%d, Proto: %s, Metric: %u", label,
  379. nl_msg_type_to_str (ri->nlmsg_type),
  380. prefix_addr_to_a (ri->prefix), ri->prefix->prefixlen,
  381. nl_rtproto_to_str (ri->rtm_protocol),
  382. ri->metric ? *ri->metric : 0);
  383. for (i = 0; i < ri->num_nhs; i++)
  384. {
  385. nhi = &ri->nhs[i];
  386. zfpm_debug(" Intf: %u, Gateway: %s, Recursive: %s, Type: %s",
  387. nhi->if_index, addr_to_a (ri->af, nhi->gateway),
  388. nhi->recursive ? "yes" : "no",
  389. nexthop_type_to_str (nhi->type));
  390. }
  391. }
  392. /*
  393. * zfpm_netlink_encode_route
  394. *
  395. * Create a netlink message corresponding to the given route in the
  396. * given buffer space.
  397. *
  398. * Returns the number of bytes written to the buffer. 0 or a negative
  399. * value indicates an error.
  400. */
  401. int
  402. zfpm_netlink_encode_route (int cmd, rib_dest_t *dest, struct rib *rib,
  403. char *in_buf, size_t in_buf_len)
  404. {
  405. netlink_route_info_t ri_space, *ri;
  406. ri = &ri_space;
  407. if (!netlink_route_info_fill (ri, cmd, dest, rib))
  408. return 0;
  409. zfpm_log_route_info (ri, __FUNCTION__);
  410. return netlink_route_info_encode (ri, in_buf, in_buf_len);
  411. }