route_obj.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /*
  2. * lib/route/route_obj.c Route Object
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation version 2.1
  7. * of the License.
  8. *
  9. * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
  10. */
  11. /**
  12. * @ingroup route
  13. * @defgroup route_obj Route Object
  14. *
  15. * @par Attributes
  16. * @code
  17. * Name Default
  18. * -------------------------------------------------------------
  19. * routing table RT_TABLE_MAIN
  20. * scope RT_SCOPE_NOWHERE
  21. * tos 0
  22. * realms 0
  23. * protocol RTPROT_STATIC
  24. * prio 0
  25. * family AF_UNSPEC
  26. * type RTN_UNICAST
  27. * oif RTNL_LINK_NOT_FOUND
  28. * iif NULL
  29. * mpalgo IP_MP_ALG_NONE
  30. * @endcode
  31. *
  32. * @{
  33. */
  34. #include <netlink-local.h>
  35. #include <netlink/netlink.h>
  36. #include <netlink/cache.h>
  37. #include <netlink/utils.h>
  38. #include <netlink/data.h>
  39. #include <netlink/route/rtnl.h>
  40. #include <netlink/route/route.h>
  41. #include <netlink/route/link.h>
  42. /** @cond SKIP */
  43. #define ROUTE_ATTR_FAMILY 0x000001
  44. #define ROUTE_ATTR_TOS 0x000002
  45. #define ROUTE_ATTR_TABLE 0x000004
  46. #define ROUTE_ATTR_PROTOCOL 0x000008
  47. #define ROUTE_ATTR_SCOPE 0x000010
  48. #define ROUTE_ATTR_TYPE 0x000020
  49. #define ROUTE_ATTR_FLAGS 0x000040
  50. #define ROUTE_ATTR_DST 0x000080
  51. #define ROUTE_ATTR_SRC 0x000100
  52. #define ROUTE_ATTR_IIF 0x000200
  53. #define ROUTE_ATTR_OIF 0x000400
  54. #define ROUTE_ATTR_GATEWAY 0x000800
  55. #define ROUTE_ATTR_PRIO 0x001000
  56. #define ROUTE_ATTR_PREF_SRC 0x002000
  57. #define ROUTE_ATTR_METRICS 0x004000
  58. #define ROUTE_ATTR_MULTIPATH 0x008000
  59. #define ROUTE_ATTR_REALMS 0x010000
  60. #define ROUTE_ATTR_CACHEINFO 0x020000
  61. #define ROUTE_ATTR_MP_ALGO 0x040000
  62. /** @endcond */
  63. static int route_dump_brief(struct nl_object *a, struct nl_dump_params *p);
  64. static void route_constructor(struct nl_object *c)
  65. {
  66. struct rtnl_route *r = (struct rtnl_route *) c;
  67. nl_init_list_head(&r->rt_nexthops);
  68. }
  69. static void route_free_data(struct nl_object *c)
  70. {
  71. struct rtnl_route *r = (struct rtnl_route *) c;
  72. struct rtnl_nexthop *nh, *tmp;
  73. if (r == NULL)
  74. return;
  75. nl_addr_put(r->rt_dst);
  76. nl_addr_put(r->rt_src);
  77. nl_addr_put(r->rt_gateway);
  78. nl_addr_put(r->rt_pref_src);
  79. nl_list_for_each_entry_safe(nh, tmp, &r->rt_nexthops, rtnh_list) {
  80. rtnl_route_remove_nexthop(nh);
  81. rtnl_route_nh_free(nh);
  82. }
  83. }
  84. static int route_clone(struct nl_object *_dst, struct nl_object *_src)
  85. {
  86. struct rtnl_route *dst = (struct rtnl_route *) _dst;
  87. struct rtnl_route *src = (struct rtnl_route *) _src;
  88. struct rtnl_nexthop *nh, *new;
  89. if (src->rt_dst)
  90. if (!(dst->rt_dst = nl_addr_clone(src->rt_dst)))
  91. goto errout;
  92. if (src->rt_src)
  93. if (!(dst->rt_src = nl_addr_clone(src->rt_src)))
  94. goto errout;
  95. if (src->rt_gateway)
  96. if (!(dst->rt_gateway = nl_addr_clone(src->rt_gateway)))
  97. goto errout;
  98. if (src->rt_pref_src)
  99. if (!(dst->rt_pref_src = nl_addr_clone(src->rt_pref_src)))
  100. goto errout;
  101. nl_init_list_head(&dst->rt_nexthops);
  102. nl_list_for_each_entry(nh, &src->rt_nexthops, rtnh_list) {
  103. new = rtnl_route_nh_clone(nh);
  104. if (!new)
  105. goto errout;
  106. rtnl_route_add_nexthop(dst, new);
  107. }
  108. return 0;
  109. errout:
  110. return nl_get_errno();
  111. }
  112. static int route_dump_brief(struct nl_object *a, struct nl_dump_params *p)
  113. {
  114. struct rtnl_route *r = (struct rtnl_route *) a;
  115. struct nl_cache *link_cache;
  116. char buf[64];
  117. link_cache = nl_cache_mngt_require("route/link");
  118. if (!(r->ce_mask & ROUTE_ATTR_DST) ||
  119. nl_addr_get_len(r->rt_dst) == 0)
  120. dp_dump(p, "default ");
  121. else
  122. dp_dump(p, "%s ", nl_addr2str(r->rt_dst, buf, sizeof(buf)));
  123. if (r->ce_mask & ROUTE_ATTR_OIF) {
  124. if (link_cache)
  125. dp_dump(p, "dev %s ",
  126. rtnl_link_i2name(link_cache, r->rt_oif,
  127. buf, sizeof(buf)));
  128. else
  129. dp_dump(p, "dev %d ", r->rt_oif);
  130. }
  131. if (r->ce_mask & ROUTE_ATTR_GATEWAY)
  132. dp_dump(p, "via %s ", nl_addr2str(r->rt_gateway, buf,
  133. sizeof(buf)));
  134. else if (r->ce_mask & ROUTE_ATTR_MULTIPATH)
  135. dp_dump(p, "via nexthops ");
  136. if (r->ce_mask & ROUTE_ATTR_SCOPE)
  137. dp_dump(p, "scope %s ",
  138. rtnl_scope2str(r->rt_scope, buf, sizeof(buf)));
  139. if (r->ce_mask & ROUTE_ATTR_FLAGS && r->rt_flags) {
  140. int flags = r->rt_flags;
  141. dp_dump(p, "<");
  142. #define PRINT_FLAG(f) if (flags & RTNH_F_##f) { \
  143. flags &= ~RTNH_F_##f; dp_dump(p, #f "%s", flags ? "," : ""); }
  144. PRINT_FLAG(DEAD);
  145. PRINT_FLAG(ONLINK);
  146. PRINT_FLAG(PERVASIVE);
  147. #undef PRINT_FLAG
  148. #define PRINT_FLAG(f) if (flags & RTM_F_##f) { \
  149. flags &= ~RTM_F_##f; dp_dump(p, #f "%s", flags ? "," : ""); }
  150. PRINT_FLAG(NOTIFY);
  151. PRINT_FLAG(CLONED);
  152. PRINT_FLAG(EQUALIZE);
  153. PRINT_FLAG(PREFIX);
  154. #undef PRINT_FLAG
  155. dp_dump(p, ">");
  156. }
  157. dp_dump(p, "\n");
  158. return 1;
  159. }
  160. static int route_dump_full(struct nl_object *a, struct nl_dump_params *p)
  161. {
  162. struct rtnl_route *r = (struct rtnl_route *) a;
  163. struct nl_cache *link_cache;
  164. char buf[256];
  165. int i, line;
  166. link_cache = nl_cache_mngt_require("route/link");
  167. line = route_dump_brief(a, p);
  168. if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
  169. struct rtnl_nexthop *nh;
  170. nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
  171. dp_dump_line(p, line++, " via ");
  172. if (nh->rtnh_mask & NEXTHOP_HAS_GATEWAY)
  173. dp_dump(p, "%s ",
  174. nl_addr2str(nh->rtnh_gateway,
  175. buf, sizeof(buf)));
  176. if (link_cache) {
  177. dp_dump(p, "dev %s ",
  178. rtnl_link_i2name(link_cache,
  179. nh->rtnh_ifindex,
  180. buf, sizeof(buf)));
  181. } else
  182. dp_dump(p, "dev %d ", nh->rtnh_ifindex);
  183. dp_dump(p, "weight %u <%s>\n", nh->rtnh_weight,
  184. rtnl_route_nh_flags2str(nh->rtnh_flags,
  185. buf, sizeof(buf)));
  186. }
  187. }
  188. dp_dump_line(p, line++, " ");
  189. if (r->ce_mask & ROUTE_ATTR_PREF_SRC)
  190. dp_dump(p, "preferred-src %s ",
  191. nl_addr2str(r->rt_pref_src, buf, sizeof(buf)));
  192. if (r->ce_mask & ROUTE_ATTR_TABLE)
  193. dp_dump(p, "table %s ",
  194. rtnl_route_table2str(r->rt_table, buf, sizeof(buf)));
  195. if (r->ce_mask & ROUTE_ATTR_TYPE)
  196. dp_dump(p, "type %s ",
  197. nl_rtntype2str(r->rt_type, buf, sizeof(buf)));
  198. if (r->ce_mask & ROUTE_ATTR_PRIO)
  199. dp_dump(p, "metric %#x ", r->rt_prio);
  200. if (r->ce_mask & ROUTE_ATTR_FAMILY)
  201. dp_dump(p, "family %s ",
  202. nl_af2str(r->rt_family, buf, sizeof(buf)));
  203. if (r->ce_mask & ROUTE_ATTR_PROTOCOL)
  204. dp_dump(p, "protocol %s ",
  205. rtnl_route_proto2str(r->rt_protocol, buf, sizeof(buf)));
  206. dp_dump(p, "\n");
  207. if ((r->ce_mask & (ROUTE_ATTR_IIF | ROUTE_ATTR_SRC | ROUTE_ATTR_TOS |
  208. ROUTE_ATTR_REALMS)) ||
  209. ((r->ce_mask & ROUTE_ATTR_CACHEINFO) &&
  210. r->rt_cacheinfo.rtci_error)) {
  211. dp_dump_line(p, line++, " ");
  212. if (r->ce_mask & ROUTE_ATTR_IIF)
  213. dp_dump(p, "iif %s ", r->rt_iif);
  214. if (r->ce_mask & ROUTE_ATTR_SRC)
  215. dp_dump(p, "src %s ",
  216. nl_addr2str(r->rt_src, buf, sizeof(buf)));
  217. if (r->ce_mask & ROUTE_ATTR_TOS)
  218. dp_dump(p, "tos %#x ", r->rt_tos);
  219. if (r->ce_mask & ROUTE_ATTR_REALMS)
  220. dp_dump(p, "realm %04x:%04x ",
  221. RTNL_REALM_FROM(r->rt_realms),
  222. RTNL_REALM_TO(r->rt_realms));
  223. if ((r->ce_mask & ROUTE_ATTR_CACHEINFO) &&
  224. r->rt_cacheinfo.rtci_error)
  225. dp_dump(p, "error %d (%s) ", r->rt_cacheinfo.rtci_error,
  226. strerror_r(-r->rt_cacheinfo.rtci_error, buf, sizeof(buf)));
  227. dp_dump(p, "\n");
  228. }
  229. if (r->ce_mask & ROUTE_ATTR_METRICS) {
  230. dp_dump_line(p, line++, " ");
  231. for (i = 0; i < RTAX_MAX; i++)
  232. if (r->rt_metrics_mask & (1 << i))
  233. dp_dump(p, "%s %u ",
  234. rtnl_route_metric2str(i+1,
  235. buf, sizeof(buf)),
  236. r->rt_metrics[i]);
  237. dp_dump(p, "\n");
  238. }
  239. return line;
  240. }
  241. static int route_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
  242. {
  243. struct rtnl_route *route = (struct rtnl_route *) obj;
  244. int line;
  245. line = route_dump_full(obj, p);
  246. if (route->ce_mask & ROUTE_ATTR_CACHEINFO) {
  247. struct rtnl_rtcacheinfo *ci = &route->rt_cacheinfo;
  248. dp_dump_line(p, line++, " used %u refcnt %u ",
  249. ci->rtci_used, ci->rtci_clntref);
  250. dp_dump_line(p, line++, "last-use %us expires %us\n",
  251. ci->rtci_last_use / nl_get_hz(),
  252. ci->rtci_expires / nl_get_hz());
  253. }
  254. return line;
  255. }
  256. static int route_dump_xml(struct nl_object *obj, struct nl_dump_params *p)
  257. {
  258. struct rtnl_route *route = (struct rtnl_route *) obj;
  259. char buf[128];
  260. int line = 0;
  261. dp_dump_line(p, line++, "<route>\n");
  262. dp_dump_line(p, line++, " <family>%s</family>\n",
  263. nl_af2str(route->rt_family, buf, sizeof(buf)));
  264. if (route->ce_mask & ROUTE_ATTR_DST)
  265. dp_dump_line(p, line++, " <dst>%s</dst>\n",
  266. nl_addr2str(route->rt_dst, buf, sizeof(buf)));
  267. if (route->ce_mask & ROUTE_ATTR_SRC)
  268. dp_dump_line(p, line++, " <src>%s</src>\n",
  269. nl_addr2str(route->rt_src, buf, sizeof(buf)));
  270. if (route->ce_mask & ROUTE_ATTR_GATEWAY)
  271. dp_dump_line(p, line++, " <gateway>%s</gateway>\n",
  272. nl_addr2str(route->rt_gateway, buf, sizeof(buf)));
  273. if (route->ce_mask & ROUTE_ATTR_PREF_SRC)
  274. dp_dump_line(p, line++, " <prefsrc>%s</prefsrc>\n",
  275. nl_addr2str(route->rt_pref_src, buf, sizeof(buf)));
  276. if (route->ce_mask & ROUTE_ATTR_IIF)
  277. dp_dump_line(p, line++, " <iif>%s</iif>\n", route->rt_iif);
  278. if (route->ce_mask & ROUTE_ATTR_REALMS)
  279. dp_dump_line(p, line++, " <realms>%u</realms>\n",
  280. route->rt_realms);
  281. if (route->ce_mask & ROUTE_ATTR_TOS)
  282. dp_dump_line(p, line++, " <tos>%u</tos>\n", route->rt_tos);
  283. if (route->ce_mask & ROUTE_ATTR_TABLE)
  284. dp_dump_line(p, line++, " <table>%u</table>\n",
  285. route->rt_table);
  286. if (route->ce_mask & ROUTE_ATTR_SCOPE)
  287. dp_dump_line(p, line++, " <scope>%s</scope>\n",
  288. rtnl_scope2str(route->rt_scope, buf, sizeof(buf)));
  289. if (route->ce_mask & ROUTE_ATTR_PRIO)
  290. dp_dump_line(p, line++, " <metric>%u</metric>\n",
  291. route->rt_prio);
  292. if (route->ce_mask & ROUTE_ATTR_OIF) {
  293. struct nl_cache *link_cache;
  294. link_cache = nl_cache_mngt_require("route/link");
  295. if (link_cache)
  296. dp_dump_line(p, line++, " <oif>%s</oif>\n",
  297. rtnl_link_i2name(link_cache,
  298. route->rt_oif,
  299. buf, sizeof(buf)));
  300. else
  301. dp_dump_line(p, line++, " <oif>%u</oif>\n",
  302. route->rt_oif);
  303. }
  304. if (route->ce_mask & ROUTE_ATTR_TYPE)
  305. dp_dump_line(p, line++, " <type>%s</type>\n",
  306. nl_rtntype2str(route->rt_type, buf, sizeof(buf)));
  307. dp_dump_line(p, line++, "</route>\n");
  308. #if 0
  309. uint8_t rt_protocol;
  310. uint32_t rt_flags;
  311. uint32_t rt_metrics[RTAX_MAX];
  312. uint32_t rt_metrics_mask;
  313. struct rtnl_nexthop * rt_nexthops;
  314. struct rtnl_rtcacheinfo rt_cacheinfo;
  315. uint32_t rt_mp_algo;
  316. #endif
  317. return line;
  318. }
  319. static int route_dump_env(struct nl_object *obj, struct nl_dump_params *p)
  320. {
  321. struct rtnl_route *route = (struct rtnl_route *) obj;
  322. char buf[128];
  323. int line = 0;
  324. dp_dump_line(p, line++, "ROUTE_FAMILY=%s\n",
  325. nl_af2str(route->rt_family, buf, sizeof(buf)));
  326. if (route->ce_mask & ROUTE_ATTR_DST)
  327. dp_dump_line(p, line++, "ROUTE_DST=%s\n",
  328. nl_addr2str(route->rt_dst, buf, sizeof(buf)));
  329. if (route->ce_mask & ROUTE_ATTR_SRC)
  330. dp_dump_line(p, line++, "ROUTE_SRC=%s\n",
  331. nl_addr2str(route->rt_src, buf, sizeof(buf)));
  332. if (route->ce_mask & ROUTE_ATTR_GATEWAY)
  333. dp_dump_line(p, line++, "ROUTE_GATEWAY=%s\n",
  334. nl_addr2str(route->rt_gateway, buf, sizeof(buf)));
  335. if (route->ce_mask & ROUTE_ATTR_PREF_SRC)
  336. dp_dump_line(p, line++, "ROUTE_PREFSRC=%s\n",
  337. nl_addr2str(route->rt_pref_src, buf, sizeof(buf)));
  338. if (route->ce_mask & ROUTE_ATTR_IIF)
  339. dp_dump_line(p, line++, "ROUTE_IIF=%s\n", route->rt_iif);
  340. if (route->ce_mask & ROUTE_ATTR_REALMS)
  341. dp_dump_line(p, line++, "ROUTE_REALM=%u\n",
  342. route->rt_realms);
  343. if (route->ce_mask & ROUTE_ATTR_TOS)
  344. dp_dump_line(p, line++, "ROUTE_TOS=%u\n", route->rt_tos);
  345. if (route->ce_mask & ROUTE_ATTR_TABLE)
  346. dp_dump_line(p, line++, "ROUTE_TABLE=%u\n",
  347. route->rt_table);
  348. if (route->ce_mask & ROUTE_ATTR_SCOPE)
  349. dp_dump_line(p, line++, "ROUTE_SCOPE=%s\n",
  350. rtnl_scope2str(route->rt_scope, buf, sizeof(buf)));
  351. if (route->ce_mask & ROUTE_ATTR_PRIO)
  352. dp_dump_line(p, line++, "ROUTE_METRIC=%u\n",
  353. route->rt_prio);
  354. if (route->ce_mask & ROUTE_ATTR_OIF) {
  355. struct nl_cache *link_cache;
  356. dp_dump_line(p, line++, "ROUTE_OIF_IFINDEX=%u\n",
  357. route->rt_oif);
  358. link_cache = nl_cache_mngt_require("route/link");
  359. if (link_cache)
  360. dp_dump_line(p, line++, "ROUTE_OIF_IFNAME=%s\n",
  361. rtnl_link_i2name(link_cache,
  362. route->rt_oif,
  363. buf, sizeof(buf)));
  364. }
  365. if (route->ce_mask & ROUTE_ATTR_TYPE)
  366. dp_dump_line(p, line++, "ROUTE_TYPE=%s\n",
  367. nl_rtntype2str(route->rt_type, buf, sizeof(buf)));
  368. return line;
  369. }
  370. static int route_compare(struct nl_object *_a, struct nl_object *_b,
  371. uint32_t attrs, int flags)
  372. {
  373. struct rtnl_route *a = (struct rtnl_route *) _a;
  374. struct rtnl_route *b = (struct rtnl_route *) _b;
  375. int diff = 0;
  376. #define ROUTE_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ROUTE_ATTR_##ATTR, a, b, EXPR)
  377. diff |= ROUTE_DIFF(FAMILY, a->rt_family != b->rt_family);
  378. diff |= ROUTE_DIFF(TOS, a->rt_tos != b->rt_tos);
  379. diff |= ROUTE_DIFF(TABLE, a->rt_table != b->rt_table);
  380. diff |= ROUTE_DIFF(PROTOCOL, a->rt_protocol != b->rt_protocol);
  381. diff |= ROUTE_DIFF(SCOPE, a->rt_scope != b->rt_scope);
  382. diff |= ROUTE_DIFF(TYPE, a->rt_type != b->rt_type);
  383. diff |= ROUTE_DIFF(OIF, a->rt_oif != b->rt_oif);
  384. diff |= ROUTE_DIFF(PRIO, a->rt_prio != b->rt_prio);
  385. diff |= ROUTE_DIFF(REALMS, a->rt_realms != b->rt_realms);
  386. diff |= ROUTE_DIFF(MP_ALGO, a->rt_mp_algo != b->rt_mp_algo);
  387. diff |= ROUTE_DIFF(DST, nl_addr_cmp(a->rt_dst, b->rt_dst));
  388. diff |= ROUTE_DIFF(SRC, nl_addr_cmp(a->rt_src, b->rt_src));
  389. diff |= ROUTE_DIFF(IIF, strcmp(a->rt_iif, b->rt_iif));
  390. diff |= ROUTE_DIFF(PREF_SRC, nl_addr_cmp(a->rt_pref_src,
  391. b->rt_pref_src));
  392. diff |= ROUTE_DIFF(GATEWAY, nl_addr_cmp(a->rt_gateway,
  393. b->rt_gateway));
  394. /* FIXME: Compare metrics, multipath config */
  395. if (flags & LOOSE_FLAG_COMPARISON)
  396. diff |= ROUTE_DIFF(FLAGS,
  397. (a->rt_flags ^ b->rt_flags) & b->rt_flag_mask);
  398. else
  399. diff |= ROUTE_DIFF(FLAGS, a->rt_flags != b->rt_flags);
  400. #undef ROUTE_DIFF
  401. return diff;
  402. }
  403. static struct trans_tbl route_attrs[] = {
  404. __ADD(ROUTE_ATTR_FAMILY, family)
  405. __ADD(ROUTE_ATTR_TOS, tos)
  406. __ADD(ROUTE_ATTR_TABLE, table)
  407. __ADD(ROUTE_ATTR_PROTOCOL, protocol)
  408. __ADD(ROUTE_ATTR_SCOPE, scope)
  409. __ADD(ROUTE_ATTR_TYPE, type)
  410. __ADD(ROUTE_ATTR_FLAGS, flags)
  411. __ADD(ROUTE_ATTR_DST, dst)
  412. __ADD(ROUTE_ATTR_SRC, src)
  413. __ADD(ROUTE_ATTR_IIF, iif)
  414. __ADD(ROUTE_ATTR_OIF, oif)
  415. __ADD(ROUTE_ATTR_GATEWAY, gateway)
  416. __ADD(ROUTE_ATTR_PRIO, prio)
  417. __ADD(ROUTE_ATTR_PREF_SRC, pref_src)
  418. __ADD(ROUTE_ATTR_METRICS, metrics)
  419. __ADD(ROUTE_ATTR_MULTIPATH, multipath)
  420. __ADD(ROUTE_ATTR_REALMS, realms)
  421. __ADD(ROUTE_ATTR_CACHEINFO, cacheinfo)
  422. __ADD(ROUTE_ATTR_MP_ALGO, mp_algo)
  423. };
  424. static char *route_attrs2str(int attrs, char *buf, size_t len)
  425. {
  426. return __flags2str(attrs, buf, len, route_attrs,
  427. ARRAY_SIZE(route_attrs));
  428. }
  429. /**
  430. * @name Allocation/Freeing
  431. * @{
  432. */
  433. struct rtnl_route *rtnl_route_alloc(void)
  434. {
  435. return (struct rtnl_route *) nl_object_alloc(&route_obj_ops);
  436. }
  437. void rtnl_route_get(struct rtnl_route *route)
  438. {
  439. nl_object_get((struct nl_object *) route);
  440. }
  441. void rtnl_route_put(struct rtnl_route *route)
  442. {
  443. nl_object_put((struct nl_object *) route);
  444. }
  445. /** @} */
  446. /**
  447. * @name Attributes
  448. * @{
  449. */
  450. void rtnl_route_set_table(struct rtnl_route *route, int table)
  451. {
  452. route->rt_table = table;
  453. route->ce_mask |= ROUTE_ATTR_TABLE;
  454. }
  455. int rtnl_route_get_table(struct rtnl_route *route)
  456. {
  457. if (route->ce_mask & ROUTE_ATTR_TABLE)
  458. return route->rt_table;
  459. else
  460. return RT_TABLE_MAIN;
  461. }
  462. void rtnl_route_set_scope(struct rtnl_route *route, int scope)
  463. {
  464. route->rt_scope = scope;
  465. route->ce_mask |= ROUTE_ATTR_SCOPE;
  466. }
  467. int rtnl_route_get_scope(struct rtnl_route *route)
  468. {
  469. if (route->ce_mask & ROUTE_ATTR_SCOPE)
  470. return route->rt_scope;
  471. else
  472. return RT_SCOPE_NOWHERE;
  473. }
  474. void rtnl_route_set_tos(struct rtnl_route *route, int tos)
  475. {
  476. route->rt_tos = tos;
  477. route->ce_mask |= ROUTE_ATTR_TOS;
  478. }
  479. int rtnl_route_get_tos(struct rtnl_route *route)
  480. {
  481. return route->rt_tos;
  482. }
  483. void rtnl_route_set_realms(struct rtnl_route *route, realm_t realms)
  484. {
  485. route->rt_realms = realms;
  486. route->ce_mask |= ROUTE_ATTR_REALMS;
  487. }
  488. realm_t rtnl_route_get_realms(struct rtnl_route *route)
  489. {
  490. return route->rt_realms;
  491. }
  492. void rtnl_route_set_protocol(struct rtnl_route *route, int proto)
  493. {
  494. route->rt_protocol = proto;
  495. route->ce_mask |= ROUTE_ATTR_PROTOCOL;
  496. }
  497. int rtnl_route_get_protocol(struct rtnl_route *route)
  498. {
  499. if (route->ce_mask & ROUTE_ATTR_PROTOCOL)
  500. return route->rt_protocol;
  501. else
  502. return RTPROT_STATIC;
  503. }
  504. void rtnl_route_set_prio(struct rtnl_route *route, int prio)
  505. {
  506. route->rt_prio = prio;
  507. route->ce_mask |= ROUTE_ATTR_PRIO;
  508. }
  509. int rtnl_route_get_prio(struct rtnl_route *route)
  510. {
  511. return route->rt_prio;
  512. }
  513. void rtnl_route_set_family(struct rtnl_route *route, int family)
  514. {
  515. route->rt_family = family;
  516. route->ce_mask |= ROUTE_ATTR_FAMILY;
  517. }
  518. int rtnl_route_get_family(struct rtnl_route *route)
  519. {
  520. if (route->ce_mask & ROUTE_ATTR_FAMILY)
  521. return route->rt_family;
  522. else
  523. return AF_UNSPEC;
  524. }
  525. int rtnl_route_set_dst(struct rtnl_route *route, struct nl_addr *addr)
  526. {
  527. if (route->ce_mask & ROUTE_ATTR_FAMILY) {
  528. if (addr->a_family != route->rt_family)
  529. return nl_error(EINVAL, "Address family mismatch");
  530. } else
  531. route->rt_family = addr->a_family;
  532. if (route->rt_dst)
  533. nl_addr_put(route->rt_dst);
  534. nl_addr_get(addr);
  535. route->rt_dst = addr;
  536. route->ce_mask |= (ROUTE_ATTR_DST | ROUTE_ATTR_FAMILY);
  537. return 0;
  538. }
  539. struct nl_addr *rtnl_route_get_dst(struct rtnl_route *route)
  540. {
  541. return route->rt_dst;
  542. }
  543. int rtnl_route_get_dst_len(struct rtnl_route *route)
  544. {
  545. if (route->ce_mask & ROUTE_ATTR_DST)
  546. return nl_addr_get_prefixlen(route->rt_dst);
  547. else
  548. return 0;
  549. }
  550. int rtnl_route_set_src(struct rtnl_route *route, struct nl_addr *addr)
  551. {
  552. if (route->ce_mask & ROUTE_ATTR_FAMILY) {
  553. if (addr->a_family != route->rt_family)
  554. return nl_error(EINVAL, "Address family mismatch");
  555. } else
  556. route->rt_family = addr->a_family;
  557. if (route->rt_src)
  558. nl_addr_put(route->rt_src);
  559. nl_addr_get(addr);
  560. route->rt_src = addr;
  561. route->ce_mask |= (ROUTE_ATTR_SRC | ROUTE_ATTR_FAMILY);
  562. return 0;
  563. }
  564. struct nl_addr *rtnl_route_get_src(struct rtnl_route *route)
  565. {
  566. return route->rt_src;
  567. }
  568. int rtnl_route_get_src_len(struct rtnl_route *route)
  569. {
  570. if (route->ce_mask & ROUTE_ATTR_SRC)
  571. return nl_addr_get_prefixlen(route->rt_src);
  572. else
  573. return 0;
  574. }
  575. int rtnl_route_set_gateway(struct rtnl_route *route, struct nl_addr *addr)
  576. {
  577. if (route->ce_mask & ROUTE_ATTR_FAMILY) {
  578. if (addr->a_family != route->rt_family)
  579. return nl_error(EINVAL, "Address family mismatch");
  580. } else
  581. route->rt_family = addr->a_family;
  582. if (route->rt_gateway)
  583. nl_addr_put(route->rt_gateway);
  584. nl_addr_get(addr);
  585. route->rt_gateway = addr;
  586. route->ce_mask |= (ROUTE_ATTR_GATEWAY | ROUTE_ATTR_FAMILY);
  587. return 0;
  588. }
  589. struct nl_addr *rtnl_route_get_gateway(struct rtnl_route *route)
  590. {
  591. return route->rt_gateway;
  592. }
  593. void rtnl_route_set_type(struct rtnl_route *route, int type)
  594. {
  595. route->rt_type = type;
  596. route->ce_mask |= ROUTE_ATTR_TYPE;
  597. }
  598. int rtnl_route_get_type(struct rtnl_route *route)
  599. {
  600. if (route->ce_mask & ROUTE_ATTR_TYPE)
  601. return route->rt_type;
  602. else
  603. return RTN_UNICAST;
  604. }
  605. void rtnl_route_set_flags(struct rtnl_route *route, unsigned int flags)
  606. {
  607. route->rt_flag_mask |= flags;
  608. route->rt_flags |= flags;
  609. route->ce_mask |= ROUTE_ATTR_FLAGS;
  610. }
  611. void rtnl_route_unset_flags(struct rtnl_route *route, unsigned int flags)
  612. {
  613. route->rt_flag_mask |= flags;
  614. route->rt_flags &= ~flags;
  615. route->ce_mask |= ROUTE_ATTR_FLAGS;
  616. }
  617. unsigned int rtnl_route_get_flags(struct rtnl_route *route)
  618. {
  619. return route->rt_flags;
  620. }
  621. int rtnl_route_set_metric(struct rtnl_route *route, int metric, uint32_t value)
  622. {
  623. if (metric > RTAX_MAX || metric < 1)
  624. return nl_error(EINVAL, "Metric out of range (1..%d)",
  625. RTAX_MAX);
  626. route->rt_metrics[metric - 1] = value;
  627. route->rt_metrics_mask |= (1 << (metric - 1));
  628. return 0;
  629. }
  630. int rtnl_route_unset_metric(struct rtnl_route *route, int metric)
  631. {
  632. if (metric > RTAX_MAX || metric < 1)
  633. return nl_error(EINVAL, "Metric out of range (1..%d)",
  634. RTAX_MAX);
  635. route->rt_metrics_mask &= ~(1 << (metric - 1));
  636. return 0;
  637. }
  638. unsigned int rtnl_route_get_metric(struct rtnl_route *route, int metric)
  639. {
  640. if (metric > RTAX_MAX || metric < 1)
  641. return UINT_MAX;
  642. if (!(route->rt_metrics_mask & (1 << (metric - 1))))
  643. return UINT_MAX;
  644. return route->rt_metrics[metric - 1];
  645. }
  646. int rtnl_route_set_pref_src(struct rtnl_route *route, struct nl_addr *addr)
  647. {
  648. if (route->ce_mask & ROUTE_ATTR_FAMILY) {
  649. if (addr->a_family != route->rt_family)
  650. return nl_error(EINVAL, "Address family mismatch");
  651. } else
  652. route->rt_family = addr->a_family;
  653. if (route->rt_pref_src)
  654. nl_addr_put(route->rt_pref_src);
  655. nl_addr_get(addr);
  656. route->rt_pref_src = addr;
  657. route->ce_mask |= (ROUTE_ATTR_PREF_SRC | ROUTE_ATTR_FAMILY);
  658. return 0;
  659. }
  660. struct nl_addr *rtnl_route_get_pref_src(struct rtnl_route *route)
  661. {
  662. return route->rt_pref_src;
  663. }
  664. void rtnl_route_set_oif(struct rtnl_route *route, int ifindex)
  665. {
  666. route->rt_oif = ifindex;
  667. route->ce_mask |= ROUTE_ATTR_OIF;
  668. }
  669. int rtnl_route_get_oif(struct rtnl_route *route)
  670. {
  671. if (route->ce_mask & ROUTE_ATTR_OIF)
  672. return route->rt_oif;
  673. else
  674. return RTNL_LINK_NOT_FOUND;
  675. }
  676. void rtnl_route_set_iif(struct rtnl_route *route, const char *name)
  677. {
  678. strncpy(route->rt_iif, name, sizeof(route->rt_iif) - 1);
  679. route->ce_mask |= ROUTE_ATTR_IIF;
  680. }
  681. char *rtnl_route_get_iif(struct rtnl_route *route)
  682. {
  683. if (route->ce_mask & ROUTE_ATTR_IIF)
  684. return route->rt_iif;
  685. else
  686. return NULL;
  687. }
  688. void rtnl_route_add_nexthop(struct rtnl_route *route, struct rtnl_nexthop *nh)
  689. {
  690. nl_list_add_tail(&nh->rtnh_list, &route->rt_nexthops);
  691. route->ce_mask |= ROUTE_ATTR_MULTIPATH;
  692. }
  693. void rtnl_route_remove_nexthop(struct rtnl_nexthop *nh)
  694. {
  695. nl_list_del(&nh->rtnh_list);
  696. }
  697. struct nl_list_head *rtnl_route_get_nexthops(struct rtnl_route *route)
  698. {
  699. return &route->rt_nexthops;
  700. }
  701. void rtnl_route_set_cacheinfo(struct rtnl_route *route,
  702. struct rtnl_rtcacheinfo *ci)
  703. {
  704. memcpy(&route->rt_cacheinfo, ci, sizeof(*ci));
  705. route->ce_mask |= ROUTE_ATTR_CACHEINFO;
  706. }
  707. uint32_t rtnl_route_get_mp_algo(struct rtnl_route *route)
  708. {
  709. if (route->ce_mask & ROUTE_ATTR_MP_ALGO)
  710. return route->rt_mp_algo;
  711. else
  712. return IP_MP_ALG_NONE;
  713. }
  714. void rtnl_route_set_mp_algo(struct rtnl_route *route, uint32_t algo)
  715. {
  716. route->rt_mp_algo = algo;
  717. route->ce_mask |= ROUTE_ATTR_MP_ALGO;
  718. }
  719. /** @} */
  720. struct nl_object_ops route_obj_ops = {
  721. .oo_name = "route/route",
  722. .oo_size = sizeof(struct rtnl_route),
  723. .oo_constructor = route_constructor,
  724. .oo_free_data = route_free_data,
  725. .oo_clone = route_clone,
  726. .oo_dump[NL_DUMP_BRIEF] = route_dump_brief,
  727. .oo_dump[NL_DUMP_FULL] = route_dump_full,
  728. .oo_dump[NL_DUMP_STATS] = route_dump_stats,
  729. .oo_dump[NL_DUMP_XML] = route_dump_xml,
  730. .oo_dump[NL_DUMP_ENV] = route_dump_env,
  731. .oo_compare = route_compare,
  732. .oo_attrs2str = route_attrs2str,
  733. .oo_id_attrs = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
  734. ROUTE_ATTR_TABLE | ROUTE_ATTR_DST),
  735. };
  736. /** @} */