3
0

iproute.c 29 KB

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