zebra_fpm_netlink.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 && !rib)
  219. goto skip;
  220. if (rib)
  221. ri->rtm_protocol = netlink_proto_from_route_type (rib->type);
  222. if ((rib->flags & ZEBRA_FLAG_BLACKHOLE) || (rib->flags & ZEBRA_FLAG_REJECT))
  223. discard = 1;
  224. else
  225. discard = 0;
  226. if (cmd == RTM_NEWROUTE)
  227. {
  228. if (discard)
  229. {
  230. if (rib->flags & ZEBRA_FLAG_BLACKHOLE)
  231. ri->rtm_type = RTN_BLACKHOLE;
  232. else if (rib->flags & ZEBRA_FLAG_REJECT)
  233. ri->rtm_type = RTN_UNREACHABLE;
  234. else
  235. assert (0);
  236. }
  237. else
  238. ri->rtm_type = RTN_UNICAST;
  239. }
  240. ri->metric = &rib->metric;
  241. if (discard)
  242. {
  243. goto skip;
  244. }
  245. for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
  246. {
  247. if (ri->num_nhs >= MULTIPATH_NUM)
  248. break;
  249. if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
  250. continue;
  251. if ((cmd == RTM_NEWROUTE
  252. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
  253. || (cmd == RTM_DELROUTE
  254. && CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)))
  255. {
  256. netlink_route_info_add_nh (ri, nexthop, recursing);
  257. }
  258. }
  259. /* If there is no useful nexthop then return. */
  260. if (ri->num_nhs == 0)
  261. {
  262. zfpm_debug ("netlink_encode_route(): No useful nexthop.");
  263. return 0;
  264. }
  265. skip:
  266. return 1;
  267. }
  268. /*
  269. * netlink_route_info_encode
  270. *
  271. * Returns the number of bytes written to the buffer. 0 or a negative
  272. * value indicates an error.
  273. */
  274. static int
  275. netlink_route_info_encode (netlink_route_info_t *ri, char *in_buf,
  276. size_t in_buf_len)
  277. {
  278. size_t bytelen;
  279. int nexthop_num = 0;
  280. size_t buf_offset;
  281. netlink_nh_info_t *nhi;
  282. struct
  283. {
  284. struct nlmsghdr n;
  285. struct rtmsg r;
  286. char buf[1];
  287. } *req;
  288. req = (void *) in_buf;
  289. buf_offset = ((char *) req->buf) - ((char *) req);
  290. if (in_buf_len < buf_offset) {
  291. assert(0);
  292. return 0;
  293. }
  294. memset (req, 0, buf_offset);
  295. bytelen = af_addr_size (ri->af);
  296. req->n.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
  297. req->n.nlmsg_flags = NLM_F_CREATE | NLM_F_REQUEST;
  298. req->n.nlmsg_type = ri->nlmsg_type;
  299. req->r.rtm_family = ri->af;
  300. req->r.rtm_table = ri->rtm_table;
  301. req->r.rtm_dst_len = ri->prefix->prefixlen;
  302. req->r.rtm_protocol = ri->rtm_protocol;
  303. req->r.rtm_scope = RT_SCOPE_UNIVERSE;
  304. addattr_l (&req->n, in_buf_len, RTA_DST, &ri->prefix->u.prefix, bytelen);
  305. req->r.rtm_type = ri->rtm_type;
  306. /* Metric. */
  307. if (ri->metric)
  308. addattr32 (&req->n, in_buf_len, RTA_PRIORITY, *ri->metric);
  309. if (ri->num_nhs == 0)
  310. goto done;
  311. if (ri->num_nhs == 1)
  312. {
  313. nhi = &ri->nhs[0];
  314. if (nhi->gateway)
  315. {
  316. addattr_l (&req->n, in_buf_len, RTA_GATEWAY, nhi->gateway,
  317. bytelen);
  318. }
  319. if (nhi->if_index)
  320. {
  321. addattr32 (&req->n, in_buf_len, RTA_OIF, nhi->if_index);
  322. }
  323. goto done;
  324. }
  325. /*
  326. * Multipath case.
  327. */
  328. char buf[NL_PKT_BUF_SIZE];
  329. struct rtattr *rta = (void *) buf;
  330. struct rtnexthop *rtnh;
  331. rta->rta_type = RTA_MULTIPATH;
  332. rta->rta_len = RTA_LENGTH (0);
  333. rtnh = RTA_DATA (rta);
  334. for (nexthop_num = 0; nexthop_num < ri->num_nhs; nexthop_num++)
  335. {
  336. nhi = &ri->nhs[nexthop_num];
  337. rtnh->rtnh_len = sizeof (*rtnh);
  338. rtnh->rtnh_flags = 0;
  339. rtnh->rtnh_hops = 0;
  340. rtnh->rtnh_ifindex = 0;
  341. rta->rta_len += rtnh->rtnh_len;
  342. if (nhi->gateway)
  343. {
  344. rta_addattr_l (rta, sizeof (buf), RTA_GATEWAY, nhi->gateway, bytelen);
  345. rtnh->rtnh_len += sizeof (struct rtattr) + bytelen;
  346. }
  347. if (nhi->if_index)
  348. {
  349. rtnh->rtnh_ifindex = nhi->if_index;
  350. }
  351. rtnh = RTNH_NEXT (rtnh);
  352. }
  353. assert (rta->rta_len > RTA_LENGTH (0));
  354. addattr_l (&req->n, in_buf_len, RTA_MULTIPATH, RTA_DATA (rta),
  355. RTA_PAYLOAD (rta));
  356. done:
  357. if (ri->pref_src)
  358. {
  359. addattr_l (&req->n, in_buf_len, RTA_PREFSRC, &ri->pref_src, bytelen);
  360. }
  361. assert (req->n.nlmsg_len < in_buf_len);
  362. return req->n.nlmsg_len;
  363. }
  364. /*
  365. * zfpm_log_route_info
  366. *
  367. * Helper function to log the information in a route_info structure.
  368. */
  369. static void
  370. zfpm_log_route_info (netlink_route_info_t *ri, const char *label)
  371. {
  372. netlink_nh_info_t *nhi;
  373. int i;
  374. zfpm_debug ("%s : %s %s/%d, Proto: %s, Metric: %u", label,
  375. nl_msg_type_to_str (ri->nlmsg_type),
  376. prefix_addr_to_a (ri->prefix), ri->prefix->prefixlen,
  377. nl_rtproto_to_str (ri->rtm_protocol),
  378. ri->metric ? *ri->metric : 0);
  379. for (i = 0; i < ri->num_nhs; i++)
  380. {
  381. nhi = &ri->nhs[i];
  382. zfpm_debug(" Intf: %u, Gateway: %s, Recursive: %s, Type: %s",
  383. nhi->if_index, addr_to_a (ri->af, nhi->gateway),
  384. nhi->recursive ? "yes" : "no",
  385. nexthop_type_to_str (nhi->type));
  386. }
  387. }
  388. /*
  389. * zfpm_netlink_encode_route
  390. *
  391. * Create a netlink message corresponding to the given route in the
  392. * given buffer space.
  393. *
  394. * Returns the number of bytes written to the buffer. 0 or a negative
  395. * value indicates an error.
  396. */
  397. int
  398. zfpm_netlink_encode_route (int cmd, rib_dest_t *dest, struct rib *rib,
  399. char *in_buf, size_t in_buf_len)
  400. {
  401. netlink_route_info_t ri_space, *ri;
  402. ri = &ri_space;
  403. if (!netlink_route_info_fill (ri, cmd, dest, rib))
  404. return 0;
  405. zfpm_log_route_info (ri, __FUNCTION__);
  406. return netlink_route_info_encode (ri, in_buf, in_buf_len);
  407. }