route_obj.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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-2008 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. * protocol RTPROT_STATIC
  23. * prio 0
  24. * family AF_UNSPEC
  25. * type RTN_UNICAST
  26. * iif NULL
  27. * @endcode
  28. *
  29. * @{
  30. */
  31. #include <netlink-private/netlink.h>
  32. #include <netlink/netlink.h>
  33. #include <netlink/cache.h>
  34. #include <netlink/utils.h>
  35. #include <netlink/data.h>
  36. #include <netlink/hashtable.h>
  37. #include <netlink/route/rtnl.h>
  38. #include <netlink/route/route.h>
  39. #include <netlink/route/link.h>
  40. #include <netlink/route/nexthop.h>
  41. /** @cond SKIP */
  42. #define ROUTE_ATTR_FAMILY 0x000001
  43. #define ROUTE_ATTR_TOS 0x000002
  44. #define ROUTE_ATTR_TABLE 0x000004
  45. #define ROUTE_ATTR_PROTOCOL 0x000008
  46. #define ROUTE_ATTR_SCOPE 0x000010
  47. #define ROUTE_ATTR_TYPE 0x000020
  48. #define ROUTE_ATTR_FLAGS 0x000040
  49. #define ROUTE_ATTR_DST 0x000080
  50. #define ROUTE_ATTR_SRC 0x000100
  51. #define ROUTE_ATTR_IIF 0x000200
  52. #define ROUTE_ATTR_OIF 0x000400
  53. #define ROUTE_ATTR_GATEWAY 0x000800
  54. #define ROUTE_ATTR_PRIO 0x001000
  55. #define ROUTE_ATTR_PREF_SRC 0x002000
  56. #define ROUTE_ATTR_METRICS 0x004000
  57. #define ROUTE_ATTR_MULTIPATH 0x008000
  58. #define ROUTE_ATTR_REALMS 0x010000
  59. #define ROUTE_ATTR_CACHEINFO 0x020000
  60. /** @endcond */
  61. static void route_constructor(struct nl_object *c)
  62. {
  63. struct rtnl_route *r = (struct rtnl_route *) c;
  64. r->rt_family = AF_UNSPEC;
  65. r->rt_scope = RT_SCOPE_NOWHERE;
  66. r->rt_table = RT_TABLE_MAIN;
  67. r->rt_protocol = RTPROT_STATIC;
  68. r->rt_type = RTN_UNICAST;
  69. r->rt_prio = 0;
  70. nl_init_list_head(&r->rt_nexthops);
  71. }
  72. static void route_free_data(struct nl_object *c)
  73. {
  74. struct rtnl_route *r = (struct rtnl_route *) c;
  75. struct rtnl_nexthop *nh, *tmp;
  76. if (r == NULL)
  77. return;
  78. nl_addr_put(r->rt_dst);
  79. nl_addr_put(r->rt_src);
  80. nl_addr_put(r->rt_pref_src);
  81. nl_list_for_each_entry_safe(nh, tmp, &r->rt_nexthops, rtnh_list) {
  82. rtnl_route_remove_nexthop(r, nh);
  83. rtnl_route_nh_free(nh);
  84. }
  85. }
  86. static int route_clone(struct nl_object *_dst, struct nl_object *_src)
  87. {
  88. struct rtnl_route *dst = (struct rtnl_route *) _dst;
  89. struct rtnl_route *src = (struct rtnl_route *) _src;
  90. struct rtnl_nexthop *nh, *new;
  91. if (src->rt_dst)
  92. if (!(dst->rt_dst = nl_addr_clone(src->rt_dst)))
  93. return -NLE_NOMEM;
  94. if (src->rt_src)
  95. if (!(dst->rt_src = nl_addr_clone(src->rt_src)))
  96. return -NLE_NOMEM;
  97. if (src->rt_pref_src)
  98. if (!(dst->rt_pref_src = nl_addr_clone(src->rt_pref_src)))
  99. return -NLE_NOMEM;
  100. /* Will be inc'ed again while adding the nexthops of the source */
  101. dst->rt_nr_nh = 0;
  102. nl_init_list_head(&dst->rt_nexthops);
  103. nl_list_for_each_entry(nh, &src->rt_nexthops, rtnh_list) {
  104. new = rtnl_route_nh_clone(nh);
  105. if (!new)
  106. return -NLE_NOMEM;
  107. rtnl_route_add_nexthop(dst, new);
  108. }
  109. return 0;
  110. }
  111. static void route_dump_line(struct nl_object *a, struct nl_dump_params *p)
  112. {
  113. struct rtnl_route *r = (struct rtnl_route *) a;
  114. int cache = 0, flags;
  115. char buf[64];
  116. if (r->rt_flags & RTM_F_CLONED)
  117. cache = 1;
  118. nl_dump_line(p, "%s ", nl_af2str(r->rt_family, buf, sizeof(buf)));
  119. if (cache)
  120. nl_dump(p, "cache ");
  121. if (!(r->ce_mask & ROUTE_ATTR_DST) ||
  122. nl_addr_get_len(r->rt_dst) == 0)
  123. nl_dump(p, "default ");
  124. else
  125. nl_dump(p, "%s ", nl_addr2str(r->rt_dst, buf, sizeof(buf)));
  126. if (r->ce_mask & ROUTE_ATTR_TABLE && !cache)
  127. nl_dump(p, "table %s ",
  128. rtnl_route_table2str(r->rt_table, buf, sizeof(buf)));
  129. if (r->ce_mask & ROUTE_ATTR_TYPE)
  130. nl_dump(p, "type %s ",
  131. nl_rtntype2str(r->rt_type, buf, sizeof(buf)));
  132. if (r->ce_mask & ROUTE_ATTR_TOS && r->rt_tos != 0)
  133. nl_dump(p, "tos %#x ", r->rt_tos);
  134. if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
  135. struct rtnl_nexthop *nh;
  136. nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
  137. p->dp_ivar = NH_DUMP_FROM_ONELINE;
  138. rtnl_route_nh_dump(nh, p);
  139. }
  140. }
  141. flags = r->rt_flags & ~(RTM_F_CLONED);
  142. if (r->ce_mask & ROUTE_ATTR_FLAGS && flags) {
  143. nl_dump(p, "<");
  144. #define PRINT_FLAG(f) if (flags & RTNH_F_##f) { \
  145. flags &= ~RTNH_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
  146. PRINT_FLAG(DEAD);
  147. PRINT_FLAG(ONLINK);
  148. PRINT_FLAG(PERVASIVE);
  149. #undef PRINT_FLAG
  150. #define PRINT_FLAG(f) if (flags & RTM_F_##f) { \
  151. flags &= ~RTM_F_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
  152. PRINT_FLAG(NOTIFY);
  153. PRINT_FLAG(EQUALIZE);
  154. PRINT_FLAG(PREFIX);
  155. #undef PRINT_FLAG
  156. #define PRINT_FLAG(f) if (flags & RTCF_##f) { \
  157. flags &= ~RTCF_##f; nl_dump(p, #f "%s", flags ? "," : ""); }
  158. PRINT_FLAG(NOTIFY);
  159. PRINT_FLAG(REDIRECTED);
  160. PRINT_FLAG(DOREDIRECT);
  161. PRINT_FLAG(DIRECTSRC);
  162. PRINT_FLAG(DNAT);
  163. PRINT_FLAG(BROADCAST);
  164. PRINT_FLAG(MULTICAST);
  165. PRINT_FLAG(LOCAL);
  166. #undef PRINT_FLAG
  167. nl_dump(p, ">");
  168. }
  169. nl_dump(p, "\n");
  170. }
  171. static void route_dump_details(struct nl_object *a, struct nl_dump_params *p)
  172. {
  173. struct rtnl_route *r = (struct rtnl_route *) a;
  174. struct nl_cache *link_cache;
  175. char buf[256];
  176. int i;
  177. link_cache = nl_cache_mngt_require_safe("route/link");
  178. route_dump_line(a, p);
  179. nl_dump_line(p, " ");
  180. if (r->ce_mask & ROUTE_ATTR_PREF_SRC)
  181. nl_dump(p, "preferred-src %s ",
  182. nl_addr2str(r->rt_pref_src, buf, sizeof(buf)));
  183. if (r->ce_mask & ROUTE_ATTR_SCOPE && r->rt_scope != RT_SCOPE_NOWHERE)
  184. nl_dump(p, "scope %s ",
  185. rtnl_scope2str(r->rt_scope, buf, sizeof(buf)));
  186. if (r->ce_mask & ROUTE_ATTR_PRIO)
  187. nl_dump(p, "priority %#x ", r->rt_prio);
  188. if (r->ce_mask & ROUTE_ATTR_PROTOCOL)
  189. nl_dump(p, "protocol %s ",
  190. rtnl_route_proto2str(r->rt_protocol, buf, sizeof(buf)));
  191. if (r->ce_mask & ROUTE_ATTR_IIF) {
  192. if (link_cache) {
  193. nl_dump(p, "iif %s ",
  194. rtnl_link_i2name(link_cache, r->rt_iif,
  195. buf, sizeof(buf)));
  196. } else
  197. nl_dump(p, "iif %d ", r->rt_iif);
  198. }
  199. if (r->ce_mask & ROUTE_ATTR_SRC)
  200. nl_dump(p, "src %s ", nl_addr2str(r->rt_src, buf, sizeof(buf)));
  201. nl_dump(p, "\n");
  202. if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
  203. struct rtnl_nexthop *nh;
  204. nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
  205. nl_dump_line(p, " ");
  206. p->dp_ivar = NH_DUMP_FROM_DETAILS;
  207. rtnl_route_nh_dump(nh, p);
  208. nl_dump(p, "\n");
  209. }
  210. }
  211. if ((r->ce_mask & ROUTE_ATTR_CACHEINFO) && r->rt_cacheinfo.rtci_error) {
  212. nl_dump_line(p, " cacheinfo error %d (%s)\n",
  213. r->rt_cacheinfo.rtci_error,
  214. strerror_r(-r->rt_cacheinfo.rtci_error, buf, sizeof(buf)));
  215. }
  216. if (r->ce_mask & ROUTE_ATTR_METRICS) {
  217. nl_dump_line(p, " metrics [");
  218. for (i = 0; i < RTAX_MAX; i++)
  219. if (r->rt_metrics_mask & (1 << i))
  220. nl_dump(p, "%s %u ",
  221. rtnl_route_metric2str(i+1,
  222. buf, sizeof(buf)),
  223. r->rt_metrics[i]);
  224. nl_dump(p, "]\n");
  225. }
  226. if (link_cache)
  227. nl_cache_put(link_cache);
  228. }
  229. static void route_dump_stats(struct nl_object *obj, struct nl_dump_params *p)
  230. {
  231. struct rtnl_route *route = (struct rtnl_route *) obj;
  232. route_dump_details(obj, p);
  233. if (route->ce_mask & ROUTE_ATTR_CACHEINFO) {
  234. struct rtnl_rtcacheinfo *ci = &route->rt_cacheinfo;
  235. nl_dump_line(p, " used %u refcnt %u last-use %us "
  236. "expires %us\n",
  237. ci->rtci_used, ci->rtci_clntref,
  238. ci->rtci_last_use / nl_get_user_hz(),
  239. ci->rtci_expires / nl_get_user_hz());
  240. }
  241. }
  242. static void route_keygen(struct nl_object *obj, uint32_t *hashkey,
  243. uint32_t table_sz)
  244. {
  245. struct rtnl_route *route = (struct rtnl_route *) obj;
  246. unsigned int rkey_sz;
  247. struct nl_addr *addr = NULL;
  248. struct route_hash_key {
  249. uint8_t rt_family;
  250. uint8_t rt_tos;
  251. uint32_t rt_table;
  252. uint32_t rt_prio;
  253. char rt_addr[0];
  254. } __attribute__((packed)) *rkey;
  255. #ifdef NL_DEBUG
  256. char buf[INET6_ADDRSTRLEN+5];
  257. #endif
  258. if (route->rt_dst)
  259. addr = route->rt_dst;
  260. rkey_sz = sizeof(*rkey);
  261. if (addr)
  262. rkey_sz += nl_addr_get_len(addr);
  263. rkey = calloc(1, rkey_sz);
  264. if (!rkey) {
  265. NL_DBG(2, "Warning: calloc failed for %d bytes...\n", rkey_sz);
  266. *hashkey = 0;
  267. return;
  268. }
  269. rkey->rt_family = route->rt_family;
  270. rkey->rt_tos = route->rt_tos;
  271. rkey->rt_table = route->rt_table;
  272. rkey->rt_prio = route->rt_prio;
  273. if (addr)
  274. memcpy(rkey->rt_addr, nl_addr_get_binary_addr(addr),
  275. nl_addr_get_len(addr));
  276. *hashkey = nl_hash(rkey, rkey_sz, 0) % table_sz;
  277. NL_DBG(5, "route %p key (fam %d tos %d table %d addr %s) keysz %d "
  278. "hash 0x%x\n", route, rkey->rt_family, rkey->rt_tos,
  279. rkey->rt_table, nl_addr2str(addr, buf, sizeof(buf)),
  280. rkey_sz, *hashkey);
  281. free(rkey);
  282. return;
  283. }
  284. static int route_compare(struct nl_object *_a, struct nl_object *_b,
  285. uint32_t attrs, int flags)
  286. {
  287. struct rtnl_route *a = (struct rtnl_route *) _a;
  288. struct rtnl_route *b = (struct rtnl_route *) _b;
  289. struct rtnl_nexthop *nh_a, *nh_b;
  290. int i, diff = 0, found;
  291. #define ROUTE_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, ROUTE_ATTR_##ATTR, a, b, EXPR)
  292. diff |= ROUTE_DIFF(FAMILY, a->rt_family != b->rt_family);
  293. diff |= ROUTE_DIFF(TOS, a->rt_tos != b->rt_tos);
  294. diff |= ROUTE_DIFF(TABLE, a->rt_table != b->rt_table);
  295. diff |= ROUTE_DIFF(PROTOCOL, a->rt_protocol != b->rt_protocol);
  296. diff |= ROUTE_DIFF(SCOPE, a->rt_scope != b->rt_scope);
  297. diff |= ROUTE_DIFF(TYPE, a->rt_type != b->rt_type);
  298. diff |= ROUTE_DIFF(PRIO, a->rt_prio != b->rt_prio);
  299. diff |= ROUTE_DIFF(DST, nl_addr_cmp(a->rt_dst, b->rt_dst));
  300. diff |= ROUTE_DIFF(SRC, nl_addr_cmp(a->rt_src, b->rt_src));
  301. diff |= ROUTE_DIFF(IIF, a->rt_iif != b->rt_iif);
  302. diff |= ROUTE_DIFF(PREF_SRC, nl_addr_cmp(a->rt_pref_src,
  303. b->rt_pref_src));
  304. if (flags & LOOSE_COMPARISON) {
  305. nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
  306. found = 0;
  307. nl_list_for_each_entry(nh_a, &a->rt_nexthops,
  308. rtnh_list) {
  309. if (!rtnl_route_nh_compare(nh_a, nh_b,
  310. nh_b->ce_mask, 1)) {
  311. found = 1;
  312. break;
  313. }
  314. }
  315. if (!found)
  316. goto nh_mismatch;
  317. }
  318. for (i = 0; i < RTAX_MAX - 1; i++) {
  319. if (a->rt_metrics_mask & (1 << i) &&
  320. (!(b->rt_metrics_mask & (1 << i)) ||
  321. a->rt_metrics[i] != b->rt_metrics[i]))
  322. diff |= ROUTE_DIFF(METRICS, 1);
  323. }
  324. diff |= ROUTE_DIFF(FLAGS,
  325. (a->rt_flags ^ b->rt_flags) & b->rt_flag_mask);
  326. } else {
  327. if (a->rt_nr_nh != b->rt_nr_nh)
  328. goto nh_mismatch;
  329. /* search for a dup in each nh of a */
  330. nl_list_for_each_entry(nh_a, &a->rt_nexthops, rtnh_list) {
  331. found = 0;
  332. nl_list_for_each_entry(nh_b, &b->rt_nexthops,
  333. rtnh_list) {
  334. if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
  335. found = 1;
  336. break;
  337. }
  338. }
  339. if (!found)
  340. goto nh_mismatch;
  341. }
  342. /* search for a dup in each nh of b, covers case where a has
  343. * dupes itself */
  344. nl_list_for_each_entry(nh_b, &b->rt_nexthops, rtnh_list) {
  345. found = 0;
  346. nl_list_for_each_entry(nh_a, &a->rt_nexthops,
  347. rtnh_list) {
  348. if (!rtnl_route_nh_compare(nh_a, nh_b, ~0, 0)) {
  349. found = 1;
  350. break;
  351. }
  352. }
  353. if (!found)
  354. goto nh_mismatch;
  355. }
  356. for (i = 0; i < RTAX_MAX - 1; i++) {
  357. if ((a->rt_metrics_mask & (1 << i)) ^
  358. (b->rt_metrics_mask & (1 << i)))
  359. diff |= ROUTE_DIFF(METRICS, 1);
  360. else
  361. diff |= ROUTE_DIFF(METRICS,
  362. a->rt_metrics[i] != b->rt_metrics[i]);
  363. }
  364. diff |= ROUTE_DIFF(FLAGS, a->rt_flags != b->rt_flags);
  365. }
  366. out:
  367. return diff;
  368. nh_mismatch:
  369. diff |= ROUTE_DIFF(MULTIPATH, 1);
  370. goto out;
  371. #undef ROUTE_DIFF
  372. }
  373. static int route_update(struct nl_object *old_obj, struct nl_object *new_obj)
  374. {
  375. struct rtnl_route *new_route = (struct rtnl_route *) new_obj;
  376. struct rtnl_route *old_route = (struct rtnl_route *) old_obj;
  377. struct rtnl_nexthop *new_nh;
  378. int action = new_obj->ce_msgtype;
  379. #ifdef NL_DEBUG
  380. char buf[INET6_ADDRSTRLEN+5];
  381. #endif
  382. /*
  383. * ipv6 ECMP route notifications from the kernel come as
  384. * separate notifications, one for every nexthop. This update
  385. * function collapses such route msgs into a single
  386. * route with multiple nexthops. The resulting object looks
  387. * similar to a ipv4 ECMP route
  388. */
  389. if (new_route->rt_family != AF_INET6 ||
  390. new_route->rt_table == RT_TABLE_LOCAL)
  391. return -NLE_OPNOTSUPP;
  392. /*
  393. * For routes that are already multipath,
  394. * or dont have a nexthop dont do anything
  395. */
  396. if (rtnl_route_get_nnexthops(new_route) != 1)
  397. return -NLE_OPNOTSUPP;
  398. /*
  399. * Get the only nexthop entry from the new route. For
  400. * IPv6 we always get a route with a 0th NH
  401. * filled or nothing at all
  402. */
  403. new_nh = rtnl_route_nexthop_n(new_route, 0);
  404. if (!new_nh || !rtnl_route_nh_get_gateway(new_nh))
  405. return -NLE_OPNOTSUPP;
  406. switch(action) {
  407. case RTM_NEWROUTE : {
  408. struct rtnl_nexthop *cloned_nh;
  409. /*
  410. * Add the nexthop to old route
  411. */
  412. cloned_nh = rtnl_route_nh_clone(new_nh);
  413. if (!cloned_nh)
  414. return -NLE_NOMEM;
  415. rtnl_route_add_nexthop(old_route, cloned_nh);
  416. NL_DBG(2, "Route obj %p updated. Added "
  417. "nexthop %p via %s\n", old_route, cloned_nh,
  418. nl_addr2str(cloned_nh->rtnh_gateway, buf,
  419. sizeof(buf)));
  420. }
  421. break;
  422. case RTM_DELROUTE : {
  423. struct rtnl_nexthop *old_nh;
  424. /*
  425. * Only take care of nexthop deletes and not
  426. * route deletes. So, if there is only one nexthop
  427. * quite likely we did not update it. So dont do
  428. * anything and return
  429. */
  430. if (rtnl_route_get_nnexthops(old_route) <= 1)
  431. return -NLE_OPNOTSUPP;
  432. /*
  433. * Find the next hop in old route and delete it
  434. */
  435. nl_list_for_each_entry(old_nh, &old_route->rt_nexthops,
  436. rtnh_list) {
  437. if (!rtnl_route_nh_compare(old_nh, new_nh, ~0, 0)) {
  438. rtnl_route_remove_nexthop(old_route, old_nh);
  439. NL_DBG(2, "Route obj %p updated. Removed "
  440. "nexthop %p via %s\n", old_route,
  441. old_nh,
  442. nl_addr2str(old_nh->rtnh_gateway, buf,
  443. sizeof(buf)));
  444. rtnl_route_nh_free(old_nh);
  445. break;
  446. }
  447. }
  448. }
  449. break;
  450. default:
  451. NL_DBG(2, "Unknown action associated "
  452. "to object %p during route update\n", new_obj);
  453. return -NLE_OPNOTSUPP;
  454. }
  455. return NLE_SUCCESS;
  456. }
  457. static const struct trans_tbl route_attrs[] = {
  458. __ADD(ROUTE_ATTR_FAMILY, family)
  459. __ADD(ROUTE_ATTR_TOS, tos)
  460. __ADD(ROUTE_ATTR_TABLE, table)
  461. __ADD(ROUTE_ATTR_PROTOCOL, protocol)
  462. __ADD(ROUTE_ATTR_SCOPE, scope)
  463. __ADD(ROUTE_ATTR_TYPE, type)
  464. __ADD(ROUTE_ATTR_FLAGS, flags)
  465. __ADD(ROUTE_ATTR_DST, dst)
  466. __ADD(ROUTE_ATTR_SRC, src)
  467. __ADD(ROUTE_ATTR_IIF, iif)
  468. __ADD(ROUTE_ATTR_OIF, oif)
  469. __ADD(ROUTE_ATTR_GATEWAY, gateway)
  470. __ADD(ROUTE_ATTR_PRIO, prio)
  471. __ADD(ROUTE_ATTR_PREF_SRC, pref_src)
  472. __ADD(ROUTE_ATTR_METRICS, metrics)
  473. __ADD(ROUTE_ATTR_MULTIPATH, multipath)
  474. __ADD(ROUTE_ATTR_REALMS, realms)
  475. __ADD(ROUTE_ATTR_CACHEINFO, cacheinfo)
  476. };
  477. static char *route_attrs2str(int attrs, char *buf, size_t len)
  478. {
  479. return __flags2str(attrs, buf, len, route_attrs,
  480. ARRAY_SIZE(route_attrs));
  481. }
  482. /**
  483. * @name Allocation/Freeing
  484. * @{
  485. */
  486. struct rtnl_route *rtnl_route_alloc(void)
  487. {
  488. return (struct rtnl_route *) nl_object_alloc(&route_obj_ops);
  489. }
  490. void rtnl_route_get(struct rtnl_route *route)
  491. {
  492. nl_object_get((struct nl_object *) route);
  493. }
  494. void rtnl_route_put(struct rtnl_route *route)
  495. {
  496. nl_object_put((struct nl_object *) route);
  497. }
  498. /** @} */
  499. /**
  500. * @name Attributes
  501. * @{
  502. */
  503. void rtnl_route_set_table(struct rtnl_route *route, uint32_t table)
  504. {
  505. route->rt_table = table;
  506. route->ce_mask |= ROUTE_ATTR_TABLE;
  507. }
  508. uint32_t rtnl_route_get_table(struct rtnl_route *route)
  509. {
  510. return route->rt_table;
  511. }
  512. void rtnl_route_set_scope(struct rtnl_route *route, uint8_t scope)
  513. {
  514. route->rt_scope = scope;
  515. route->ce_mask |= ROUTE_ATTR_SCOPE;
  516. }
  517. uint8_t rtnl_route_get_scope(struct rtnl_route *route)
  518. {
  519. return route->rt_scope;
  520. }
  521. void rtnl_route_set_tos(struct rtnl_route *route, uint8_t tos)
  522. {
  523. route->rt_tos = tos;
  524. route->ce_mask |= ROUTE_ATTR_TOS;
  525. }
  526. uint8_t rtnl_route_get_tos(struct rtnl_route *route)
  527. {
  528. return route->rt_tos;
  529. }
  530. void rtnl_route_set_protocol(struct rtnl_route *route, uint8_t protocol)
  531. {
  532. route->rt_protocol = protocol;
  533. route->ce_mask |= ROUTE_ATTR_PROTOCOL;
  534. }
  535. uint8_t rtnl_route_get_protocol(struct rtnl_route *route)
  536. {
  537. return route->rt_protocol;
  538. }
  539. void rtnl_route_set_priority(struct rtnl_route *route, uint32_t prio)
  540. {
  541. route->rt_prio = prio;
  542. route->ce_mask |= ROUTE_ATTR_PRIO;
  543. }
  544. uint32_t rtnl_route_get_priority(struct rtnl_route *route)
  545. {
  546. return route->rt_prio;
  547. }
  548. int rtnl_route_set_family(struct rtnl_route *route, uint8_t family)
  549. {
  550. if (family != AF_INET && family != AF_INET6 && family != AF_DECnet)
  551. return -NLE_AF_NOSUPPORT;
  552. route->rt_family = family;
  553. route->ce_mask |= ROUTE_ATTR_FAMILY;
  554. return 0;
  555. }
  556. uint8_t rtnl_route_get_family(struct rtnl_route *route)
  557. {
  558. return route->rt_family;
  559. }
  560. int rtnl_route_set_dst(struct rtnl_route *route, struct nl_addr *addr)
  561. {
  562. if (route->ce_mask & ROUTE_ATTR_FAMILY) {
  563. if (addr->a_family != route->rt_family)
  564. return -NLE_AF_MISMATCH;
  565. } else
  566. route->rt_family = addr->a_family;
  567. if (route->rt_dst)
  568. nl_addr_put(route->rt_dst);
  569. nl_addr_get(addr);
  570. route->rt_dst = addr;
  571. route->ce_mask |= (ROUTE_ATTR_DST | ROUTE_ATTR_FAMILY);
  572. return 0;
  573. }
  574. struct nl_addr *rtnl_route_get_dst(struct rtnl_route *route)
  575. {
  576. return route->rt_dst;
  577. }
  578. int rtnl_route_set_src(struct rtnl_route *route, struct nl_addr *addr)
  579. {
  580. if (addr->a_family == AF_INET)
  581. return -NLE_SRCRT_NOSUPPORT;
  582. if (route->ce_mask & ROUTE_ATTR_FAMILY) {
  583. if (addr->a_family != route->rt_family)
  584. return -NLE_AF_MISMATCH;
  585. } else
  586. route->rt_family = addr->a_family;
  587. if (route->rt_src)
  588. nl_addr_put(route->rt_src);
  589. nl_addr_get(addr);
  590. route->rt_src = addr;
  591. route->ce_mask |= (ROUTE_ATTR_SRC | ROUTE_ATTR_FAMILY);
  592. return 0;
  593. }
  594. struct nl_addr *rtnl_route_get_src(struct rtnl_route *route)
  595. {
  596. return route->rt_src;
  597. }
  598. int rtnl_route_set_type(struct rtnl_route *route, uint8_t type)
  599. {
  600. if (type > RTN_MAX)
  601. return -NLE_RANGE;
  602. route->rt_type = type;
  603. route->ce_mask |= ROUTE_ATTR_TYPE;
  604. return 0;
  605. }
  606. uint8_t rtnl_route_get_type(struct rtnl_route *route)
  607. {
  608. return route->rt_type;
  609. }
  610. void rtnl_route_set_flags(struct rtnl_route *route, uint32_t flags)
  611. {
  612. route->rt_flag_mask |= flags;
  613. route->rt_flags |= flags;
  614. route->ce_mask |= ROUTE_ATTR_FLAGS;
  615. }
  616. void rtnl_route_unset_flags(struct rtnl_route *route, uint32_t flags)
  617. {
  618. route->rt_flag_mask |= flags;
  619. route->rt_flags &= ~flags;
  620. route->ce_mask |= ROUTE_ATTR_FLAGS;
  621. }
  622. uint32_t rtnl_route_get_flags(struct rtnl_route *route)
  623. {
  624. return route->rt_flags;
  625. }
  626. int rtnl_route_set_metric(struct rtnl_route *route, int metric, uint32_t value)
  627. {
  628. if (metric > RTAX_MAX || metric < 1)
  629. return -NLE_RANGE;
  630. route->rt_metrics[metric - 1] = value;
  631. if (!(route->rt_metrics_mask & (1 << (metric - 1)))) {
  632. route->rt_nmetrics++;
  633. route->rt_metrics_mask |= (1 << (metric - 1));
  634. }
  635. route->ce_mask |= ROUTE_ATTR_METRICS;
  636. return 0;
  637. }
  638. int rtnl_route_unset_metric(struct rtnl_route *route, int metric)
  639. {
  640. if (metric > RTAX_MAX || metric < 1)
  641. return -NLE_RANGE;
  642. if (route->rt_metrics_mask & (1 << (metric - 1))) {
  643. route->rt_nmetrics--;
  644. route->rt_metrics_mask &= ~(1 << (metric - 1));
  645. }
  646. return 0;
  647. }
  648. int rtnl_route_get_metric(struct rtnl_route *route, int metric, uint32_t *value)
  649. {
  650. if (metric > RTAX_MAX || metric < 1)
  651. return -NLE_RANGE;
  652. if (!(route->rt_metrics_mask & (1 << (metric - 1))))
  653. return -NLE_OBJ_NOTFOUND;
  654. if (value)
  655. *value = route->rt_metrics[metric - 1];
  656. return 0;
  657. }
  658. int rtnl_route_set_pref_src(struct rtnl_route *route, struct nl_addr *addr)
  659. {
  660. if (route->ce_mask & ROUTE_ATTR_FAMILY) {
  661. if (addr->a_family != route->rt_family)
  662. return -NLE_AF_MISMATCH;
  663. } else
  664. route->rt_family = addr->a_family;
  665. if (route->rt_pref_src)
  666. nl_addr_put(route->rt_pref_src);
  667. nl_addr_get(addr);
  668. route->rt_pref_src = addr;
  669. route->ce_mask |= (ROUTE_ATTR_PREF_SRC | ROUTE_ATTR_FAMILY);
  670. return 0;
  671. }
  672. struct nl_addr *rtnl_route_get_pref_src(struct rtnl_route *route)
  673. {
  674. return route->rt_pref_src;
  675. }
  676. void rtnl_route_set_iif(struct rtnl_route *route, int ifindex)
  677. {
  678. route->rt_iif = ifindex;
  679. route->ce_mask |= ROUTE_ATTR_IIF;
  680. }
  681. int rtnl_route_get_iif(struct rtnl_route *route)
  682. {
  683. return route->rt_iif;
  684. }
  685. void rtnl_route_add_nexthop(struct rtnl_route *route, struct rtnl_nexthop *nh)
  686. {
  687. nl_list_add_tail(&nh->rtnh_list, &route->rt_nexthops);
  688. route->rt_nr_nh++;
  689. route->ce_mask |= ROUTE_ATTR_MULTIPATH;
  690. }
  691. void rtnl_route_remove_nexthop(struct rtnl_route *route, struct rtnl_nexthop *nh)
  692. {
  693. if (route->ce_mask & ROUTE_ATTR_MULTIPATH) {
  694. route->rt_nr_nh--;
  695. nl_list_del(&nh->rtnh_list);
  696. }
  697. }
  698. struct nl_list_head *rtnl_route_get_nexthops(struct rtnl_route *route)
  699. {
  700. if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
  701. return &route->rt_nexthops;
  702. return NULL;
  703. }
  704. int rtnl_route_get_nnexthops(struct rtnl_route *route)
  705. {
  706. if (route->ce_mask & ROUTE_ATTR_MULTIPATH)
  707. return route->rt_nr_nh;
  708. return 0;
  709. }
  710. void rtnl_route_foreach_nexthop(struct rtnl_route *r,
  711. void (*cb)(struct rtnl_nexthop *, void *),
  712. void *arg)
  713. {
  714. struct rtnl_nexthop *nh;
  715. if (r->ce_mask & ROUTE_ATTR_MULTIPATH) {
  716. nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
  717. cb(nh, arg);
  718. }
  719. }
  720. }
  721. struct rtnl_nexthop *rtnl_route_nexthop_n(struct rtnl_route *r, int n)
  722. {
  723. struct rtnl_nexthop *nh;
  724. uint32_t i;
  725. if (r->ce_mask & ROUTE_ATTR_MULTIPATH && r->rt_nr_nh > n) {
  726. i = 0;
  727. nl_list_for_each_entry(nh, &r->rt_nexthops, rtnh_list) {
  728. if (i == n) return nh;
  729. i++;
  730. }
  731. }
  732. return NULL;
  733. }
  734. /** @} */
  735. /**
  736. * @name Utilities
  737. * @{
  738. */
  739. /**
  740. * Guess scope of a route object.
  741. * @arg route Route object.
  742. *
  743. * Guesses the scope of a route object, based on the following rules:
  744. * @code
  745. * 1) Local route -> local scope
  746. * 2) At least one nexthop not directly connected -> universe scope
  747. * 3) All others -> link scope
  748. * @endcode
  749. *
  750. * @return Scope value.
  751. */
  752. int rtnl_route_guess_scope(struct rtnl_route *route)
  753. {
  754. if (route->rt_type == RTN_LOCAL)
  755. return RT_SCOPE_HOST;
  756. if (!nl_list_empty(&route->rt_nexthops)) {
  757. struct rtnl_nexthop *nh;
  758. /*
  759. * Use scope uiniverse if there is at least one nexthop which
  760. * is not directly connected
  761. */
  762. nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
  763. if (nh->rtnh_gateway)
  764. return RT_SCOPE_UNIVERSE;
  765. }
  766. }
  767. return RT_SCOPE_LINK;
  768. }
  769. /** @} */
  770. static struct nla_policy route_policy[RTA_MAX+1] = {
  771. [RTA_IIF] = { .type = NLA_U32 },
  772. [RTA_OIF] = { .type = NLA_U32 },
  773. [RTA_PRIORITY] = { .type = NLA_U32 },
  774. [RTA_FLOW] = { .type = NLA_U32 },
  775. [RTA_CACHEINFO] = { .minlen = sizeof(struct rta_cacheinfo) },
  776. [RTA_METRICS] = { .type = NLA_NESTED },
  777. [RTA_MULTIPATH] = { .type = NLA_NESTED },
  778. };
  779. static int parse_multipath(struct rtnl_route *route, struct nlattr *attr)
  780. {
  781. struct rtnl_nexthop *nh = NULL;
  782. struct rtnexthop *rtnh = nla_data(attr);
  783. size_t tlen = nla_len(attr);
  784. int err;
  785. while (tlen >= sizeof(*rtnh) && tlen >= rtnh->rtnh_len) {
  786. nh = rtnl_route_nh_alloc();
  787. if (!nh)
  788. return -NLE_NOMEM;
  789. rtnl_route_nh_set_weight(nh, rtnh->rtnh_hops);
  790. rtnl_route_nh_set_ifindex(nh, rtnh->rtnh_ifindex);
  791. rtnl_route_nh_set_flags(nh, rtnh->rtnh_flags);
  792. if (rtnh->rtnh_len > sizeof(*rtnh)) {
  793. struct nlattr *ntb[RTA_MAX + 1];
  794. err = nla_parse(ntb, RTA_MAX, (struct nlattr *)
  795. RTNH_DATA(rtnh),
  796. rtnh->rtnh_len - sizeof(*rtnh),
  797. route_policy);
  798. if (err < 0)
  799. goto errout;
  800. if (ntb[RTA_GATEWAY]) {
  801. struct nl_addr *addr;
  802. addr = nl_addr_alloc_attr(ntb[RTA_GATEWAY],
  803. route->rt_family);
  804. if (!addr) {
  805. err = -NLE_NOMEM;
  806. goto errout;
  807. }
  808. rtnl_route_nh_set_gateway(nh, addr);
  809. nl_addr_put(addr);
  810. }
  811. if (ntb[RTA_FLOW]) {
  812. uint32_t realms;
  813. realms = nla_get_u32(ntb[RTA_FLOW]);
  814. rtnl_route_nh_set_realms(nh, realms);
  815. }
  816. }
  817. rtnl_route_add_nexthop(route, nh);
  818. tlen -= RTNH_ALIGN(rtnh->rtnh_len);
  819. rtnh = RTNH_NEXT(rtnh);
  820. }
  821. err = 0;
  822. errout:
  823. if (err && nh)
  824. rtnl_route_nh_free(nh);
  825. return err;
  826. }
  827. int rtnl_route_parse(struct nlmsghdr *nlh, struct rtnl_route **result)
  828. {
  829. struct rtmsg *rtm;
  830. struct rtnl_route *route;
  831. struct nlattr *tb[RTA_MAX + 1];
  832. struct nl_addr *src = NULL, *dst = NULL, *addr;
  833. struct rtnl_nexthop *old_nh = NULL;
  834. int err, family;
  835. route = rtnl_route_alloc();
  836. if (!route) {
  837. err = -NLE_NOMEM;
  838. goto errout;
  839. }
  840. route->ce_msgtype = nlh->nlmsg_type;
  841. err = nlmsg_parse(nlh, sizeof(struct rtmsg), tb, RTA_MAX, route_policy);
  842. if (err < 0)
  843. goto errout;
  844. rtm = nlmsg_data(nlh);
  845. route->rt_family = family = rtm->rtm_family;
  846. route->rt_tos = rtm->rtm_tos;
  847. route->rt_table = rtm->rtm_table;
  848. route->rt_type = rtm->rtm_type;
  849. route->rt_scope = rtm->rtm_scope;
  850. route->rt_protocol = rtm->rtm_protocol;
  851. route->rt_flags = rtm->rtm_flags;
  852. route->rt_prio = 0;
  853. route->ce_mask |= ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
  854. ROUTE_ATTR_TABLE | ROUTE_ATTR_TYPE |
  855. ROUTE_ATTR_SCOPE | ROUTE_ATTR_PROTOCOL |
  856. ROUTE_ATTR_FLAGS | ROUTE_ATTR_PRIO;
  857. if (tb[RTA_DST]) {
  858. if (!(dst = nl_addr_alloc_attr(tb[RTA_DST], family)))
  859. goto errout_nomem;
  860. } else {
  861. if (!(dst = nl_addr_alloc(0)))
  862. goto errout_nomem;
  863. nl_addr_set_family(dst, rtm->rtm_family);
  864. }
  865. nl_addr_set_prefixlen(dst, rtm->rtm_dst_len);
  866. err = rtnl_route_set_dst(route, dst);
  867. if (err < 0)
  868. goto errout;
  869. nl_addr_put(dst);
  870. if (tb[RTA_SRC]) {
  871. if (!(src = nl_addr_alloc_attr(tb[RTA_SRC], family)))
  872. goto errout_nomem;
  873. } else if (rtm->rtm_src_len)
  874. if (!(src = nl_addr_alloc(0)))
  875. goto errout_nomem;
  876. if (src) {
  877. nl_addr_set_prefixlen(src, rtm->rtm_src_len);
  878. rtnl_route_set_src(route, src);
  879. nl_addr_put(src);
  880. }
  881. if (tb[RTA_TABLE])
  882. rtnl_route_set_table(route, nla_get_u32(tb[RTA_TABLE]));
  883. if (tb[RTA_IIF])
  884. rtnl_route_set_iif(route, nla_get_u32(tb[RTA_IIF]));
  885. if (tb[RTA_PRIORITY])
  886. rtnl_route_set_priority(route, nla_get_u32(tb[RTA_PRIORITY]));
  887. if (tb[RTA_PREFSRC]) {
  888. if (!(addr = nl_addr_alloc_attr(tb[RTA_PREFSRC], family)))
  889. goto errout_nomem;
  890. rtnl_route_set_pref_src(route, addr);
  891. nl_addr_put(addr);
  892. }
  893. if (tb[RTA_METRICS]) {
  894. struct nlattr *mtb[RTAX_MAX + 1];
  895. int i;
  896. err = nla_parse_nested(mtb, RTAX_MAX, tb[RTA_METRICS], NULL);
  897. if (err < 0)
  898. goto errout;
  899. for (i = 1; i <= RTAX_MAX; i++) {
  900. if (mtb[i] && nla_len(mtb[i]) >= sizeof(uint32_t)) {
  901. uint32_t m = nla_get_u32(mtb[i]);
  902. if (rtnl_route_set_metric(route, i, m) < 0)
  903. goto errout;
  904. }
  905. }
  906. }
  907. if (tb[RTA_MULTIPATH])
  908. if ((err = parse_multipath(route, tb[RTA_MULTIPATH])) < 0)
  909. goto errout;
  910. if (tb[RTA_CACHEINFO]) {
  911. nla_memcpy(&route->rt_cacheinfo, tb[RTA_CACHEINFO],
  912. sizeof(route->rt_cacheinfo));
  913. route->ce_mask |= ROUTE_ATTR_CACHEINFO;
  914. }
  915. if (tb[RTA_OIF]) {
  916. if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
  917. goto errout;
  918. rtnl_route_nh_set_ifindex(old_nh, nla_get_u32(tb[RTA_OIF]));
  919. }
  920. if (tb[RTA_GATEWAY]) {
  921. if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
  922. goto errout;
  923. if (!(addr = nl_addr_alloc_attr(tb[RTA_GATEWAY], family)))
  924. goto errout_nomem;
  925. rtnl_route_nh_set_gateway(old_nh, addr);
  926. nl_addr_put(addr);
  927. }
  928. if (tb[RTA_FLOW]) {
  929. if (!old_nh && !(old_nh = rtnl_route_nh_alloc()))
  930. goto errout;
  931. rtnl_route_nh_set_realms(old_nh, nla_get_u32(tb[RTA_FLOW]));
  932. }
  933. if (old_nh) {
  934. rtnl_route_nh_set_flags(old_nh, rtm->rtm_flags & 0xff);
  935. if (route->rt_nr_nh == 0) {
  936. /* If no nexthops have been provided via RTA_MULTIPATH
  937. * we add it as regular nexthop to maintain backwards
  938. * compatibility */
  939. rtnl_route_add_nexthop(route, old_nh);
  940. } else {
  941. /* Kernel supports new style nexthop configuration,
  942. * verify that it is a duplicate and discard nexthop. */
  943. struct rtnl_nexthop *first;
  944. first = nl_list_first_entry(&route->rt_nexthops,
  945. struct rtnl_nexthop,
  946. rtnh_list);
  947. if (!first)
  948. BUG();
  949. if (rtnl_route_nh_compare(old_nh, first,
  950. old_nh->ce_mask, 0)) {
  951. err = -NLE_INVAL;
  952. goto errout;
  953. }
  954. rtnl_route_nh_free(old_nh);
  955. }
  956. }
  957. *result = route;
  958. return 0;
  959. errout:
  960. rtnl_route_put(route);
  961. return err;
  962. errout_nomem:
  963. err = -NLE_NOMEM;
  964. goto errout;
  965. }
  966. int rtnl_route_build_msg(struct nl_msg *msg, struct rtnl_route *route)
  967. {
  968. int i;
  969. struct nlattr *metrics;
  970. struct rtmsg rtmsg = {
  971. .rtm_family = route->rt_family,
  972. .rtm_tos = route->rt_tos,
  973. .rtm_table = route->rt_table,
  974. .rtm_protocol = route->rt_protocol,
  975. .rtm_scope = route->rt_scope,
  976. .rtm_type = route->rt_type,
  977. .rtm_flags = route->rt_flags,
  978. };
  979. if (route->rt_dst == NULL)
  980. return -NLE_MISSING_ATTR;
  981. rtmsg.rtm_dst_len = nl_addr_get_prefixlen(route->rt_dst);
  982. if (route->rt_src)
  983. rtmsg.rtm_src_len = nl_addr_get_prefixlen(route->rt_src);
  984. if (!(route->ce_mask & ROUTE_ATTR_SCOPE))
  985. rtmsg.rtm_scope = rtnl_route_guess_scope(route);
  986. if (rtnl_route_get_nnexthops(route) == 1) {
  987. struct rtnl_nexthop *nh;
  988. nh = rtnl_route_nexthop_n(route, 0);
  989. rtmsg.rtm_flags |= nh->rtnh_flags;
  990. }
  991. if (nlmsg_append(msg, &rtmsg, sizeof(rtmsg), NLMSG_ALIGNTO) < 0)
  992. goto nla_put_failure;
  993. /* Additional table attribute replacing the 8bit in the header, was
  994. * required to allow more than 256 tables. */
  995. NLA_PUT_U32(msg, RTA_TABLE, route->rt_table);
  996. if (nl_addr_get_len(route->rt_dst))
  997. NLA_PUT_ADDR(msg, RTA_DST, route->rt_dst);
  998. NLA_PUT_U32(msg, RTA_PRIORITY, route->rt_prio);
  999. if (route->ce_mask & ROUTE_ATTR_SRC)
  1000. NLA_PUT_ADDR(msg, RTA_SRC, route->rt_src);
  1001. if (route->ce_mask & ROUTE_ATTR_PREF_SRC)
  1002. NLA_PUT_ADDR(msg, RTA_PREFSRC, route->rt_pref_src);
  1003. if (route->ce_mask & ROUTE_ATTR_IIF)
  1004. NLA_PUT_U32(msg, RTA_IIF, route->rt_iif);
  1005. if (route->rt_nmetrics > 0) {
  1006. uint32_t val;
  1007. metrics = nla_nest_start(msg, RTA_METRICS);
  1008. if (metrics == NULL)
  1009. goto nla_put_failure;
  1010. for (i = 1; i <= RTAX_MAX; i++) {
  1011. if (!rtnl_route_get_metric(route, i, &val))
  1012. NLA_PUT_U32(msg, i, val);
  1013. }
  1014. nla_nest_end(msg, metrics);
  1015. }
  1016. if (rtnl_route_get_nnexthops(route) == 1) {
  1017. struct rtnl_nexthop *nh;
  1018. nh = rtnl_route_nexthop_n(route, 0);
  1019. if (nh->rtnh_gateway)
  1020. NLA_PUT_ADDR(msg, RTA_GATEWAY, nh->rtnh_gateway);
  1021. if (nh->rtnh_ifindex)
  1022. NLA_PUT_U32(msg, RTA_OIF, nh->rtnh_ifindex);
  1023. if (nh->rtnh_realms)
  1024. NLA_PUT_U32(msg, RTA_FLOW, nh->rtnh_realms);
  1025. } else if (rtnl_route_get_nnexthops(route) > 1) {
  1026. struct nlattr *multipath;
  1027. struct rtnl_nexthop *nh;
  1028. if (!(multipath = nla_nest_start(msg, RTA_MULTIPATH)))
  1029. goto nla_put_failure;
  1030. nl_list_for_each_entry(nh, &route->rt_nexthops, rtnh_list) {
  1031. struct rtnexthop *rtnh;
  1032. rtnh = nlmsg_reserve(msg, sizeof(*rtnh), NLMSG_ALIGNTO);
  1033. if (!rtnh)
  1034. goto nla_put_failure;
  1035. rtnh->rtnh_flags = nh->rtnh_flags;
  1036. rtnh->rtnh_hops = nh->rtnh_weight;
  1037. rtnh->rtnh_ifindex = nh->rtnh_ifindex;
  1038. if (nh->rtnh_gateway)
  1039. NLA_PUT_ADDR(msg, RTA_GATEWAY,
  1040. nh->rtnh_gateway);
  1041. if (nh->rtnh_realms)
  1042. NLA_PUT_U32(msg, RTA_FLOW, nh->rtnh_realms);
  1043. rtnh->rtnh_len = nlmsg_tail(msg->nm_nlh) -
  1044. (void *) rtnh;
  1045. }
  1046. nla_nest_end(msg, multipath);
  1047. }
  1048. return 0;
  1049. nla_put_failure:
  1050. return -NLE_MSGSIZE;
  1051. }
  1052. /** @cond SKIP */
  1053. struct nl_object_ops route_obj_ops = {
  1054. .oo_name = "route/route",
  1055. .oo_size = sizeof(struct rtnl_route),
  1056. .oo_constructor = route_constructor,
  1057. .oo_free_data = route_free_data,
  1058. .oo_clone = route_clone,
  1059. .oo_dump = {
  1060. [NL_DUMP_LINE] = route_dump_line,
  1061. [NL_DUMP_DETAILS] = route_dump_details,
  1062. [NL_DUMP_STATS] = route_dump_stats,
  1063. },
  1064. .oo_compare = route_compare,
  1065. .oo_keygen = route_keygen,
  1066. .oo_update = route_update,
  1067. .oo_attrs2str = route_attrs2str,
  1068. .oo_id_attrs = (ROUTE_ATTR_FAMILY | ROUTE_ATTR_TOS |
  1069. ROUTE_ATTR_TABLE | ROUTE_ATTR_DST |
  1070. ROUTE_ATTR_PRIO),
  1071. };
  1072. /** @endcond */
  1073. /** @} */