iproute.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  4. *
  5. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  6. *
  7. * Changes:
  8. *
  9. * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
  10. * Kunihiro Ishiguro <kunihiro@zebra.org> 001102: rtnh_ifindex was not initialized
  11. */
  12. #include "ip_common.h" /* #include "libbb.h" is inside */
  13. #include "common_bufsiz.h"
  14. #include "rt_names.h"
  15. #include "utils.h"
  16. #include <linux/version.h>
  17. /* RTA_TABLE is not a define, can't test with ifdef. */
  18. /* As a proxy, test which kernels toolchain expects: */
  19. #define HAVE_RTA_TABLE (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19))
  20. #ifndef RTAX_RTTVAR
  21. #define RTAX_RTTVAR RTAX_HOPS
  22. #endif
  23. struct filter_t {
  24. int tb;
  25. /* Misnomer. Does not mean "flushed something" */
  26. /* More like "flush commands were constructed by print_route()" */
  27. smallint flushed;
  28. /* Flush cmd buf. If !NULL, print_route() constructs flush commands in it */
  29. char *flushb;
  30. int flushp;
  31. int flushe;
  32. struct rtnl_handle *rth;
  33. //int protocol, protocolmask; - write-only fields?!
  34. int scope, scopemask;
  35. //int type; - read-only
  36. //int typemask; - unused
  37. //int tos, tosmask; - unused
  38. int iif;
  39. int oif;
  40. //int realm, realmmask; - unused
  41. //inet_prefix rprefsrc; - read-only
  42. inet_prefix rvia;
  43. inet_prefix rdst;
  44. inet_prefix mdst;
  45. inet_prefix rsrc;
  46. inet_prefix msrc;
  47. } FIX_ALIASING;
  48. typedef struct filter_t filter_t;
  49. #define G_filter (*(filter_t*)bb_common_bufsiz1)
  50. #define INIT_G() do { setup_common_bufsiz(); } while (0)
  51. static int flush_update(void)
  52. {
  53. if (rtnl_send_check(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
  54. bb_perror_msg("can't send flush request");
  55. return -1;
  56. }
  57. G_filter.flushp = 0;
  58. return 0;
  59. }
  60. static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM,
  61. struct nlmsghdr *n, void *arg UNUSED_PARAM)
  62. {
  63. struct rtmsg *r = NLMSG_DATA(n);
  64. int len = n->nlmsg_len;
  65. struct rtattr *tb[RTA_MAX+1];
  66. inet_prefix dst;
  67. inet_prefix src;
  68. int host_len = -1;
  69. uint32_t tid;
  70. if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
  71. fprintf(stderr, "Not a route: %08x %08x %08x\n",
  72. n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
  73. return 0;
  74. }
  75. if (G_filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
  76. return 0;
  77. len -= NLMSG_LENGTH(sizeof(*r));
  78. if (len < 0)
  79. bb_error_msg_and_die("wrong nlmsg len %d", len);
  80. //memset(tb, 0, sizeof(tb)); - parse_rtattr does this
  81. parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
  82. #if HAVE_RTA_TABLE
  83. if (tb[RTA_TABLE])
  84. tid = *(uint32_t *)RTA_DATA(tb[RTA_TABLE]);
  85. else
  86. #endif
  87. tid = r->rtm_table;
  88. if (r->rtm_family == AF_INET6)
  89. host_len = 128;
  90. else if (r->rtm_family == AF_INET)
  91. host_len = 32;
  92. if (r->rtm_family == AF_INET6) {
  93. if (G_filter.tb) {
  94. if (G_filter.tb < 0) {
  95. if (!(r->rtm_flags & RTM_F_CLONED)) {
  96. return 0;
  97. }
  98. } else {
  99. if (r->rtm_flags & RTM_F_CLONED) {
  100. return 0;
  101. }
  102. if (G_filter.tb == RT_TABLE_LOCAL) {
  103. if (r->rtm_type != RTN_LOCAL) {
  104. return 0;
  105. }
  106. } else if (G_filter.tb == RT_TABLE_MAIN) {
  107. if (r->rtm_type == RTN_LOCAL) {
  108. return 0;
  109. }
  110. } else {
  111. return 0;
  112. }
  113. }
  114. }
  115. } else {
  116. if (G_filter.tb > 0 && G_filter.tb != tid) {
  117. return 0;
  118. }
  119. }
  120. if ((G_filter.scope ^ r->rtm_scope) & G_filter.scopemask)
  121. return 0;
  122. if (G_filter.rdst.family
  123. && (r->rtm_family != G_filter.rdst.family || G_filter.rdst.bitlen > r->rtm_dst_len)
  124. ) {
  125. return 0;
  126. }
  127. if (G_filter.mdst.family
  128. && (r->rtm_family != G_filter.mdst.family
  129. || (G_filter.mdst.bitlen >= 0 && G_filter.mdst.bitlen < r->rtm_dst_len)
  130. )
  131. ) {
  132. return 0;
  133. }
  134. if (G_filter.rsrc.family
  135. && (r->rtm_family != G_filter.rsrc.family || G_filter.rsrc.bitlen > r->rtm_src_len)
  136. ) {
  137. return 0;
  138. }
  139. if (G_filter.msrc.family
  140. && (r->rtm_family != G_filter.msrc.family
  141. || (G_filter.msrc.bitlen >= 0 && G_filter.msrc.bitlen < r->rtm_src_len)
  142. )
  143. ) {
  144. return 0;
  145. }
  146. memset(&src, 0, sizeof(src));
  147. memset(&dst, 0, sizeof(dst));
  148. if (tb[RTA_SRC]) {
  149. src.bitlen = r->rtm_src_len;
  150. src.bytelen = (r->rtm_family == AF_INET6 ? 16 : 4);
  151. memcpy(src.data, RTA_DATA(tb[RTA_SRC]), src.bytelen);
  152. }
  153. if (tb[RTA_DST]) {
  154. dst.bitlen = r->rtm_dst_len;
  155. dst.bytelen = (r->rtm_family == AF_INET6 ? 16 : 4);
  156. memcpy(dst.data, RTA_DATA(tb[RTA_DST]), dst.bytelen);
  157. }
  158. if (G_filter.rdst.family
  159. && inet_addr_match(&dst, &G_filter.rdst, G_filter.rdst.bitlen)
  160. ) {
  161. return 0;
  162. }
  163. if (G_filter.mdst.family
  164. && G_filter.mdst.bitlen >= 0
  165. && inet_addr_match(&dst, &G_filter.mdst, r->rtm_dst_len)
  166. ) {
  167. return 0;
  168. }
  169. if (G_filter.rsrc.family
  170. && inet_addr_match(&src, &G_filter.rsrc, G_filter.rsrc.bitlen)
  171. ) {
  172. return 0;
  173. }
  174. if (G_filter.msrc.family && G_filter.msrc.bitlen >= 0
  175. && inet_addr_match(&src, &G_filter.msrc, r->rtm_src_len)
  176. ) {
  177. return 0;
  178. }
  179. if (G_filter.oif != 0) {
  180. if (!tb[RTA_OIF])
  181. return 0;
  182. if (G_filter.oif != *(int*)RTA_DATA(tb[RTA_OIF]))
  183. return 0;
  184. }
  185. if (G_filter.flushb) {
  186. struct nlmsghdr *fn;
  187. /* We are creating route flush commands */
  188. if (r->rtm_family == AF_INET6
  189. && r->rtm_dst_len == 0
  190. && r->rtm_type == RTN_UNREACHABLE
  191. && tb[RTA_PRIORITY]
  192. && *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1
  193. ) {
  194. return 0;
  195. }
  196. if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
  197. if (flush_update())
  198. xfunc_die();
  199. }
  200. fn = (void*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
  201. memcpy(fn, n, n->nlmsg_len);
  202. fn->nlmsg_type = RTM_DELROUTE;
  203. fn->nlmsg_flags = NLM_F_REQUEST;
  204. fn->nlmsg_seq = ++G_filter.rth->seq;
  205. G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb;
  206. G_filter.flushed = 1;
  207. return 0;
  208. }
  209. /* We are printing routes */
  210. if (n->nlmsg_type == RTM_DELROUTE) {
  211. printf("Deleted ");
  212. }
  213. if (r->rtm_type != RTN_UNICAST /* && !G_filter.type - always 0 */) {
  214. printf("%s ", rtnl_rtntype_n2a(r->rtm_type));
  215. }
  216. if (tb[RTA_DST]) {
  217. if (r->rtm_dst_len != host_len) {
  218. printf("%s/%u ",
  219. rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_DST])),
  220. r->rtm_dst_len
  221. );
  222. } else {
  223. printf("%s ", format_host(r->rtm_family,
  224. RTA_PAYLOAD(tb[RTA_DST]),
  225. RTA_DATA(tb[RTA_DST]))
  226. );
  227. }
  228. } else if (r->rtm_dst_len) {
  229. printf("0/%d ", r->rtm_dst_len);
  230. } else {
  231. printf("default ");
  232. }
  233. if (tb[RTA_SRC]) {
  234. if (r->rtm_src_len != host_len) {
  235. printf("from %s/%u ",
  236. rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_SRC])),
  237. r->rtm_src_len
  238. );
  239. } else {
  240. printf("from %s ", format_host(r->rtm_family,
  241. RTA_PAYLOAD(tb[RTA_SRC]),
  242. RTA_DATA(tb[RTA_SRC]))
  243. );
  244. }
  245. } else if (r->rtm_src_len) {
  246. printf("from 0/%u ", r->rtm_src_len);
  247. }
  248. if (tb[RTA_GATEWAY] && G_filter.rvia.bitlen != host_len) {
  249. printf("via %s ", format_host(r->rtm_family,
  250. RTA_PAYLOAD(tb[RTA_GATEWAY]),
  251. RTA_DATA(tb[RTA_GATEWAY]))
  252. );
  253. }
  254. if (tb[RTA_OIF]) {
  255. printf("dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
  256. }
  257. #if ENABLE_FEATURE_IP_RULE
  258. if (tid && tid != RT_TABLE_MAIN && !G_filter.tb)
  259. printf("table %s ", rtnl_rttable_n2a(tid));
  260. #endif
  261. /* Todo: parse & show "proto kernel" here */
  262. if (!(r->rtm_flags & RTM_F_CLONED)) {
  263. if ((r->rtm_scope != RT_SCOPE_UNIVERSE) && G_filter.scopemask != -1)
  264. printf("scope %s ", rtnl_rtscope_n2a(r->rtm_scope));
  265. }
  266. if (tb[RTA_PREFSRC] && /*G_filter.rprefsrc.bitlen - always 0*/ 0 != host_len) {
  267. /* Do not use format_host(). It is our local addr
  268. and symbolic name will not be useful.
  269. */
  270. printf(" src %s ", rt_addr_n2a(r->rtm_family,
  271. RTA_DATA(tb[RTA_PREFSRC])));
  272. }
  273. if (tb[RTA_PRIORITY]) {
  274. printf(" metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
  275. }
  276. if (r->rtm_flags & RTNH_F_DEAD) {
  277. printf("dead ");
  278. }
  279. if (r->rtm_flags & RTNH_F_ONLINK) {
  280. printf("onlink ");
  281. }
  282. if (r->rtm_flags & RTNH_F_PERVASIVE) {
  283. printf("pervasive ");
  284. }
  285. if (r->rtm_flags & RTM_F_NOTIFY) {
  286. printf("notify ");
  287. }
  288. if (r->rtm_family == AF_INET6) {
  289. struct rta_cacheinfo *ci = NULL;
  290. if (tb[RTA_CACHEINFO]) {
  291. ci = RTA_DATA(tb[RTA_CACHEINFO]);
  292. }
  293. if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
  294. if (r->rtm_flags & RTM_F_CLONED) {
  295. printf("%c cache ", _SL_);
  296. }
  297. if (ci->rta_expires) {
  298. printf(" expires %dsec", ci->rta_expires / get_hz());
  299. }
  300. if (ci->rta_error != 0) {
  301. printf(" error %d", ci->rta_error);
  302. }
  303. } else if (ci) {
  304. if (ci->rta_error != 0)
  305. printf(" error %d", ci->rta_error);
  306. }
  307. }
  308. if (tb[RTA_IIF] && G_filter.iif == 0) {
  309. printf(" iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
  310. }
  311. bb_putchar('\n');
  312. return 0;
  313. }
  314. static int str_is_lock(const char *str)
  315. {
  316. return strcmp(str, "lock") == 0;
  317. }
  318. /* Return value becomes exitcode. It's okay to not return at all */
  319. static int iproute_modify(int cmd, unsigned flags, char **argv)
  320. {
  321. /* If you add stuff here, update iproute_full_usage */
  322. static const char keywords[] ALIGN1 =
  323. "src\0""via\0"
  324. "mtu\0""advmss\0"
  325. "scope\0""protocol\0"IF_FEATURE_IP_RULE("table\0")
  326. "dev\0""oif\0""to\0""metric\0""onlink\0";
  327. #define keyword_via (keywords + sizeof("src"))
  328. #define keyword_mtu (keyword_via + sizeof("via"))
  329. #define keyword_advmss (keyword_mtu + sizeof("mtu"))
  330. #define keyword_scope (keyword_advmss + sizeof("advmss"))
  331. #define keyword_proto (keyword_scope + sizeof("scope"))
  332. #define keyword_table (keyword_proto + sizeof("protocol"))
  333. enum {
  334. ARG_src,
  335. ARG_via,
  336. ARG_mtu,
  337. ARG_advmss,
  338. ARG_scope,
  339. ARG_protocol,
  340. IF_FEATURE_IP_RULE(ARG_table,)
  341. ARG_dev,
  342. ARG_oif,
  343. ARG_to,
  344. ARG_metric,
  345. ARG_onlink,
  346. };
  347. enum {
  348. gw_ok = 1 << 0,
  349. dst_ok = 1 << 1,
  350. proto_ok = 1 << 2,
  351. type_ok = 1 << 3
  352. };
  353. struct rtnl_handle rth;
  354. struct {
  355. struct nlmsghdr n;
  356. struct rtmsg r;
  357. char buf[1024];
  358. } req;
  359. char mxbuf[256];
  360. struct rtattr * mxrta = (void*)mxbuf;
  361. unsigned mxlock = 0;
  362. char *d = NULL;
  363. smalluint ok = 0;
  364. smalluint scope_ok = 0;
  365. int arg;
  366. memset(&req, 0, sizeof(req));
  367. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
  368. req.n.nlmsg_flags = NLM_F_REQUEST | flags;
  369. req.n.nlmsg_type = cmd;
  370. req.r.rtm_family = preferred_family;
  371. if (RT_TABLE_MAIN != 0) /* if it is zero, memset already did it */
  372. req.r.rtm_table = RT_TABLE_MAIN;
  373. if (RT_SCOPE_NOWHERE != 0)
  374. req.r.rtm_scope = RT_SCOPE_NOWHERE;
  375. if (cmd != RTM_DELROUTE) {
  376. req.r.rtm_scope = RT_SCOPE_UNIVERSE;
  377. if (RTPROT_BOOT != 0)
  378. req.r.rtm_protocol = RTPROT_BOOT;
  379. if (RTN_UNICAST != 0)
  380. req.r.rtm_type = RTN_UNICAST;
  381. }
  382. mxrta->rta_type = RTA_METRICS;
  383. mxrta->rta_len = RTA_LENGTH(0);
  384. while (*argv) {
  385. arg = index_in_substrings(keywords, *argv);
  386. if (arg == ARG_src) {
  387. inet_prefix addr;
  388. NEXT_ARG();
  389. get_addr(&addr, *argv, req.r.rtm_family);
  390. if (req.r.rtm_family == AF_UNSPEC)
  391. req.r.rtm_family = addr.family;
  392. addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
  393. } else if (arg == ARG_via) {
  394. inet_prefix addr;
  395. ok |= gw_ok;
  396. NEXT_ARG();
  397. get_addr(&addr, *argv, req.r.rtm_family);
  398. if (req.r.rtm_family == AF_UNSPEC) {
  399. req.r.rtm_family = addr.family;
  400. }
  401. addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
  402. } else if (arg == ARG_mtu) {
  403. unsigned mtu;
  404. NEXT_ARG();
  405. if (str_is_lock(*argv)) {
  406. mxlock |= (1 << RTAX_MTU);
  407. NEXT_ARG();
  408. }
  409. mtu = get_unsigned(*argv, keyword_mtu);
  410. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
  411. } else if (arg == ARG_advmss) {
  412. unsigned mss;
  413. NEXT_ARG();
  414. if (str_is_lock(*argv)) {
  415. mxlock |= (1 << RTAX_ADVMSS);
  416. NEXT_ARG();
  417. }
  418. mss = get_unsigned(*argv, keyword_advmss);
  419. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_ADVMSS, mss);
  420. } else if (arg == ARG_scope) {
  421. uint32_t scope;
  422. NEXT_ARG();
  423. if (rtnl_rtscope_a2n(&scope, *argv))
  424. invarg_1_to_2(*argv, keyword_scope);
  425. req.r.rtm_scope = scope;
  426. scope_ok = 1;
  427. } else if (arg == ARG_protocol) {
  428. uint32_t prot;
  429. NEXT_ARG();
  430. if (rtnl_rtprot_a2n(&prot, *argv))
  431. invarg_1_to_2(*argv, keyword_proto);
  432. req.r.rtm_protocol = prot;
  433. ok |= proto_ok;
  434. #if ENABLE_FEATURE_IP_RULE
  435. } else if (arg == ARG_table) {
  436. uint32_t tid;
  437. NEXT_ARG();
  438. if (rtnl_rttable_a2n(&tid, *argv))
  439. invarg_1_to_2(*argv, keyword_table);
  440. #if HAVE_RTA_TABLE
  441. if (tid > 255) {
  442. req.r.rtm_table = RT_TABLE_UNSPEC;
  443. addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
  444. } else
  445. #endif
  446. req.r.rtm_table = tid;
  447. #endif
  448. } else if (arg == ARG_dev || arg == ARG_oif) {
  449. NEXT_ARG();
  450. d = *argv;
  451. } else if (arg == ARG_metric) {
  452. //TODO: "metric", "priority" and "preference" are synonyms
  453. uint32_t metric;
  454. NEXT_ARG();
  455. metric = get_u32(*argv, "metric");
  456. addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric);
  457. } else if (arg == ARG_onlink) {
  458. req.r.rtm_flags |= RTNH_F_ONLINK;
  459. } else {
  460. int type;
  461. inet_prefix dst;
  462. if (arg == ARG_to) {
  463. NEXT_ARG();
  464. }
  465. if ((**argv < '0' || **argv > '9')
  466. && rtnl_rtntype_a2n(&type, *argv) == 0
  467. ) {
  468. NEXT_ARG();
  469. req.r.rtm_type = type;
  470. ok |= type_ok;
  471. }
  472. if (ok & dst_ok) {
  473. duparg2("to", *argv);
  474. }
  475. get_prefix(&dst, *argv, req.r.rtm_family);
  476. if (req.r.rtm_family == AF_UNSPEC) {
  477. req.r.rtm_family = dst.family;
  478. }
  479. req.r.rtm_dst_len = dst.bitlen;
  480. ok |= dst_ok;
  481. if (dst.bytelen) {
  482. addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
  483. }
  484. }
  485. /* Other keywords recognized by iproute2-3.19.0: */
  486. #if 0
  487. } else if (strcmp(*argv, "from") == 0) {
  488. inet_prefix addr;
  489. NEXT_ARG();
  490. get_prefix(&addr, *argv, req.r.rtm_family);
  491. if (req.r.rtm_family == AF_UNSPEC)
  492. req.r.rtm_family = addr.family;
  493. if (addr.bytelen)
  494. addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
  495. req.r.rtm_src_len = addr.bitlen;
  496. } else if (strcmp(*argv, "tos") == 0 ||
  497. matches(*argv, "dsfield") == 0) {
  498. __u32 tos;
  499. NEXT_ARG();
  500. if (rtnl_dsfield_a2n(&tos, *argv))
  501. invarg("\"tos\" value is invalid\n", *argv);
  502. req.r.rtm_tos = tos;
  503. } else if (strcmp(*argv, "hoplimit") == 0) {
  504. unsigned hoplimit;
  505. NEXT_ARG();
  506. if (strcmp(*argv, "lock") == 0) {
  507. mxlock |= (1<<RTAX_HOPLIMIT);
  508. NEXT_ARG();
  509. }
  510. if (get_unsigned(&hoplimit, *argv, 0))
  511. invarg("\"hoplimit\" value is invalid\n", *argv);
  512. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_HOPLIMIT, hoplimit);
  513. } else if (matches(*argv, "reordering") == 0) {
  514. unsigned reord;
  515. NEXT_ARG();
  516. if (strcmp(*argv, "lock") == 0) {
  517. mxlock |= (1<<RTAX_REORDERING);
  518. NEXT_ARG();
  519. }
  520. if (get_unsigned(&reord, *argv, 0))
  521. invarg("\"reordering\" value is invalid\n", *argv);
  522. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_REORDERING, reord);
  523. } else if (strcmp(*argv, "rtt") == 0) {
  524. unsigned rtt;
  525. NEXT_ARG();
  526. if (strcmp(*argv, "lock") == 0) {
  527. mxlock |= (1<<RTAX_RTT);
  528. NEXT_ARG();
  529. }
  530. if (get_time_rtt(&rtt, *argv, &raw))
  531. invarg("\"rtt\" value is invalid\n", *argv);
  532. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTT,
  533. (raw) ? rtt : rtt * 8);
  534. } else if (strcmp(*argv, "rto_min") == 0) {
  535. unsigned rto_min;
  536. NEXT_ARG();
  537. mxlock |= (1<<RTAX_RTO_MIN);
  538. if (get_time_rtt(&rto_min, *argv, &raw))
  539. invarg("\"rto_min\" value is invalid\n",
  540. *argv);
  541. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTO_MIN,
  542. rto_min);
  543. } else if (matches(*argv, "window") == 0) {
  544. unsigned win;
  545. NEXT_ARG();
  546. if (strcmp(*argv, "lock") == 0) {
  547. mxlock |= (1<<RTAX_WINDOW);
  548. NEXT_ARG();
  549. }
  550. if (get_unsigned(&win, *argv, 0))
  551. invarg("\"window\" value is invalid\n", *argv);
  552. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_WINDOW, win);
  553. } else if (matches(*argv, "cwnd") == 0) {
  554. unsigned win;
  555. NEXT_ARG();
  556. if (strcmp(*argv, "lock") == 0) {
  557. mxlock |= (1<<RTAX_CWND);
  558. NEXT_ARG();
  559. }
  560. if (get_unsigned(&win, *argv, 0))
  561. invarg("\"cwnd\" value is invalid\n", *argv);
  562. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_CWND, win);
  563. } else if (matches(*argv, "initcwnd") == 0) {
  564. unsigned win;
  565. NEXT_ARG();
  566. if (strcmp(*argv, "lock") == 0) {
  567. mxlock |= (1<<RTAX_INITCWND);
  568. NEXT_ARG();
  569. }
  570. if (get_unsigned(&win, *argv, 0))
  571. invarg("\"initcwnd\" value is invalid\n", *argv);
  572. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_INITCWND, win);
  573. } else if (matches(*argv, "initrwnd") == 0) {
  574. unsigned win;
  575. NEXT_ARG();
  576. if (strcmp(*argv, "lock") == 0) {
  577. mxlock |= (1<<RTAX_INITRWND);
  578. NEXT_ARG();
  579. }
  580. if (get_unsigned(&win, *argv, 0))
  581. invarg("\"initrwnd\" value is invalid\n", *argv);
  582. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_INITRWND, win);
  583. } else if (matches(*argv, "features") == 0) {
  584. unsigned int features = 0;
  585. while (argc > 0) {
  586. NEXT_ARG();
  587. if (strcmp(*argv, "ecn") == 0)
  588. features |= RTAX_FEATURE_ECN;
  589. else
  590. invarg("\"features\" value not valid\n", *argv);
  591. break;
  592. }
  593. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_FEATURES, features);
  594. } else if (matches(*argv, "quickack") == 0) {
  595. unsigned quickack;
  596. NEXT_ARG();
  597. if (get_unsigned(&quickack, *argv, 0))
  598. invarg("\"quickack\" value is invalid\n", *argv);
  599. if (quickack != 1 && quickack != 0)
  600. invarg("\"quickack\" value should be 0 or 1\n", *argv);
  601. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_QUICKACK, quickack);
  602. } else if (matches(*argv, "rttvar") == 0) {
  603. unsigned win;
  604. NEXT_ARG();
  605. if (strcmp(*argv, "lock") == 0) {
  606. mxlock |= (1<<RTAX_RTTVAR);
  607. NEXT_ARG();
  608. }
  609. if (get_time_rtt(&win, *argv, &raw))
  610. invarg("\"rttvar\" value is invalid\n", *argv);
  611. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTTVAR,
  612. (raw) ? win : win * 4);
  613. } else if (matches(*argv, "ssthresh") == 0) {
  614. unsigned win;
  615. NEXT_ARG();
  616. if (strcmp(*argv, "lock") == 0) {
  617. mxlock |= (1<<RTAX_SSTHRESH);
  618. NEXT_ARG();
  619. }
  620. if (get_unsigned(&win, *argv, 0))
  621. invarg("\"ssthresh\" value is invalid\n", *argv);
  622. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_SSTHRESH, win);
  623. } else if (matches(*argv, "realms") == 0) {
  624. __u32 realm;
  625. NEXT_ARG();
  626. if (get_rt_realms(&realm, *argv))
  627. invarg("\"realm\" value is invalid\n", *argv);
  628. addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
  629. } else if (strcmp(*argv, "nexthop") == 0) {
  630. nhs_ok = 1;
  631. break;
  632. }
  633. #endif
  634. argv++;
  635. }
  636. xrtnl_open(&rth);
  637. if (d) {
  638. int idx;
  639. ll_init_map(&rth);
  640. if (d) {
  641. idx = xll_name_to_index(d);
  642. addattr32(&req.n, sizeof(req), RTA_OIF, idx);
  643. }
  644. }
  645. if (mxrta->rta_len > RTA_LENGTH(0)) {
  646. if (mxlock) {
  647. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
  648. }
  649. addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
  650. }
  651. if (!scope_ok) {
  652. if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT)
  653. req.r.rtm_scope = RT_SCOPE_HOST;
  654. else
  655. if (req.r.rtm_type == RTN_BROADCAST
  656. || req.r.rtm_type == RTN_MULTICAST
  657. || req.r.rtm_type == RTN_ANYCAST
  658. ) {
  659. req.r.rtm_scope = RT_SCOPE_LINK;
  660. }
  661. else if (req.r.rtm_type == RTN_UNICAST || req.r.rtm_type == RTN_UNSPEC) {
  662. if (cmd == RTM_DELROUTE)
  663. req.r.rtm_scope = RT_SCOPE_NOWHERE;
  664. else if (!(ok & gw_ok))
  665. req.r.rtm_scope = RT_SCOPE_LINK;
  666. }
  667. }
  668. if (req.r.rtm_family == AF_UNSPEC) {
  669. req.r.rtm_family = AF_INET;
  670. }
  671. if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
  672. return 2;
  673. }
  674. return 0;
  675. }
  676. static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
  677. {
  678. struct {
  679. struct nlmsghdr nlh;
  680. struct rtmsg rtm;
  681. } req;
  682. struct sockaddr_nl nladdr;
  683. memset(&nladdr, 0, sizeof(nladdr));
  684. memset(&req, 0, sizeof(req));
  685. nladdr.nl_family = AF_NETLINK;
  686. req.nlh.nlmsg_len = sizeof(req);
  687. if (RTM_GETROUTE)
  688. req.nlh.nlmsg_type = RTM_GETROUTE;
  689. if (NLM_F_ROOT | NLM_F_REQUEST)
  690. req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
  691. /*req.nlh.nlmsg_pid = 0; - memset did it already */
  692. req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
  693. req.rtm.rtm_family = family;
  694. if (RTM_F_CLONED)
  695. req.rtm.rtm_flags = RTM_F_CLONED;
  696. return xsendto(rth->fd, (void*)&req, sizeof(req), (struct sockaddr*)&nladdr, sizeof(nladdr));
  697. }
  698. static void iproute_flush_cache(void)
  699. {
  700. static const char fn[] ALIGN1 = "/proc/sys/net/ipv4/route/flush";
  701. int flush_fd = open_or_warn(fn, O_WRONLY);
  702. if (flush_fd < 0) {
  703. return;
  704. }
  705. if (write(flush_fd, "-1", 2) < 2) {
  706. bb_perror_msg("can't flush routing cache");
  707. return;
  708. }
  709. close(flush_fd);
  710. }
  711. static void iproute_reset_filter(void)
  712. {
  713. memset(&G_filter, 0, sizeof(G_filter));
  714. G_filter.mdst.bitlen = -1;
  715. G_filter.msrc.bitlen = -1;
  716. }
  717. /* Return value becomes exitcode. It's okay to not return at all */
  718. static int iproute_list_or_flush(char **argv, int flush)
  719. {
  720. int do_ipv6 = preferred_family;
  721. struct rtnl_handle rth;
  722. char *id = NULL;
  723. char *od = NULL;
  724. static const char keywords[] ALIGN1 =
  725. /* If you add stuff here, update iproute_full_usage */
  726. /* "ip route list/flush" parameters: */
  727. "protocol\0" "dev\0" "oif\0" "iif\0"
  728. "via\0" "table\0" "cache\0"
  729. "from\0" "to\0" "scope\0"
  730. /* and possible further keywords */
  731. "all\0"
  732. "root\0"
  733. "match\0"
  734. "exact\0"
  735. "main\0"
  736. ;
  737. enum {
  738. KW_proto, KW_dev, KW_oif, KW_iif,
  739. KW_via, KW_table, KW_cache,
  740. KW_from, KW_to, KW_scope,
  741. /* */
  742. KW_all,
  743. KW_root,
  744. KW_match,
  745. KW_exact,
  746. KW_main,
  747. };
  748. int arg, parm;
  749. iproute_reset_filter();
  750. G_filter.tb = RT_TABLE_MAIN;
  751. if (flush && !*argv)
  752. bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\"");
  753. while (*argv) {
  754. arg = index_in_substrings(keywords, *argv);
  755. if (arg == KW_proto) {
  756. uint32_t prot = 0;
  757. NEXT_ARG();
  758. //G_filter.protocolmask = -1;
  759. if (rtnl_rtprot_a2n(&prot, *argv)) {
  760. if (index_in_strings(keywords, *argv) != KW_all)
  761. invarg_1_to_2(*argv, "protocol");
  762. prot = 0;
  763. //G_filter.protocolmask = 0;
  764. }
  765. //G_filter.protocol = prot;
  766. } else if (arg == KW_dev || arg == KW_oif) {
  767. NEXT_ARG();
  768. od = *argv;
  769. } else if (arg == KW_iif) {
  770. NEXT_ARG();
  771. id = *argv;
  772. } else if (arg == KW_via) {
  773. NEXT_ARG();
  774. get_prefix(&G_filter.rvia, *argv, do_ipv6);
  775. } else if (arg == KW_table) { /* table all/cache/main */
  776. NEXT_ARG();
  777. parm = index_in_substrings(keywords, *argv);
  778. if (parm == KW_cache)
  779. G_filter.tb = -1;
  780. else if (parm == KW_all)
  781. G_filter.tb = 0;
  782. else if (parm != KW_main) {
  783. #if ENABLE_FEATURE_IP_RULE
  784. uint32_t tid;
  785. if (rtnl_rttable_a2n(&tid, *argv))
  786. invarg_1_to_2(*argv, "table");
  787. G_filter.tb = tid;
  788. #else
  789. invarg_1_to_2(*argv, "table");
  790. #endif
  791. }
  792. } else if (arg == KW_cache) {
  793. /* The command 'ip route flush cache' is used by OpenSWAN.
  794. * Assuming it's a synonym for 'ip route flush table cache' */
  795. G_filter.tb = -1;
  796. } else if (arg == KW_scope) {
  797. uint32_t scope;
  798. NEXT_ARG();
  799. G_filter.scopemask = -1;
  800. if (rtnl_rtscope_a2n(&scope, *argv)) {
  801. if (strcmp(*argv, "all") != 0)
  802. invarg_1_to_2(*argv, "scope");
  803. scope = RT_SCOPE_NOWHERE;
  804. G_filter.scopemask = 0;
  805. }
  806. G_filter.scope = scope;
  807. } else if (arg == KW_from) {
  808. NEXT_ARG();
  809. parm = index_in_substrings(keywords, *argv);
  810. if (parm == KW_root) {
  811. NEXT_ARG();
  812. get_prefix(&G_filter.rsrc, *argv, do_ipv6);
  813. } else if (parm == KW_match) {
  814. NEXT_ARG();
  815. get_prefix(&G_filter.msrc, *argv, do_ipv6);
  816. } else {
  817. if (parm == KW_exact)
  818. NEXT_ARG();
  819. get_prefix(&G_filter.msrc, *argv, do_ipv6);
  820. G_filter.rsrc = G_filter.msrc;
  821. }
  822. } else { /* "to" is the default parameter */
  823. if (arg == KW_to) {
  824. NEXT_ARG();
  825. arg = index_in_substrings(keywords, *argv);
  826. }
  827. /* parm = arg; - would be more plausible, but we reuse 'arg' here */
  828. if (arg == KW_root) {
  829. NEXT_ARG();
  830. get_prefix(&G_filter.rdst, *argv, do_ipv6);
  831. } else if (arg == KW_match) {
  832. NEXT_ARG();
  833. get_prefix(&G_filter.mdst, *argv, do_ipv6);
  834. } else { /* "to exact" is the default */
  835. if (arg == KW_exact)
  836. NEXT_ARG();
  837. get_prefix(&G_filter.mdst, *argv, do_ipv6);
  838. G_filter.rdst = G_filter.mdst;
  839. }
  840. }
  841. argv++;
  842. }
  843. if (do_ipv6 == AF_UNSPEC && G_filter.tb) {
  844. do_ipv6 = AF_INET;
  845. }
  846. xrtnl_open(&rth);
  847. ll_init_map(&rth);
  848. if (id || od) {
  849. int idx;
  850. if (id) {
  851. idx = xll_name_to_index(id);
  852. G_filter.iif = idx;
  853. }
  854. if (od) {
  855. idx = xll_name_to_index(od);
  856. G_filter.oif = idx;
  857. }
  858. }
  859. if (flush) {
  860. char flushb[4096-512];
  861. if (G_filter.tb == -1) { /* "flush table cache" */
  862. if (do_ipv6 != AF_INET6)
  863. iproute_flush_cache();
  864. if (do_ipv6 == AF_INET)
  865. return 0;
  866. }
  867. G_filter.flushb = flushb;
  868. G_filter.flushp = 0;
  869. G_filter.flushe = sizeof(flushb);
  870. G_filter.rth = &rth;
  871. for (;;) {
  872. xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
  873. G_filter.flushed = 0;
  874. xrtnl_dump_filter(&rth, print_route, NULL);
  875. if (G_filter.flushed == 0)
  876. return 0;
  877. if (flush_update())
  878. return 1;
  879. }
  880. }
  881. if (G_filter.tb != -1) {
  882. xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
  883. } else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
  884. bb_perror_msg_and_die("can't send dump request");
  885. }
  886. xrtnl_dump_filter(&rth, print_route, NULL);
  887. return 0;
  888. }
  889. /* Return value becomes exitcode. It's okay to not return at all */
  890. static int iproute_get(char **argv)
  891. {
  892. struct rtnl_handle rth;
  893. struct {
  894. struct nlmsghdr n;
  895. struct rtmsg r;
  896. char buf[1024];
  897. } req;
  898. char *idev = NULL;
  899. char *odev = NULL;
  900. bool connected = 0;
  901. bool from_ok = 0;
  902. static const char options[] ALIGN1 =
  903. "from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0";
  904. memset(&req, 0, sizeof(req));
  905. iproute_reset_filter();
  906. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
  907. if (NLM_F_REQUEST)
  908. req.n.nlmsg_flags = NLM_F_REQUEST;
  909. if (RTM_GETROUTE)
  910. req.n.nlmsg_type = RTM_GETROUTE;
  911. req.r.rtm_family = preferred_family;
  912. /*req.r.rtm_table = 0; - memset did this already */
  913. /*req.r.rtm_protocol = 0;*/
  914. /*req.r.rtm_scope = 0;*/
  915. /*req.r.rtm_type = 0;*/
  916. /*req.r.rtm_src_len = 0;*/
  917. /*req.r.rtm_dst_len = 0;*/
  918. /*req.r.rtm_tos = 0;*/
  919. while (*argv) {
  920. switch (index_in_strings(options, *argv)) {
  921. case 0: /* from */
  922. {
  923. inet_prefix addr;
  924. NEXT_ARG();
  925. from_ok = 1;
  926. get_prefix(&addr, *argv, req.r.rtm_family);
  927. if (req.r.rtm_family == AF_UNSPEC) {
  928. req.r.rtm_family = addr.family;
  929. }
  930. if (addr.bytelen) {
  931. addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
  932. }
  933. req.r.rtm_src_len = addr.bitlen;
  934. break;
  935. }
  936. case 1: /* iif */
  937. NEXT_ARG();
  938. idev = *argv;
  939. break;
  940. case 2: /* oif */
  941. case 3: /* dev */
  942. NEXT_ARG();
  943. odev = *argv;
  944. break;
  945. case 4: /* notify */
  946. req.r.rtm_flags |= RTM_F_NOTIFY;
  947. break;
  948. case 5: /* connected */
  949. connected = 1;
  950. break;
  951. case 6: /* to */
  952. NEXT_ARG();
  953. default:
  954. {
  955. inet_prefix addr;
  956. get_prefix(&addr, *argv, req.r.rtm_family);
  957. if (req.r.rtm_family == AF_UNSPEC) {
  958. req.r.rtm_family = addr.family;
  959. }
  960. if (addr.bytelen) {
  961. addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
  962. }
  963. req.r.rtm_dst_len = addr.bitlen;
  964. }
  965. }
  966. argv++;
  967. }
  968. if (req.r.rtm_dst_len == 0) {
  969. bb_error_msg_and_die("need at least destination address");
  970. }
  971. xrtnl_open(&rth);
  972. ll_init_map(&rth);
  973. if (idev || odev) {
  974. int idx;
  975. if (idev) {
  976. idx = xll_name_to_index(idev);
  977. addattr32(&req.n, sizeof(req), RTA_IIF, idx);
  978. }
  979. if (odev) {
  980. idx = xll_name_to_index(odev);
  981. addattr32(&req.n, sizeof(req), RTA_OIF, idx);
  982. }
  983. }
  984. if (req.r.rtm_family == AF_UNSPEC) {
  985. req.r.rtm_family = AF_INET;
  986. }
  987. if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
  988. return 2;
  989. }
  990. if (connected && !from_ok) {
  991. struct rtmsg *r = NLMSG_DATA(&req.n);
  992. int len = req.n.nlmsg_len;
  993. struct rtattr * tb[RTA_MAX+1];
  994. print_route(NULL, &req.n, NULL);
  995. if (req.n.nlmsg_type != RTM_NEWROUTE) {
  996. bb_error_msg_and_die("not a route?");
  997. }
  998. len -= NLMSG_LENGTH(sizeof(*r));
  999. if (len < 0) {
  1000. bb_error_msg_and_die("wrong len %d", len);
  1001. }
  1002. //memset(tb, 0, sizeof(tb)); - parse_rtattr does this
  1003. parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
  1004. if (tb[RTA_PREFSRC]) {
  1005. tb[RTA_PREFSRC]->rta_type = RTA_SRC;
  1006. r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
  1007. } else if (!tb[RTA_SRC]) {
  1008. bb_error_msg_and_die("can't connect the route");
  1009. }
  1010. if (!odev && tb[RTA_OIF]) {
  1011. tb[RTA_OIF]->rta_type = 0;
  1012. }
  1013. if (tb[RTA_GATEWAY]) {
  1014. tb[RTA_GATEWAY]->rta_type = 0;
  1015. }
  1016. if (!idev && tb[RTA_IIF]) {
  1017. tb[RTA_IIF]->rta_type = 0;
  1018. }
  1019. req.n.nlmsg_flags = NLM_F_REQUEST;
  1020. req.n.nlmsg_type = RTM_GETROUTE;
  1021. if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
  1022. return 2;
  1023. }
  1024. }
  1025. print_route(NULL, &req.n, NULL);
  1026. return 0;
  1027. }
  1028. /* Return value becomes exitcode. It's okay to not return at all */
  1029. int FAST_FUNC do_iproute(char **argv)
  1030. {
  1031. static const char ip_route_commands[] ALIGN1 =
  1032. "a\0""add\0""append\0""change\0""chg\0"
  1033. "delete\0""get\0""list\0""show\0"
  1034. "prepend\0""replace\0""test\0""flush\0"
  1035. ;
  1036. enum {
  1037. CMD_a = 0, CMD_add, CMD_append, CMD_change, CMD_chg,
  1038. CMD_delete, CMD_get, CMD_list, CMD_show,
  1039. CMD_prepend, CMD_replace, CMD_test, CMD_flush,
  1040. };
  1041. int command_num;
  1042. unsigned flags = 0;
  1043. int cmd = RTM_NEWROUTE;
  1044. INIT_G();
  1045. if (!*argv)
  1046. return iproute_list_or_flush(argv, 0);
  1047. /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
  1048. /* It probably means that it is using "first match" rule */
  1049. command_num = index_in_substrings(ip_route_commands, *argv);
  1050. switch (command_num) {
  1051. case CMD_a:
  1052. case CMD_add:
  1053. flags = NLM_F_CREATE|NLM_F_EXCL;
  1054. break;
  1055. case CMD_append:
  1056. flags = NLM_F_CREATE|NLM_F_APPEND;
  1057. break;
  1058. case CMD_change:
  1059. case CMD_chg:
  1060. flags = NLM_F_REPLACE;
  1061. break;
  1062. case CMD_delete:
  1063. cmd = RTM_DELROUTE;
  1064. break;
  1065. case CMD_get:
  1066. return iproute_get(argv + 1);
  1067. case CMD_list:
  1068. case CMD_show:
  1069. return iproute_list_or_flush(argv + 1, 0);
  1070. case CMD_prepend:
  1071. flags = NLM_F_CREATE;
  1072. break;
  1073. case CMD_replace:
  1074. flags = NLM_F_CREATE|NLM_F_REPLACE;
  1075. break;
  1076. case CMD_test:
  1077. flags = NLM_F_EXCL;
  1078. break;
  1079. case CMD_flush:
  1080. return iproute_list_or_flush(argv + 1, 1);
  1081. default:
  1082. invarg_1_to_2(*argv, applet_name);
  1083. }
  1084. return iproute_modify(cmd, flags, argv + 1);
  1085. }