interface.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  1. /*
  2. * Interface function.
  3. * Copyright (C) 1997, 1999 Kunihiro Ishiguro
  4. *
  5. * This file is part of GNU Zebra.
  6. *
  7. * GNU Zebra is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2, or (at your option) any
  10. * later version.
  11. *
  12. * GNU Zebra is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Zebra; see the file COPYING. If not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "if.h"
  24. #include "vty.h"
  25. #include "sockunion.h"
  26. #include "prefix.h"
  27. #include "command.h"
  28. #include "memory.h"
  29. #include "ioctl.h"
  30. #include "connected.h"
  31. #include "log.h"
  32. #include "zclient.h"
  33. #include "vrf.h"
  34. #include "zebra/interface.h"
  35. #include "zebra/rtadv.h"
  36. #include "zebra/rib.h"
  37. #include "zebra/zserv.h"
  38. #include "zebra/redistribute.h"
  39. #include "zebra/debug.h"
  40. #include "zebra/irdp.h"
  41. #if defined (HAVE_RTADV)
  42. /* Order is intentional. Matches RFC4191. This array is also used for
  43. command matching, so only modify with care. */
  44. const char *rtadv_pref_strs[] = { "medium", "high", "INVALID", "low", 0 };
  45. #endif /* HAVE_RTADV */
  46. /* Called when new interface is added. */
  47. static int
  48. if_zebra_new_hook (struct interface *ifp)
  49. {
  50. struct zebra_if *zebra_if;
  51. zebra_if = XCALLOC (MTYPE_TMP, sizeof (struct zebra_if));
  52. zebra_if->multicast = IF_ZEBRA_MULTICAST_UNSPEC;
  53. zebra_if->shutdown = IF_ZEBRA_SHUTDOWN_OFF;
  54. #if defined (HAVE_RTADV)
  55. {
  56. /* Set default router advertise values. */
  57. struct rtadvconf *rtadv;
  58. rtadv = &zebra_if->rtadv;
  59. rtadv->AdvSendAdvertisements = 0;
  60. rtadv->MaxRtrAdvInterval = RTADV_MAX_RTR_ADV_INTERVAL;
  61. rtadv->MinRtrAdvInterval = RTADV_MIN_RTR_ADV_INTERVAL;
  62. rtadv->AdvIntervalTimer = 0;
  63. rtadv->AdvManagedFlag = 0;
  64. rtadv->AdvOtherConfigFlag = 0;
  65. rtadv->AdvHomeAgentFlag = 0;
  66. rtadv->AdvLinkMTU = 0;
  67. rtadv->AdvReachableTime = 0;
  68. rtadv->AdvRetransTimer = 0;
  69. rtadv->AdvCurHopLimit = 0;
  70. rtadv->AdvDefaultLifetime = -1; /* derive from MaxRtrAdvInterval */
  71. rtadv->HomeAgentPreference = 0;
  72. rtadv->HomeAgentLifetime = -1; /* derive from AdvDefaultLifetime */
  73. rtadv->AdvIntervalOption = 0;
  74. rtadv->DefaultPreference = RTADV_PREF_MEDIUM;
  75. rtadv->AdvPrefixList = list_new ();
  76. }
  77. #endif /* HAVE_RTADV */
  78. /* Initialize installed address chains tree. */
  79. zebra_if->ipv4_subnets = route_table_init ();
  80. ifp->info = zebra_if;
  81. return 0;
  82. }
  83. /* Called when interface is deleted. */
  84. static int
  85. if_zebra_delete_hook (struct interface *ifp)
  86. {
  87. struct zebra_if *zebra_if;
  88. if (ifp->info)
  89. {
  90. zebra_if = ifp->info;
  91. /* Free installed address chains tree. */
  92. if (zebra_if->ipv4_subnets)
  93. route_table_finish (zebra_if->ipv4_subnets);
  94. XFREE (MTYPE_TMP, zebra_if);
  95. }
  96. return 0;
  97. }
  98. /* Tie an interface address to its derived subnet list of addresses. */
  99. int
  100. if_subnet_add (struct interface *ifp, struct connected *ifc)
  101. {
  102. struct route_node *rn;
  103. struct zebra_if *zebra_if;
  104. struct prefix cp;
  105. struct list *addr_list;
  106. assert (ifp && ifp->info && ifc);
  107. zebra_if = ifp->info;
  108. /* Get address derived subnet node and associated address list, while marking
  109. address secondary attribute appropriately. */
  110. cp = *ifc->address;
  111. apply_mask (&cp);
  112. rn = route_node_get (zebra_if->ipv4_subnets, &cp);
  113. if ((addr_list = rn->info))
  114. SET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
  115. else
  116. {
  117. UNSET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
  118. rn->info = addr_list = list_new ();
  119. route_lock_node (rn);
  120. }
  121. /* Tie address at the tail of address list. */
  122. listnode_add (addr_list, ifc);
  123. /* Return list element count. */
  124. return (addr_list->count);
  125. }
  126. /* Untie an interface address from its derived subnet list of addresses. */
  127. int
  128. if_subnet_delete (struct interface *ifp, struct connected *ifc)
  129. {
  130. struct route_node *rn;
  131. struct zebra_if *zebra_if;
  132. struct list *addr_list;
  133. assert (ifp && ifp->info && ifc);
  134. zebra_if = ifp->info;
  135. /* Get address derived subnet node. */
  136. rn = route_node_lookup (zebra_if->ipv4_subnets, ifc->address);
  137. if (! (rn && rn->info))
  138. {
  139. zlog_warn("Trying to remove an address from an unknown subnet."
  140. " (please report this bug)");
  141. return -1;
  142. }
  143. route_unlock_node (rn);
  144. /* Untie address from subnet's address list. */
  145. addr_list = rn->info;
  146. /* Deleting an address that is not registered is a bug.
  147. * In any case, we shouldn't decrement the lock counter if the address
  148. * is unknown. */
  149. if (!listnode_lookup(addr_list, ifc))
  150. {
  151. zlog_warn("Trying to remove an address from a subnet where it is not"
  152. " currently registered. (please report this bug)");
  153. return -1;
  154. }
  155. listnode_delete (addr_list, ifc);
  156. route_unlock_node (rn);
  157. /* Return list element count, if not empty. */
  158. if (addr_list->count)
  159. {
  160. /* If deleted address is primary, mark subsequent one as such and distribute. */
  161. if (! CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY))
  162. {
  163. ifc = listgetdata (listhead (addr_list));
  164. zebra_interface_address_delete_update (ifp, ifc);
  165. UNSET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
  166. /* XXX: Linux kernel removes all the secondary addresses when the primary
  167. * address is removed. We could try to work around that, though this is
  168. * non-trivial. */
  169. zebra_interface_address_add_update (ifp, ifc);
  170. }
  171. return addr_list->count;
  172. }
  173. /* Otherwise, free list and route node. */
  174. list_free (addr_list);
  175. rn->info = NULL;
  176. route_unlock_node (rn);
  177. return 0;
  178. }
  179. /* if_flags_mangle: A place for hacks that require mangling
  180. * or tweaking the interface flags.
  181. *
  182. * ******************** Solaris flags hacks **************************
  183. *
  184. * Solaris IFF_UP flag reflects only the primary interface as the
  185. * routing socket only sends IFINFO for the primary interface. Hence
  186. * ~IFF_UP does not per se imply all the logical interfaces are also
  187. * down - which we only know of as addresses. Instead we must determine
  188. * whether the interface really is up or not according to how many
  189. * addresses are still attached. (Solaris always sends RTM_DELADDR if
  190. * an interface, logical or not, goes ~IFF_UP).
  191. *
  192. * Ie, we mangle IFF_UP to *additionally* reflect whether or not there
  193. * are addresses left in struct connected, not just the actual underlying
  194. * IFF_UP flag.
  195. *
  196. * We must hence remember the real state of IFF_UP, which we do in
  197. * struct zebra_if.primary_state.
  198. *
  199. * Setting IFF_UP within zebra to administratively shutdown the
  200. * interface will affect only the primary interface/address on Solaris.
  201. ************************End Solaris flags hacks ***********************
  202. */
  203. static void
  204. if_flags_mangle (struct interface *ifp, uint64_t *newflags)
  205. {
  206. #ifdef SUNOS_5
  207. struct zebra_if *zif = ifp->info;
  208. zif->primary_state = *newflags & (IFF_UP & 0xff);
  209. if (CHECK_FLAG (zif->primary_state, IFF_UP)
  210. || listcount(ifp->connected) > 0)
  211. SET_FLAG (*newflags, IFF_UP);
  212. else
  213. UNSET_FLAG (*newflags, IFF_UP);
  214. #endif /* SUNOS_5 */
  215. }
  216. /* Update the flags field of the ifp with the new flag set provided.
  217. * Take whatever actions are required for any changes in flags we care
  218. * about.
  219. *
  220. * newflags should be the raw value, as obtained from the OS.
  221. */
  222. void
  223. if_flags_update (struct interface *ifp, uint64_t newflags)
  224. {
  225. if_flags_mangle (ifp, &newflags);
  226. if (if_is_operative (ifp))
  227. {
  228. /* operative -> inoperative? */
  229. ifp->flags = newflags;
  230. if (!if_is_operative (ifp))
  231. if_down (ifp);
  232. }
  233. else
  234. {
  235. /* inoperative -> operative? */
  236. ifp->flags = newflags;
  237. if (if_is_operative (ifp))
  238. if_up (ifp);
  239. }
  240. }
  241. /* Wake up configured address if it is not in current kernel
  242. address. */
  243. static void
  244. if_addr_wakeup (struct interface *ifp)
  245. {
  246. struct listnode *node, *nnode;
  247. struct connected *ifc;
  248. struct prefix *p;
  249. int ret;
  250. for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, ifc))
  251. {
  252. p = ifc->address;
  253. if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED)
  254. && ! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED))
  255. {
  256. /* Address check. */
  257. if (p->family == AF_INET)
  258. {
  259. if (! if_is_up (ifp))
  260. {
  261. /* Assume zebra is configured like following:
  262. *
  263. * interface gre0
  264. * ip addr 192.0.2.1/24
  265. * !
  266. *
  267. * As soon as zebra becomes first aware that gre0 exists in the
  268. * kernel, it will set gre0 up and configure its addresses.
  269. *
  270. * (This may happen at startup when the interface already exists
  271. * or during runtime when the interface is added to the kernel)
  272. *
  273. * XXX: IRDP code is calling here via if_add_update - this seems
  274. * somewhat weird.
  275. * XXX: RUNNING is not a settable flag on any system
  276. * I (paulj) am aware of.
  277. */
  278. if_set_flags (ifp, IFF_UP | IFF_RUNNING);
  279. if_refresh (ifp);
  280. }
  281. ret = if_set_prefix (ifp, ifc);
  282. if (ret < 0)
  283. {
  284. zlog_warn ("Can't set interface's address: %s",
  285. safe_strerror(errno));
  286. continue;
  287. }
  288. SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
  289. /* The address will be advertised to zebra clients when the notification
  290. * from the kernel has been received.
  291. * It will also be added to the interface's subnet list then. */
  292. }
  293. #ifdef HAVE_IPV6
  294. if (p->family == AF_INET6)
  295. {
  296. if (! if_is_up (ifp))
  297. {
  298. /* See long comment above */
  299. if_set_flags (ifp, IFF_UP | IFF_RUNNING);
  300. if_refresh (ifp);
  301. }
  302. ret = if_prefix_add_ipv6 (ifp, ifc);
  303. if (ret < 0)
  304. {
  305. zlog_warn ("Can't set interface's address: %s",
  306. safe_strerror(errno));
  307. continue;
  308. }
  309. SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
  310. /* The address will be advertised to zebra clients when the notification
  311. * from the kernel has been received. */
  312. }
  313. #endif /* HAVE_IPV6 */
  314. }
  315. }
  316. }
  317. /* Handle interface addition */
  318. void
  319. if_add_update (struct interface *ifp)
  320. {
  321. struct zebra_if *if_data;
  322. if_data = ifp->info;
  323. assert(if_data);
  324. if (if_data->multicast == IF_ZEBRA_MULTICAST_ON)
  325. if_set_flags (ifp, IFF_MULTICAST);
  326. else if (if_data->multicast == IF_ZEBRA_MULTICAST_OFF)
  327. if_unset_flags (ifp, IFF_MULTICAST);
  328. zebra_interface_add_update (ifp);
  329. if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  330. {
  331. SET_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE);
  332. if (if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)
  333. {
  334. if (IS_ZEBRA_DEBUG_KERNEL)
  335. zlog_debug ("interface %s vrf %u index %d is shutdown. "
  336. "Won't wake it up.",
  337. ifp->name, ifp->vrf_id, ifp->ifindex);
  338. return;
  339. }
  340. if_addr_wakeup (ifp);
  341. if (IS_ZEBRA_DEBUG_KERNEL)
  342. zlog_debug ("interface %s vrf %u index %d becomes active.",
  343. ifp->name, ifp->vrf_id, ifp->ifindex);
  344. }
  345. else
  346. {
  347. if (IS_ZEBRA_DEBUG_KERNEL)
  348. zlog_debug ("interface %s vrf %u index %d is added.",
  349. ifp->name, ifp->vrf_id, ifp->ifindex);
  350. }
  351. }
  352. /* Handle an interface delete event */
  353. void
  354. if_delete_update (struct interface *ifp)
  355. {
  356. struct connected *ifc;
  357. struct prefix *p;
  358. struct route_node *rn;
  359. struct zebra_if *zebra_if;
  360. zebra_if = ifp->info;
  361. if (if_is_up(ifp))
  362. {
  363. zlog_err ("interface %s vrf %u index %d is still up while being deleted.",
  364. ifp->name, ifp->vrf_id, ifp->ifindex);
  365. return;
  366. }
  367. /* Mark interface as inactive */
  368. UNSET_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE);
  369. if (IS_ZEBRA_DEBUG_KERNEL)
  370. zlog_debug ("interface %s vrf %u index %d is now inactive.",
  371. ifp->name, ifp->vrf_id, ifp->ifindex);
  372. /* Delete connected routes from the kernel. */
  373. if (ifp->connected)
  374. {
  375. struct listnode *node;
  376. struct listnode *last = NULL;
  377. while ((node = (last ? last->next : listhead (ifp->connected))))
  378. {
  379. ifc = listgetdata (node);
  380. p = ifc->address;
  381. if (p->family == AF_INET
  382. && (rn = route_node_lookup (zebra_if->ipv4_subnets, p)))
  383. {
  384. struct listnode *anode;
  385. struct listnode *next;
  386. struct listnode *first;
  387. struct list *addr_list;
  388. route_unlock_node (rn);
  389. addr_list = (struct list *) rn->info;
  390. /* Remove addresses, secondaries first. */
  391. first = listhead (addr_list);
  392. for (anode = first->next; anode || first; anode = next)
  393. {
  394. if (!anode)
  395. {
  396. anode = first;
  397. first = NULL;
  398. }
  399. next = anode->next;
  400. ifc = listgetdata (anode);
  401. p = ifc->address;
  402. connected_down_ipv4 (ifp, ifc);
  403. /* XXX: We have to send notifications here explicitly, because we destroy
  404. * the ifc before receiving the notification about the address being deleted.
  405. */
  406. zebra_interface_address_delete_update (ifp, ifc);
  407. UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
  408. UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
  409. /* Remove from subnet chain. */
  410. list_delete_node (addr_list, anode);
  411. route_unlock_node (rn);
  412. /* Remove from interface address list (unconditionally). */
  413. if (!CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  414. {
  415. listnode_delete (ifp->connected, ifc);
  416. connected_free (ifc);
  417. }
  418. else
  419. last = node;
  420. }
  421. /* Free chain list and respective route node. */
  422. list_delete (addr_list);
  423. rn->info = NULL;
  424. route_unlock_node (rn);
  425. }
  426. #ifdef HAVE_IPV6
  427. else if (p->family == AF_INET6)
  428. {
  429. connected_down_ipv6 (ifp, ifc);
  430. zebra_interface_address_delete_update (ifp, ifc);
  431. UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
  432. UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
  433. if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  434. last = node;
  435. else
  436. {
  437. listnode_delete (ifp->connected, ifc);
  438. connected_free (ifc);
  439. }
  440. }
  441. #endif /* HAVE_IPV6 */
  442. else
  443. {
  444. last = node;
  445. }
  446. }
  447. }
  448. zebra_interface_delete_update (ifp);
  449. /* Update ifindex after distributing the delete message. This is in
  450. case any client needs to have the old value of ifindex available
  451. while processing the deletion. Each client daemon is responsible
  452. for setting ifindex to IFINDEX_INTERNAL after processing the
  453. interface deletion message. */
  454. ifp->ifindex = IFINDEX_INTERNAL;
  455. }
  456. /* Interface is up. */
  457. void
  458. if_up (struct interface *ifp)
  459. {
  460. struct listnode *node;
  461. struct listnode *next;
  462. struct connected *ifc;
  463. struct prefix *p;
  464. /* Notify the protocol daemons. */
  465. zebra_interface_up_update (ifp);
  466. /* Install connected routes to the kernel. */
  467. if (ifp->connected)
  468. {
  469. for (ALL_LIST_ELEMENTS (ifp->connected, node, next, ifc))
  470. {
  471. p = ifc->address;
  472. if (p->family == AF_INET)
  473. connected_up_ipv4 (ifp, ifc);
  474. #ifdef HAVE_IPV6
  475. else if (p->family == AF_INET6)
  476. connected_up_ipv6 (ifp, ifc);
  477. #endif /* HAVE_IPV6 */
  478. }
  479. }
  480. /* Examine all static routes. */
  481. rib_update (ifp->vrf_id);
  482. }
  483. /* Interface goes down. We have to manage different behavior of based
  484. OS. */
  485. void
  486. if_down (struct interface *ifp)
  487. {
  488. struct listnode *node;
  489. struct listnode *next;
  490. struct connected *ifc;
  491. struct prefix *p;
  492. /* Notify to the protocol daemons. */
  493. zebra_interface_down_update (ifp);
  494. /* Delete connected routes from the kernel. */
  495. if (ifp->connected)
  496. {
  497. for (ALL_LIST_ELEMENTS (ifp->connected, node, next, ifc))
  498. {
  499. p = ifc->address;
  500. if (p->family == AF_INET)
  501. connected_down_ipv4 (ifp, ifc);
  502. #ifdef HAVE_IPV6
  503. else if (p->family == AF_INET6)
  504. connected_down_ipv6 (ifp, ifc);
  505. #endif /* HAVE_IPV6 */
  506. }
  507. }
  508. /* Examine all static routes which direct to the interface. */
  509. rib_update (ifp->vrf_id);
  510. }
  511. void
  512. if_refresh (struct interface *ifp)
  513. {
  514. if_get_flags (ifp);
  515. }
  516. /* Output prefix string to vty. */
  517. static int
  518. prefix_vty_out (struct vty *vty, struct prefix *p)
  519. {
  520. char str[INET6_ADDRSTRLEN];
  521. inet_ntop (p->family, &p->u.prefix, str, sizeof (str));
  522. vty_out (vty, "%s", str);
  523. return strlen (str);
  524. }
  525. /* Dump if address information to vty. */
  526. static void
  527. connected_dump_vty (struct vty *vty, struct connected *connected)
  528. {
  529. struct prefix *p;
  530. /* Print interface address. */
  531. p = connected->address;
  532. vty_out (vty, " %s ", prefix_family_str (p));
  533. prefix_vty_out (vty, p);
  534. vty_out (vty, "/%d", p->prefixlen);
  535. /* If there is destination address, print it. */
  536. if (connected->destination)
  537. {
  538. vty_out (vty, (CONNECTED_PEER(connected) ? " peer " : " broadcast "));
  539. prefix_vty_out (vty, connected->destination);
  540. }
  541. if (CHECK_FLAG (connected->flags, ZEBRA_IFA_SECONDARY))
  542. vty_out (vty, " secondary");
  543. if (connected->label)
  544. vty_out (vty, " %s", connected->label);
  545. vty_out (vty, "%s", VTY_NEWLINE);
  546. }
  547. #if defined (HAVE_RTADV)
  548. /* Dump interface ND information to vty. */
  549. static void
  550. nd_dump_vty (struct vty *vty, struct interface *ifp)
  551. {
  552. struct zebra_if *zif;
  553. struct rtadvconf *rtadv;
  554. int interval;
  555. zif = (struct zebra_if *) ifp->info;
  556. rtadv = &zif->rtadv;
  557. if (rtadv->AdvSendAdvertisements)
  558. {
  559. vty_out (vty, " ND advertised reachable time is %d milliseconds%s",
  560. rtadv->AdvReachableTime, VTY_NEWLINE);
  561. vty_out (vty, " ND advertised retransmit interval is %d milliseconds%s",
  562. rtadv->AdvRetransTimer, VTY_NEWLINE);
  563. interval = rtadv->MaxRtrAdvInterval;
  564. if (interval % 1000)
  565. vty_out (vty, " ND router advertisements are sent every "
  566. "%d milliseconds%s", interval,
  567. VTY_NEWLINE);
  568. else
  569. vty_out (vty, " ND router advertisements are sent every "
  570. "%d seconds%s", interval / 1000,
  571. VTY_NEWLINE);
  572. if (rtadv->AdvDefaultLifetime != -1)
  573. vty_out (vty, " ND router advertisements live for %d seconds%s",
  574. rtadv->AdvDefaultLifetime, VTY_NEWLINE);
  575. else
  576. vty_out (vty, " ND router advertisements lifetime tracks ra-interval%s",
  577. VTY_NEWLINE);
  578. vty_out (vty, " ND router advertisement default router preference is "
  579. "%s%s", rtadv_pref_strs[rtadv->DefaultPreference],
  580. VTY_NEWLINE);
  581. if (rtadv->AdvManagedFlag)
  582. vty_out (vty, " Hosts use DHCP to obtain routable addresses.%s",
  583. VTY_NEWLINE);
  584. else
  585. vty_out (vty, " Hosts use stateless autoconfig for addresses.%s",
  586. VTY_NEWLINE);
  587. if (rtadv->AdvHomeAgentFlag)
  588. {
  589. vty_out (vty, " ND router advertisements with "
  590. "Home Agent flag bit set.%s",
  591. VTY_NEWLINE);
  592. if (rtadv->HomeAgentLifetime != -1)
  593. vty_out (vty, " Home Agent lifetime is %u seconds%s",
  594. rtadv->HomeAgentLifetime, VTY_NEWLINE);
  595. else
  596. vty_out (vty, " Home Agent lifetime tracks ra-lifetime%s",
  597. VTY_NEWLINE);
  598. vty_out (vty, " Home Agent preference is %u%s",
  599. rtadv->HomeAgentPreference, VTY_NEWLINE);
  600. }
  601. if (rtadv->AdvIntervalOption)
  602. vty_out (vty, " ND router advertisements with Adv. Interval option.%s",
  603. VTY_NEWLINE);
  604. }
  605. }
  606. #endif /* HAVE_RTADV */
  607. /* Interface's information print out to vty interface. */
  608. static void
  609. if_dump_vty (struct vty *vty, struct interface *ifp)
  610. {
  611. struct connected *connected;
  612. struct listnode *node;
  613. struct route_node *rn;
  614. struct zebra_if *zebra_if;
  615. zebra_if = ifp->info;
  616. vty_out (vty, "Interface %s is ", ifp->name);
  617. if (if_is_up(ifp)) {
  618. vty_out (vty, "up, line protocol ");
  619. if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)) {
  620. if (if_is_running(ifp))
  621. vty_out (vty, "is up%s", VTY_NEWLINE);
  622. else
  623. vty_out (vty, "is down%s", VTY_NEWLINE);
  624. } else {
  625. vty_out (vty, "detection is disabled%s", VTY_NEWLINE);
  626. }
  627. } else {
  628. vty_out (vty, "down%s", VTY_NEWLINE);
  629. }
  630. vty_out (vty, " vrf: %u%s", ifp->vrf_id, VTY_NEWLINE);
  631. if (ifp->desc)
  632. vty_out (vty, " Description: %s%s", ifp->desc,
  633. VTY_NEWLINE);
  634. if (ifp->ifindex == IFINDEX_INTERNAL)
  635. {
  636. vty_out(vty, " pseudo interface%s", VTY_NEWLINE);
  637. return;
  638. }
  639. else if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  640. {
  641. vty_out(vty, " index %d inactive interface%s",
  642. ifp->ifindex,
  643. VTY_NEWLINE);
  644. return;
  645. }
  646. vty_out (vty, " index %d metric %d mtu %d ",
  647. ifp->ifindex, ifp->metric, ifp->mtu);
  648. #ifdef HAVE_IPV6
  649. if (ifp->mtu6 != ifp->mtu)
  650. vty_out (vty, "mtu6 %d ", ifp->mtu6);
  651. #endif
  652. vty_out (vty, "%s flags: %s%s", VTY_NEWLINE,
  653. if_flag_dump (ifp->flags), VTY_NEWLINE);
  654. /* Hardware address. */
  655. vty_out (vty, " Type: %s%s", if_link_type_str (ifp->ll_type), VTY_NEWLINE);
  656. if (ifp->hw_addr_len != 0)
  657. {
  658. int i;
  659. vty_out (vty, " HWaddr: ");
  660. for (i = 0; i < ifp->hw_addr_len; i++)
  661. vty_out (vty, "%s%02x", i == 0 ? "" : ":", ifp->hw_addr[i]);
  662. vty_out (vty, "%s", VTY_NEWLINE);
  663. }
  664. /* Bandwidth in kbps */
  665. if (ifp->bandwidth != 0)
  666. {
  667. vty_out(vty, " bandwidth %u kbps", ifp->bandwidth);
  668. vty_out(vty, "%s", VTY_NEWLINE);
  669. }
  670. for (rn = route_top (zebra_if->ipv4_subnets); rn; rn = route_next (rn))
  671. {
  672. if (! rn->info)
  673. continue;
  674. for (ALL_LIST_ELEMENTS_RO ((struct list *)rn->info, node, connected))
  675. connected_dump_vty (vty, connected);
  676. }
  677. for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
  678. {
  679. if (CHECK_FLAG (connected->conf, ZEBRA_IFC_REAL) &&
  680. (connected->address->family == AF_INET6))
  681. connected_dump_vty (vty, connected);
  682. }
  683. #if defined (HAVE_RTADV)
  684. nd_dump_vty (vty, ifp);
  685. #endif /* HAVE_RTADV */
  686. #ifdef HAVE_PROC_NET_DEV
  687. /* Statistics print out using proc file system. */
  688. vty_out (vty, " %lu input packets (%lu multicast), %lu bytes, "
  689. "%lu dropped%s",
  690. ifp->stats.rx_packets, ifp->stats.rx_multicast,
  691. ifp->stats.rx_bytes, ifp->stats.rx_dropped, VTY_NEWLINE);
  692. vty_out (vty, " %lu input errors, %lu length, %lu overrun,"
  693. " %lu CRC, %lu frame%s",
  694. ifp->stats.rx_errors, ifp->stats.rx_length_errors,
  695. ifp->stats.rx_over_errors, ifp->stats.rx_crc_errors,
  696. ifp->stats.rx_frame_errors, VTY_NEWLINE);
  697. vty_out (vty, " %lu fifo, %lu missed%s", ifp->stats.rx_fifo_errors,
  698. ifp->stats.rx_missed_errors, VTY_NEWLINE);
  699. vty_out (vty, " %lu output packets, %lu bytes, %lu dropped%s",
  700. ifp->stats.tx_packets, ifp->stats.tx_bytes,
  701. ifp->stats.tx_dropped, VTY_NEWLINE);
  702. vty_out (vty, " %lu output errors, %lu aborted, %lu carrier,"
  703. " %lu fifo, %lu heartbeat%s",
  704. ifp->stats.tx_errors, ifp->stats.tx_aborted_errors,
  705. ifp->stats.tx_carrier_errors, ifp->stats.tx_fifo_errors,
  706. ifp->stats.tx_heartbeat_errors, VTY_NEWLINE);
  707. vty_out (vty, " %lu window, %lu collisions%s",
  708. ifp->stats.tx_window_errors, ifp->stats.collisions, VTY_NEWLINE);
  709. #endif /* HAVE_PROC_NET_DEV */
  710. #ifdef HAVE_NET_RT_IFLIST
  711. #if defined (__bsdi__) || defined (__NetBSD__)
  712. /* Statistics print out using sysctl (). */
  713. vty_out (vty, " input packets %llu, bytes %llu, dropped %llu,"
  714. " multicast packets %llu%s",
  715. (unsigned long long)ifp->stats.ifi_ipackets,
  716. (unsigned long long)ifp->stats.ifi_ibytes,
  717. (unsigned long long)ifp->stats.ifi_iqdrops,
  718. (unsigned long long)ifp->stats.ifi_imcasts,
  719. VTY_NEWLINE);
  720. vty_out (vty, " input errors %llu%s",
  721. (unsigned long long)ifp->stats.ifi_ierrors, VTY_NEWLINE);
  722. vty_out (vty, " output packets %llu, bytes %llu,"
  723. " multicast packets %llu%s",
  724. (unsigned long long)ifp->stats.ifi_opackets,
  725. (unsigned long long)ifp->stats.ifi_obytes,
  726. (unsigned long long)ifp->stats.ifi_omcasts,
  727. VTY_NEWLINE);
  728. vty_out (vty, " output errors %llu%s",
  729. (unsigned long long)ifp->stats.ifi_oerrors, VTY_NEWLINE);
  730. vty_out (vty, " collisions %llu%s",
  731. (unsigned long long)ifp->stats.ifi_collisions, VTY_NEWLINE);
  732. #else
  733. /* Statistics print out using sysctl (). */
  734. vty_out (vty, " input packets %lu, bytes %lu, dropped %lu,"
  735. " multicast packets %lu%s",
  736. ifp->stats.ifi_ipackets, ifp->stats.ifi_ibytes,
  737. ifp->stats.ifi_iqdrops, ifp->stats.ifi_imcasts,
  738. VTY_NEWLINE);
  739. vty_out (vty, " input errors %lu%s",
  740. ifp->stats.ifi_ierrors, VTY_NEWLINE);
  741. vty_out (vty, " output packets %lu, bytes %lu, multicast packets %lu%s",
  742. ifp->stats.ifi_opackets, ifp->stats.ifi_obytes,
  743. ifp->stats.ifi_omcasts, VTY_NEWLINE);
  744. vty_out (vty, " output errors %lu%s",
  745. ifp->stats.ifi_oerrors, VTY_NEWLINE);
  746. vty_out (vty, " collisions %lu%s",
  747. ifp->stats.ifi_collisions, VTY_NEWLINE);
  748. #endif /* __bsdi__ || __NetBSD__ */
  749. #endif /* HAVE_NET_RT_IFLIST */
  750. }
  751. /* Wrapper hook point for zebra daemon so that ifindex can be set
  752. * DEFUN macro not used as extract.pl HAS to ignore this
  753. * See also interface_cmd in lib/if.c
  754. */
  755. DEFUN_NOSH (zebra_interface,
  756. zebra_interface_cmd,
  757. "interface IFNAME",
  758. "Select an interface to configure\n"
  759. "Interface's name\n")
  760. {
  761. int ret;
  762. struct interface * ifp;
  763. /* Call lib interface() */
  764. if ((ret = interface_cmd.func (self, vty, argc, argv)) != CMD_SUCCESS)
  765. return ret;
  766. ifp = vty->index;
  767. if (ifp->ifindex == IFINDEX_INTERNAL)
  768. /* Is this really necessary? Shouldn't status be initialized to 0
  769. in that case? */
  770. UNSET_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE);
  771. return ret;
  772. }
  773. ALIAS (zebra_interface,
  774. zebra_interface_vrf_cmd,
  775. "interface IFNAME " VRF_CMD_STR,
  776. "Select an interface to configure\n"
  777. "Interface's name\n"
  778. VRF_CMD_HELP_STR)
  779. struct cmd_node interface_node =
  780. {
  781. INTERFACE_NODE,
  782. "%s(config-if)# ",
  783. 1
  784. };
  785. /* Show all interfaces to vty. */
  786. DEFUN (show_interface, show_interface_cmd,
  787. "show interface",
  788. SHOW_STR
  789. "Interface status and configuration\n")
  790. {
  791. struct listnode *node;
  792. struct interface *ifp;
  793. vrf_id_t vrf_id = VRF_DEFAULT;
  794. #ifdef HAVE_PROC_NET_DEV
  795. /* If system has interface statistics via proc file system, update
  796. statistics. */
  797. ifstat_update_proc ();
  798. #endif /* HAVE_PROC_NET_DEV */
  799. #ifdef HAVE_NET_RT_IFLIST
  800. ifstat_update_sysctl ();
  801. #endif /* HAVE_NET_RT_IFLIST */
  802. if (argc > 0)
  803. VTY_GET_INTEGER ("VRF ID", vrf_id, argv[0]);
  804. /* All interface print. */
  805. for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
  806. if_dump_vty (vty, ifp);
  807. return CMD_SUCCESS;
  808. }
  809. ALIAS (show_interface,
  810. show_interface_vrf_cmd,
  811. "show interface " VRF_CMD_STR,
  812. SHOW_STR
  813. "Interface status and configuration\n"
  814. VRF_CMD_HELP_STR)
  815. /* Show all interfaces to vty. */
  816. DEFUN (show_interface_vrf_all, show_interface_vrf_all_cmd,
  817. "show interface " VRF_ALL_CMD_STR,
  818. SHOW_STR
  819. "Interface status and configuration\n"
  820. VRF_ALL_CMD_HELP_STR)
  821. {
  822. struct listnode *node;
  823. struct interface *ifp;
  824. vrf_iter_t iter;
  825. #ifdef HAVE_PROC_NET_DEV
  826. /* If system has interface statistics via proc file system, update
  827. statistics. */
  828. ifstat_update_proc ();
  829. #endif /* HAVE_PROC_NET_DEV */
  830. #ifdef HAVE_NET_RT_IFLIST
  831. ifstat_update_sysctl ();
  832. #endif /* HAVE_NET_RT_IFLIST */
  833. /* All interface print. */
  834. for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
  835. for (ALL_LIST_ELEMENTS_RO (vrf_iter2iflist (iter), node, ifp))
  836. if_dump_vty (vty, ifp);
  837. return CMD_SUCCESS;
  838. }
  839. /* Show specified interface to vty. */
  840. DEFUN (show_interface_name, show_interface_name_cmd,
  841. "show interface IFNAME",
  842. SHOW_STR
  843. "Interface status and configuration\n"
  844. "Inteface name\n")
  845. {
  846. struct interface *ifp;
  847. vrf_id_t vrf_id = VRF_DEFAULT;
  848. #ifdef HAVE_PROC_NET_DEV
  849. /* If system has interface statistics via proc file system, update
  850. statistics. */
  851. ifstat_update_proc ();
  852. #endif /* HAVE_PROC_NET_DEV */
  853. #ifdef HAVE_NET_RT_IFLIST
  854. ifstat_update_sysctl ();
  855. #endif /* HAVE_NET_RT_IFLIST */
  856. if (argc > 1)
  857. VTY_GET_INTEGER ("VRF ID", vrf_id, argv[1]);
  858. /* Specified interface print. */
  859. ifp = if_lookup_by_name_vrf (argv[0], vrf_id);
  860. if (ifp == NULL)
  861. {
  862. vty_out (vty, "%% Can't find interface %s%s", argv[0],
  863. VTY_NEWLINE);
  864. return CMD_WARNING;
  865. }
  866. if_dump_vty (vty, ifp);
  867. return CMD_SUCCESS;
  868. }
  869. ALIAS (show_interface_name,
  870. show_interface_name_vrf_cmd,
  871. "show interface IFNAME " VRF_CMD_STR,
  872. SHOW_STR
  873. "Interface status and configuration\n"
  874. "Inteface name\n"
  875. VRF_CMD_HELP_STR)
  876. /* Show specified interface to vty. */
  877. DEFUN (show_interface_name_vrf_all, show_interface_name_vrf_all_cmd,
  878. "show interface IFNAME " VRF_ALL_CMD_STR,
  879. SHOW_STR
  880. "Interface status and configuration\n"
  881. "Inteface name\n"
  882. VRF_ALL_CMD_HELP_STR)
  883. {
  884. struct interface *ifp;
  885. vrf_iter_t iter;
  886. int found = 0;
  887. #ifdef HAVE_PROC_NET_DEV
  888. /* If system has interface statistics via proc file system, update
  889. statistics. */
  890. ifstat_update_proc ();
  891. #endif /* HAVE_PROC_NET_DEV */
  892. #ifdef HAVE_NET_RT_IFLIST
  893. ifstat_update_sysctl ();
  894. #endif /* HAVE_NET_RT_IFLIST */
  895. /* All interface print. */
  896. for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
  897. {
  898. /* Specified interface print. */
  899. ifp = if_lookup_by_name_vrf (argv[0], vrf_iter2id (iter));
  900. if (ifp)
  901. {
  902. if_dump_vty (vty, ifp);
  903. found++;
  904. }
  905. }
  906. if (!found)
  907. {
  908. vty_out (vty, "%% Can't find interface %s%s", argv[0], VTY_NEWLINE);
  909. return CMD_WARNING;
  910. }
  911. return CMD_SUCCESS;
  912. }
  913. static void
  914. if_show_description (struct vty *vty, vrf_id_t vrf_id)
  915. {
  916. struct listnode *node;
  917. struct interface *ifp;
  918. vty_out (vty, "Interface Status Protocol Description%s", VTY_NEWLINE);
  919. for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
  920. {
  921. int len;
  922. len = vty_out (vty, "%s", ifp->name);
  923. vty_out (vty, "%*s", (16 - len), " ");
  924. if (if_is_up(ifp))
  925. {
  926. vty_out (vty, "up ");
  927. if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION))
  928. {
  929. if (if_is_running(ifp))
  930. vty_out (vty, "up ");
  931. else
  932. vty_out (vty, "down ");
  933. }
  934. else
  935. {
  936. vty_out (vty, "unknown ");
  937. }
  938. }
  939. else
  940. {
  941. vty_out (vty, "down down ");
  942. }
  943. if (ifp->desc)
  944. vty_out (vty, "%s", ifp->desc);
  945. vty_out (vty, "%s", VTY_NEWLINE);
  946. }
  947. }
  948. DEFUN (show_interface_desc,
  949. show_interface_desc_cmd,
  950. "show interface description",
  951. SHOW_STR
  952. "Interface status and configuration\n"
  953. "Interface description\n")
  954. {
  955. vrf_id_t vrf_id = VRF_DEFAULT;
  956. if (argc > 0)
  957. VTY_GET_INTEGER ("VRF ID", vrf_id, argv[0]);
  958. if_show_description (vty, vrf_id);
  959. return CMD_SUCCESS;
  960. }
  961. ALIAS (show_interface_desc,
  962. show_interface_desc_vrf_cmd,
  963. "show interface description " VRF_CMD_STR,
  964. SHOW_STR
  965. "Interface status and configuration\n"
  966. "Interface description\n"
  967. VRF_CMD_HELP_STR)
  968. DEFUN (show_interface_desc_vrf_all,
  969. show_interface_desc_vrf_all_cmd,
  970. "show interface description " VRF_ALL_CMD_STR,
  971. SHOW_STR
  972. "Interface status and configuration\n"
  973. "Interface description\n"
  974. VRF_ALL_CMD_HELP_STR)
  975. {
  976. vrf_iter_t iter;
  977. for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
  978. if (!list_isempty (vrf_iter2iflist (iter)))
  979. {
  980. vty_out (vty, "%s\tVRF %u%s%s", VTY_NEWLINE,
  981. vrf_iter2id (iter),
  982. VTY_NEWLINE, VTY_NEWLINE);
  983. if_show_description (vty, vrf_iter2id (iter));
  984. }
  985. return CMD_SUCCESS;
  986. }
  987. DEFUN (multicast,
  988. multicast_cmd,
  989. "multicast",
  990. "Set multicast flag to interface\n")
  991. {
  992. int ret;
  993. struct interface *ifp;
  994. struct zebra_if *if_data;
  995. ifp = (struct interface *) vty->index;
  996. if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  997. {
  998. ret = if_set_flags (ifp, IFF_MULTICAST);
  999. if (ret < 0)
  1000. {
  1001. vty_out (vty, "Can't set multicast flag%s", VTY_NEWLINE);
  1002. return CMD_WARNING;
  1003. }
  1004. if_refresh (ifp);
  1005. }
  1006. if_data = ifp->info;
  1007. if_data->multicast = IF_ZEBRA_MULTICAST_ON;
  1008. return CMD_SUCCESS;
  1009. }
  1010. DEFUN (no_multicast,
  1011. no_multicast_cmd,
  1012. "no multicast",
  1013. NO_STR
  1014. "Unset multicast flag to interface\n")
  1015. {
  1016. int ret;
  1017. struct interface *ifp;
  1018. struct zebra_if *if_data;
  1019. ifp = (struct interface *) vty->index;
  1020. if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  1021. {
  1022. ret = if_unset_flags (ifp, IFF_MULTICAST);
  1023. if (ret < 0)
  1024. {
  1025. vty_out (vty, "Can't unset multicast flag%s", VTY_NEWLINE);
  1026. return CMD_WARNING;
  1027. }
  1028. if_refresh (ifp);
  1029. }
  1030. if_data = ifp->info;
  1031. if_data->multicast = IF_ZEBRA_MULTICAST_OFF;
  1032. return CMD_SUCCESS;
  1033. }
  1034. DEFUN (linkdetect,
  1035. linkdetect_cmd,
  1036. "link-detect",
  1037. "Enable link detection on interface\n")
  1038. {
  1039. struct interface *ifp;
  1040. int if_was_operative;
  1041. ifp = (struct interface *) vty->index;
  1042. if_was_operative = if_is_operative(ifp);
  1043. SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
  1044. /* When linkdetection is enabled, if might come down */
  1045. if (!if_is_operative(ifp) && if_was_operative) if_down(ifp);
  1046. /* FIXME: Will defer status change forwarding if interface
  1047. does not come down! */
  1048. return CMD_SUCCESS;
  1049. }
  1050. DEFUN (no_linkdetect,
  1051. no_linkdetect_cmd,
  1052. "no link-detect",
  1053. NO_STR
  1054. "Disable link detection on interface\n")
  1055. {
  1056. struct interface *ifp;
  1057. int if_was_operative;
  1058. ifp = (struct interface *) vty->index;
  1059. if_was_operative = if_is_operative(ifp);
  1060. UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
  1061. /* Interface may come up after disabling link detection */
  1062. if (if_is_operative(ifp) && !if_was_operative) if_up(ifp);
  1063. /* FIXME: see linkdetect_cmd */
  1064. return CMD_SUCCESS;
  1065. }
  1066. DEFUN (shutdown_if,
  1067. shutdown_if_cmd,
  1068. "shutdown",
  1069. "Shutdown the selected interface\n")
  1070. {
  1071. int ret;
  1072. struct interface *ifp;
  1073. struct zebra_if *if_data;
  1074. ifp = (struct interface *) vty->index;
  1075. if (ifp->ifindex != IFINDEX_INTERNAL)
  1076. {
  1077. ret = if_unset_flags (ifp, IFF_UP);
  1078. if (ret < 0)
  1079. {
  1080. vty_out (vty, "Can't shutdown interface%s", VTY_NEWLINE);
  1081. return CMD_WARNING;
  1082. }
  1083. if_refresh (ifp);
  1084. }
  1085. if_data = ifp->info;
  1086. if_data->shutdown = IF_ZEBRA_SHUTDOWN_ON;
  1087. return CMD_SUCCESS;
  1088. }
  1089. DEFUN (no_shutdown_if,
  1090. no_shutdown_if_cmd,
  1091. "no shutdown",
  1092. NO_STR
  1093. "Shutdown the selected interface\n")
  1094. {
  1095. int ret;
  1096. struct interface *ifp;
  1097. struct zebra_if *if_data;
  1098. ifp = (struct interface *) vty->index;
  1099. if (ifp->ifindex != IFINDEX_INTERNAL)
  1100. {
  1101. ret = if_set_flags (ifp, IFF_UP | IFF_RUNNING);
  1102. if (ret < 0)
  1103. {
  1104. vty_out (vty, "Can't up interface%s", VTY_NEWLINE);
  1105. return CMD_WARNING;
  1106. }
  1107. if_refresh (ifp);
  1108. /* Some addresses (in particular, IPv6 addresses on Linux) get
  1109. * removed when the interface goes down. They need to be readded.
  1110. */
  1111. if_addr_wakeup(ifp);
  1112. }
  1113. if_data = ifp->info;
  1114. if_data->shutdown = IF_ZEBRA_SHUTDOWN_OFF;
  1115. return CMD_SUCCESS;
  1116. }
  1117. DEFUN (bandwidth_if,
  1118. bandwidth_if_cmd,
  1119. "bandwidth <1-10000000>",
  1120. "Set bandwidth informational parameter\n"
  1121. "Bandwidth in kilobits\n")
  1122. {
  1123. struct interface *ifp;
  1124. unsigned int bandwidth;
  1125. ifp = (struct interface *) vty->index;
  1126. bandwidth = strtol(argv[0], NULL, 10);
  1127. /* bandwidth range is <1-10000000> */
  1128. if (bandwidth < 1 || bandwidth > 10000000)
  1129. {
  1130. vty_out (vty, "Bandwidth is invalid%s", VTY_NEWLINE);
  1131. return CMD_WARNING;
  1132. }
  1133. ifp->bandwidth = bandwidth;
  1134. /* force protocols to recalculate routes due to cost change */
  1135. if (if_is_operative (ifp))
  1136. zebra_interface_up_update (ifp);
  1137. return CMD_SUCCESS;
  1138. }
  1139. DEFUN (no_bandwidth_if,
  1140. no_bandwidth_if_cmd,
  1141. "no bandwidth",
  1142. NO_STR
  1143. "Set bandwidth informational parameter\n")
  1144. {
  1145. struct interface *ifp;
  1146. ifp = (struct interface *) vty->index;
  1147. ifp->bandwidth = 0;
  1148. /* force protocols to recalculate routes due to cost change */
  1149. if (if_is_operative (ifp))
  1150. zebra_interface_up_update (ifp);
  1151. return CMD_SUCCESS;
  1152. }
  1153. ALIAS (no_bandwidth_if,
  1154. no_bandwidth_if_val_cmd,
  1155. "no bandwidth <1-10000000>",
  1156. NO_STR
  1157. "Set bandwidth informational parameter\n"
  1158. "Bandwidth in kilobits\n")
  1159. static int
  1160. ip_address_install (struct vty *vty, struct interface *ifp,
  1161. const char *addr_str, const char *peer_str,
  1162. const char *label)
  1163. {
  1164. struct zebra_if *if_data;
  1165. struct prefix_ipv4 cp;
  1166. struct connected *ifc;
  1167. struct prefix_ipv4 *p;
  1168. int ret;
  1169. if_data = ifp->info;
  1170. ret = str2prefix_ipv4 (addr_str, &cp);
  1171. if (ret <= 0)
  1172. {
  1173. vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
  1174. return CMD_WARNING;
  1175. }
  1176. ifc = connected_check (ifp, (struct prefix *) &cp);
  1177. if (! ifc)
  1178. {
  1179. ifc = connected_new ();
  1180. ifc->ifp = ifp;
  1181. /* Address. */
  1182. p = prefix_ipv4_new ();
  1183. *p = cp;
  1184. ifc->address = (struct prefix *) p;
  1185. /* Broadcast. */
  1186. if (p->prefixlen <= IPV4_MAX_PREFIXLEN-2)
  1187. {
  1188. p = prefix_ipv4_new ();
  1189. *p = cp;
  1190. p->prefix.s_addr = ipv4_broadcast_addr(p->prefix.s_addr,p->prefixlen);
  1191. ifc->destination = (struct prefix *) p;
  1192. }
  1193. /* Label. */
  1194. if (label)
  1195. ifc->label = XSTRDUP (MTYPE_CONNECTED_LABEL, label);
  1196. /* Add to linked list. */
  1197. listnode_add (ifp->connected, ifc);
  1198. }
  1199. /* This address is configured from zebra. */
  1200. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  1201. SET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
  1202. /* In case of this route need to install kernel. */
  1203. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
  1204. && CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)
  1205. && !(if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON))
  1206. {
  1207. /* Some system need to up the interface to set IP address. */
  1208. if (! if_is_up (ifp))
  1209. {
  1210. if_set_flags (ifp, IFF_UP | IFF_RUNNING);
  1211. if_refresh (ifp);
  1212. }
  1213. ret = if_set_prefix (ifp, ifc);
  1214. if (ret < 0)
  1215. {
  1216. vty_out (vty, "%% Can't set interface IP address: %s.%s",
  1217. safe_strerror(errno), VTY_NEWLINE);
  1218. return CMD_WARNING;
  1219. }
  1220. SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
  1221. /* The address will be advertised to zebra clients when the notification
  1222. * from the kernel has been received.
  1223. * It will also be added to the subnet chain list, then. */
  1224. }
  1225. return CMD_SUCCESS;
  1226. }
  1227. static int
  1228. ip_address_uninstall (struct vty *vty, struct interface *ifp,
  1229. const char *addr_str, const char *peer_str,
  1230. const char *label)
  1231. {
  1232. struct prefix_ipv4 cp;
  1233. struct connected *ifc;
  1234. int ret;
  1235. /* Convert to prefix structure. */
  1236. ret = str2prefix_ipv4 (addr_str, &cp);
  1237. if (ret <= 0)
  1238. {
  1239. vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
  1240. return CMD_WARNING;
  1241. }
  1242. /* Check current interface address. */
  1243. ifc = connected_check (ifp, (struct prefix *) &cp);
  1244. if (! ifc)
  1245. {
  1246. vty_out (vty, "%% Can't find address%s", VTY_NEWLINE);
  1247. return CMD_WARNING;
  1248. }
  1249. /* This is not configured address. */
  1250. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  1251. return CMD_WARNING;
  1252. UNSET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
  1253. /* This is not real address or interface is not active. */
  1254. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
  1255. || ! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  1256. {
  1257. listnode_delete (ifp->connected, ifc);
  1258. connected_free (ifc);
  1259. return CMD_WARNING;
  1260. }
  1261. /* This is real route. */
  1262. ret = if_unset_prefix (ifp, ifc);
  1263. if (ret < 0)
  1264. {
  1265. vty_out (vty, "%% Can't unset interface IP address: %s.%s",
  1266. safe_strerror(errno), VTY_NEWLINE);
  1267. return CMD_WARNING;
  1268. }
  1269. UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
  1270. /* we will receive a kernel notification about this route being removed.
  1271. * this will trigger its removal from the connected list. */
  1272. return CMD_SUCCESS;
  1273. }
  1274. DEFUN (ip_address,
  1275. ip_address_cmd,
  1276. "ip address A.B.C.D/M",
  1277. "Interface Internet Protocol config commands\n"
  1278. "Set the IP address of an interface\n"
  1279. "IP address (e.g. 10.0.0.1/8)\n")
  1280. {
  1281. return ip_address_install (vty, vty->index, argv[0], NULL, NULL);
  1282. }
  1283. DEFUN (no_ip_address,
  1284. no_ip_address_cmd,
  1285. "no ip address A.B.C.D/M",
  1286. NO_STR
  1287. "Interface Internet Protocol config commands\n"
  1288. "Set the IP address of an interface\n"
  1289. "IP Address (e.g. 10.0.0.1/8)")
  1290. {
  1291. return ip_address_uninstall (vty, vty->index, argv[0], NULL, NULL);
  1292. }
  1293. #ifdef HAVE_NETLINK
  1294. DEFUN (ip_address_label,
  1295. ip_address_label_cmd,
  1296. "ip address A.B.C.D/M label LINE",
  1297. "Interface Internet Protocol config commands\n"
  1298. "Set the IP address of an interface\n"
  1299. "IP address (e.g. 10.0.0.1/8)\n"
  1300. "Label of this address\n"
  1301. "Label\n")
  1302. {
  1303. return ip_address_install (vty, vty->index, argv[0], NULL, argv[1]);
  1304. }
  1305. DEFUN (no_ip_address_label,
  1306. no_ip_address_label_cmd,
  1307. "no ip address A.B.C.D/M label LINE",
  1308. NO_STR
  1309. "Interface Internet Protocol config commands\n"
  1310. "Set the IP address of an interface\n"
  1311. "IP address (e.g. 10.0.0.1/8)\n"
  1312. "Label of this address\n"
  1313. "Label\n")
  1314. {
  1315. return ip_address_uninstall (vty, vty->index, argv[0], NULL, argv[1]);
  1316. }
  1317. #endif /* HAVE_NETLINK */
  1318. #ifdef HAVE_IPV6
  1319. static int
  1320. ipv6_address_install (struct vty *vty, struct interface *ifp,
  1321. const char *addr_str, const char *peer_str,
  1322. const char *label, int secondary)
  1323. {
  1324. struct zebra_if *if_data;
  1325. struct prefix_ipv6 cp;
  1326. struct connected *ifc;
  1327. struct prefix_ipv6 *p;
  1328. int ret;
  1329. if_data = ifp->info;
  1330. ret = str2prefix_ipv6 (addr_str, &cp);
  1331. if (ret <= 0)
  1332. {
  1333. vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
  1334. return CMD_WARNING;
  1335. }
  1336. ifc = connected_check (ifp, (struct prefix *) &cp);
  1337. if (! ifc)
  1338. {
  1339. ifc = connected_new ();
  1340. ifc->ifp = ifp;
  1341. /* Address. */
  1342. p = prefix_ipv6_new ();
  1343. *p = cp;
  1344. ifc->address = (struct prefix *) p;
  1345. /* Secondary. */
  1346. if (secondary)
  1347. SET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
  1348. /* Label. */
  1349. if (label)
  1350. ifc->label = XSTRDUP (MTYPE_CONNECTED_LABEL, label);
  1351. /* Add to linked list. */
  1352. listnode_add (ifp->connected, ifc);
  1353. }
  1354. /* This address is configured from zebra. */
  1355. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  1356. SET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
  1357. /* In case of this route need to install kernel. */
  1358. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
  1359. && CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)
  1360. && !(if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON))
  1361. {
  1362. /* Some system need to up the interface to set IP address. */
  1363. if (! if_is_up (ifp))
  1364. {
  1365. if_set_flags (ifp, IFF_UP | IFF_RUNNING);
  1366. if_refresh (ifp);
  1367. }
  1368. ret = if_prefix_add_ipv6 (ifp, ifc);
  1369. if (ret < 0)
  1370. {
  1371. vty_out (vty, "%% Can't set interface IP address: %s.%s",
  1372. safe_strerror(errno), VTY_NEWLINE);
  1373. return CMD_WARNING;
  1374. }
  1375. SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
  1376. /* The address will be advertised to zebra clients when the notification
  1377. * from the kernel has been received. */
  1378. }
  1379. return CMD_SUCCESS;
  1380. }
  1381. static int
  1382. ipv6_address_uninstall (struct vty *vty, struct interface *ifp,
  1383. const char *addr_str, const char *peer_str,
  1384. const char *label, int secondry)
  1385. {
  1386. struct prefix_ipv6 cp;
  1387. struct connected *ifc;
  1388. int ret;
  1389. /* Convert to prefix structure. */
  1390. ret = str2prefix_ipv6 (addr_str, &cp);
  1391. if (ret <= 0)
  1392. {
  1393. vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
  1394. return CMD_WARNING;
  1395. }
  1396. /* Check current interface address. */
  1397. ifc = connected_check (ifp, (struct prefix *) &cp);
  1398. if (! ifc)
  1399. {
  1400. vty_out (vty, "%% Can't find address%s", VTY_NEWLINE);
  1401. return CMD_WARNING;
  1402. }
  1403. /* This is not configured address. */
  1404. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  1405. return CMD_WARNING;
  1406. UNSET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
  1407. /* This is not real address or interface is not active. */
  1408. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
  1409. || ! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  1410. {
  1411. listnode_delete (ifp->connected, ifc);
  1412. connected_free (ifc);
  1413. return CMD_WARNING;
  1414. }
  1415. /* This is real route. */
  1416. ret = if_prefix_delete_ipv6 (ifp, ifc);
  1417. if (ret < 0)
  1418. {
  1419. vty_out (vty, "%% Can't unset interface IP address: %s.%s",
  1420. safe_strerror(errno), VTY_NEWLINE);
  1421. return CMD_WARNING;
  1422. }
  1423. UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
  1424. /* This information will be propagated to the zclients when the
  1425. * kernel notification is received. */
  1426. return CMD_SUCCESS;
  1427. }
  1428. DEFUN (ipv6_address,
  1429. ipv6_address_cmd,
  1430. "ipv6 address X:X::X:X/M",
  1431. "Interface IPv6 config commands\n"
  1432. "Set the IP address of an interface\n"
  1433. "IPv6 address (e.g. 3ffe:506::1/48)\n")
  1434. {
  1435. return ipv6_address_install (vty, vty->index, argv[0], NULL, NULL, 0);
  1436. }
  1437. DEFUN (no_ipv6_address,
  1438. no_ipv6_address_cmd,
  1439. "no ipv6 address X:X::X:X/M",
  1440. NO_STR
  1441. "Interface IPv6 config commands\n"
  1442. "Set the IP address of an interface\n"
  1443. "IPv6 address (e.g. 3ffe:506::1/48)\n")
  1444. {
  1445. return ipv6_address_uninstall (vty, vty->index, argv[0], NULL, NULL, 0);
  1446. }
  1447. #endif /* HAVE_IPV6 */
  1448. static int
  1449. if_config_write (struct vty *vty)
  1450. {
  1451. struct listnode *node;
  1452. struct interface *ifp;
  1453. vrf_iter_t iter;
  1454. for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
  1455. for (ALL_LIST_ELEMENTS_RO (vrf_iter2iflist (iter), node, ifp))
  1456. {
  1457. struct zebra_if *if_data;
  1458. struct listnode *addrnode;
  1459. struct connected *ifc;
  1460. struct prefix *p;
  1461. if_data = ifp->info;
  1462. if (ifp->vrf_id == VRF_DEFAULT)
  1463. vty_out (vty, "interface %s%s", ifp->name, VTY_NEWLINE);
  1464. else
  1465. vty_out (vty, "interface %s vrf %u%s", ifp->name, ifp->vrf_id,
  1466. VTY_NEWLINE);
  1467. if (if_data)
  1468. {
  1469. if (if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)
  1470. vty_out (vty, " shutdown%s", VTY_NEWLINE);
  1471. }
  1472. if (ifp->desc)
  1473. vty_out (vty, " description %s%s", ifp->desc,
  1474. VTY_NEWLINE);
  1475. /* Assign bandwidth here to avoid unnecessary interface flap
  1476. while processing config script */
  1477. if (ifp->bandwidth != 0)
  1478. vty_out(vty, " bandwidth %u%s", ifp->bandwidth, VTY_NEWLINE);
  1479. if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION))
  1480. vty_out(vty, " link-detect%s", VTY_NEWLINE);
  1481. else
  1482. vty_out(vty, " no link-detect%s", VTY_NEWLINE);
  1483. for (ALL_LIST_ELEMENTS_RO (ifp->connected, addrnode, ifc))
  1484. {
  1485. if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  1486. {
  1487. char buf[INET6_ADDRSTRLEN];
  1488. p = ifc->address;
  1489. vty_out (vty, " ip%s address %s",
  1490. p->family == AF_INET ? "" : "v6",
  1491. prefix2str (p, buf, sizeof(buf)));
  1492. if (ifc->label)
  1493. vty_out (vty, " label %s", ifc->label);
  1494. vty_out (vty, "%s", VTY_NEWLINE);
  1495. }
  1496. }
  1497. if (if_data)
  1498. {
  1499. if (if_data->multicast != IF_ZEBRA_MULTICAST_UNSPEC)
  1500. vty_out (vty, " %smulticast%s",
  1501. if_data->multicast == IF_ZEBRA_MULTICAST_ON ? "" : "no ",
  1502. VTY_NEWLINE);
  1503. }
  1504. #if defined (HAVE_RTADV)
  1505. rtadv_config_write (vty, ifp);
  1506. #endif /* HAVE_RTADV */
  1507. #ifdef HAVE_IRDP
  1508. irdp_config_write (vty, ifp);
  1509. #endif /* IRDP */
  1510. vty_out (vty, "!%s", VTY_NEWLINE);
  1511. }
  1512. return 0;
  1513. }
  1514. /* Allocate and initialize interface vector. */
  1515. void
  1516. zebra_if_init (void)
  1517. {
  1518. /* Initialize interface and new hook. */
  1519. if_add_hook (IF_NEW_HOOK, if_zebra_new_hook);
  1520. if_add_hook (IF_DELETE_HOOK, if_zebra_delete_hook);
  1521. /* Install configuration write function. */
  1522. install_node (&interface_node, if_config_write);
  1523. install_element (VIEW_NODE, &show_interface_cmd);
  1524. install_element (VIEW_NODE, &show_interface_vrf_cmd);
  1525. install_element (VIEW_NODE, &show_interface_vrf_all_cmd);
  1526. install_element (VIEW_NODE, &show_interface_name_cmd);
  1527. install_element (VIEW_NODE, &show_interface_name_vrf_cmd);
  1528. install_element (VIEW_NODE, &show_interface_name_vrf_all_cmd);
  1529. install_element (CONFIG_NODE, &zebra_interface_cmd);
  1530. install_element (CONFIG_NODE, &zebra_interface_vrf_cmd);
  1531. install_element (CONFIG_NODE, &no_interface_cmd);
  1532. install_element (CONFIG_NODE, &no_interface_vrf_cmd);
  1533. install_default (INTERFACE_NODE);
  1534. install_element (INTERFACE_NODE, &interface_desc_cmd);
  1535. install_element (INTERFACE_NODE, &no_interface_desc_cmd);
  1536. install_element (INTERFACE_NODE, &multicast_cmd);
  1537. install_element (INTERFACE_NODE, &no_multicast_cmd);
  1538. install_element (INTERFACE_NODE, &linkdetect_cmd);
  1539. install_element (INTERFACE_NODE, &no_linkdetect_cmd);
  1540. install_element (INTERFACE_NODE, &shutdown_if_cmd);
  1541. install_element (INTERFACE_NODE, &no_shutdown_if_cmd);
  1542. install_element (INTERFACE_NODE, &bandwidth_if_cmd);
  1543. install_element (INTERFACE_NODE, &no_bandwidth_if_cmd);
  1544. install_element (INTERFACE_NODE, &no_bandwidth_if_val_cmd);
  1545. install_element (INTERFACE_NODE, &ip_address_cmd);
  1546. install_element (INTERFACE_NODE, &no_ip_address_cmd);
  1547. #ifdef HAVE_IPV6
  1548. install_element (INTERFACE_NODE, &ipv6_address_cmd);
  1549. install_element (INTERFACE_NODE, &no_ipv6_address_cmd);
  1550. #endif /* HAVE_IPV6 */
  1551. #ifdef HAVE_NETLINK
  1552. install_element (INTERFACE_NODE, &ip_address_label_cmd);
  1553. install_element (INTERFACE_NODE, &no_ip_address_label_cmd);
  1554. #endif /* HAVE_NETLINK */
  1555. }