interface.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818
  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 "command.h"
  35. #include "zebra/interface.h"
  36. #include "zebra/rtadv.h"
  37. #include "zebra/rib.h"
  38. #include "zebra/zserv.h"
  39. #include "zebra/redistribute.h"
  40. #include "zebra/debug.h"
  41. #include "zebra/irdp.h"
  42. #if defined (HAVE_RTADV)
  43. /* Order is intentional. Matches RFC4191. This array is also used for
  44. command matching, so only modify with care. */
  45. const char *rtadv_pref_strs[] = { "medium", "high", "INVALID", "low", 0 };
  46. #endif /* HAVE_RTADV */
  47. /* We don't have a tidy top-level instance object for zebra, or interfaces */
  48. static struct zebra_if_defaults zif_defaults = {
  49. .linkdetect = IF_LINKDETECT_UNSPEC,
  50. };
  51. /* helper only for if_zebra_linkdetect */
  52. static void
  53. if_zebra_linkdetect_set_val (struct interface *ifp, zebra_if_linkdetect val)
  54. {
  55. switch (val)
  56. {
  57. case IF_LINKDETECT_ON:
  58. SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
  59. break;
  60. case IF_LINKDETECT_OFF:
  61. UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
  62. break;
  63. default: break;
  64. }
  65. }
  66. static void
  67. if_zebra_linkdetect_set (struct interface *ifp)
  68. {
  69. struct zebra_if *zif = ifp->info;
  70. assert (zif != NULL);
  71. int if_was_operative = if_is_operative(ifp);
  72. /* If user has explicitly configured for the interface, let that set */
  73. if (zif->linkdetect != IF_LINKDETECT_UNSPEC)
  74. if_zebra_linkdetect_set_val (ifp, zif->linkdetect);
  75. else
  76. {
  77. /* general compiled in default is to set */
  78. SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
  79. /* but user can specify a default too */
  80. if_zebra_linkdetect_set_val (ifp, zif_defaults.linkdetect);
  81. }
  82. /* When linkdetection is enabled, interface might come down */
  83. if (!if_is_operative(ifp) && if_was_operative) if_down(ifp);
  84. /* Alternatively, it may come up after disabling link detection */
  85. if (if_is_operative(ifp) && !if_was_operative) if_up(ifp);
  86. }
  87. /* Called when new interface is added. */
  88. static int
  89. if_zebra_new_hook (struct interface *ifp)
  90. {
  91. struct zebra_if *zebra_if;
  92. zebra_if = XCALLOC (MTYPE_TMP, sizeof (struct zebra_if));
  93. zebra_if->multicast = IF_ZEBRA_MULTICAST_UNSPEC;
  94. zebra_if->shutdown = IF_ZEBRA_SHUTDOWN_OFF;
  95. switch (zif_defaults.linkdetect)
  96. {
  97. case IF_LINKDETECT_OFF:
  98. UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
  99. break;
  100. case IF_LINKDETECT_UNSPEC:
  101. case IF_LINKDETECT_ON:
  102. default:
  103. SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
  104. break;
  105. }
  106. #if defined (HAVE_RTADV)
  107. {
  108. /* Set default router advertise values. */
  109. struct rtadvconf *rtadv;
  110. rtadv = &zebra_if->rtadv;
  111. rtadv->AdvSendAdvertisements = 0;
  112. rtadv->MaxRtrAdvInterval = RTADV_MAX_RTR_ADV_INTERVAL;
  113. rtadv->MinRtrAdvInterval = RTADV_MIN_RTR_ADV_INTERVAL;
  114. rtadv->AdvIntervalTimer = 0;
  115. rtadv->AdvManagedFlag = 0;
  116. rtadv->AdvOtherConfigFlag = 0;
  117. rtadv->AdvHomeAgentFlag = 0;
  118. rtadv->AdvLinkMTU = 0;
  119. rtadv->AdvReachableTime = 0;
  120. rtadv->AdvRetransTimer = 0;
  121. rtadv->AdvCurHopLimit = 0;
  122. rtadv->AdvDefaultLifetime = -1; /* derive from MaxRtrAdvInterval */
  123. rtadv->HomeAgentPreference = 0;
  124. rtadv->HomeAgentLifetime = -1; /* derive from AdvDefaultLifetime */
  125. rtadv->AdvIntervalOption = 0;
  126. rtadv->DefaultPreference = RTADV_PREF_MEDIUM;
  127. rtadv->AdvPrefixList = list_new ();
  128. }
  129. #endif /* HAVE_RTADV */
  130. /* Initialize installed address chains tree. */
  131. zebra_if->ipv4_subnets = route_table_init ();
  132. ifp->info = zebra_if;
  133. return 0;
  134. }
  135. /* Called when interface is deleted. */
  136. static int
  137. if_zebra_delete_hook (struct interface *ifp)
  138. {
  139. struct zebra_if *zebra_if;
  140. if (ifp->info)
  141. {
  142. zebra_if = ifp->info;
  143. /* Free installed address chains tree. */
  144. if (zebra_if->ipv4_subnets)
  145. route_table_finish (zebra_if->ipv4_subnets);
  146. XFREE (MTYPE_TMP, zebra_if);
  147. }
  148. return 0;
  149. }
  150. /* Tie an interface address to its derived subnet list of addresses. */
  151. int
  152. if_subnet_add (struct interface *ifp, struct connected *ifc)
  153. {
  154. struct route_node *rn;
  155. struct zebra_if *zebra_if;
  156. struct prefix cp;
  157. struct list *addr_list;
  158. assert (ifp && ifp->info && ifc);
  159. zebra_if = ifp->info;
  160. /* Get address derived subnet node and associated address list, while marking
  161. address secondary attribute appropriately. */
  162. cp = *ifc->address;
  163. apply_mask (&cp);
  164. rn = route_node_get (zebra_if->ipv4_subnets, &cp);
  165. if ((addr_list = rn->info))
  166. SET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
  167. else
  168. {
  169. UNSET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
  170. rn->info = addr_list = list_new ();
  171. route_lock_node (rn);
  172. }
  173. /* Tie address at the tail of address list. */
  174. listnode_add (addr_list, ifc);
  175. /* Return list element count. */
  176. return (addr_list->count);
  177. }
  178. /* Untie an interface address from its derived subnet list of addresses. */
  179. int
  180. if_subnet_delete (struct interface *ifp, struct connected *ifc)
  181. {
  182. struct route_node *rn;
  183. struct zebra_if *zebra_if;
  184. struct list *addr_list;
  185. assert (ifp && ifp->info && ifc);
  186. zebra_if = ifp->info;
  187. /* Get address derived subnet node. */
  188. rn = route_node_lookup (zebra_if->ipv4_subnets, ifc->address);
  189. if (! (rn && rn->info))
  190. {
  191. zlog_warn("Trying to remove an address from an unknown subnet."
  192. " (please report this bug)");
  193. return -1;
  194. }
  195. route_unlock_node (rn);
  196. /* Untie address from subnet's address list. */
  197. addr_list = rn->info;
  198. /* Deleting an address that is not registered is a bug.
  199. * In any case, we shouldn't decrement the lock counter if the address
  200. * is unknown. */
  201. if (!listnode_lookup(addr_list, ifc))
  202. {
  203. zlog_warn("Trying to remove an address from a subnet where it is not"
  204. " currently registered. (please report this bug)");
  205. return -1;
  206. }
  207. listnode_delete (addr_list, ifc);
  208. route_unlock_node (rn);
  209. /* Return list element count, if not empty. */
  210. if (addr_list->count)
  211. {
  212. /* If deleted address is primary, mark subsequent one as such and distribute. */
  213. if (! CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY))
  214. {
  215. ifc = listgetdata ((struct listnode *)listhead (addr_list));
  216. zebra_interface_address_delete_update (ifp, ifc);
  217. UNSET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
  218. /* XXX: Linux kernel removes all the secondary addresses when the primary
  219. * address is removed. We could try to work around that, though this is
  220. * non-trivial. */
  221. zebra_interface_address_add_update (ifp, ifc);
  222. }
  223. return addr_list->count;
  224. }
  225. /* Otherwise, free list and route node. */
  226. list_free (addr_list);
  227. rn->info = NULL;
  228. route_unlock_node (rn);
  229. return 0;
  230. }
  231. /* if_flags_mangle: A place for hacks that require mangling
  232. * or tweaking the interface flags.
  233. *
  234. * ******************** Solaris flags hacks **************************
  235. *
  236. * Solaris IFF_UP flag reflects only the primary interface as the
  237. * routing socket only sends IFINFO for the primary interface. Hence
  238. * ~IFF_UP does not per se imply all the logical interfaces are also
  239. * down - which we only know of as addresses. Instead we must determine
  240. * whether the interface really is up or not according to how many
  241. * addresses are still attached. (Solaris always sends RTM_DELADDR if
  242. * an interface, logical or not, goes ~IFF_UP).
  243. *
  244. * Ie, we mangle IFF_UP to *additionally* reflect whether or not there
  245. * are addresses left in struct connected, not just the actual underlying
  246. * IFF_UP flag.
  247. *
  248. * We must hence remember the real state of IFF_UP, which we do in
  249. * struct zebra_if.primary_state.
  250. *
  251. * Setting IFF_UP within zebra to administratively shutdown the
  252. * interface will affect only the primary interface/address on Solaris.
  253. ************************End Solaris flags hacks ***********************
  254. */
  255. static void
  256. if_flags_mangle (struct interface *ifp, uint64_t *newflags)
  257. {
  258. #ifdef SUNOS_5
  259. struct zebra_if *zif = ifp->info;
  260. zif->primary_state = *newflags & (IFF_UP & 0xff);
  261. if (CHECK_FLAG (zif->primary_state, IFF_UP)
  262. || listcount(ifp->connected) > 0)
  263. SET_FLAG (*newflags, IFF_UP);
  264. else
  265. UNSET_FLAG (*newflags, IFF_UP);
  266. #endif /* SUNOS_5 */
  267. }
  268. /* Update the flags field of the ifp with the new flag set provided.
  269. * Take whatever actions are required for any changes in flags we care
  270. * about.
  271. *
  272. * newflags should be the raw value, as obtained from the OS.
  273. */
  274. void
  275. if_flags_update (struct interface *ifp, uint64_t newflags)
  276. {
  277. if_flags_mangle (ifp, &newflags);
  278. if (if_is_operative (ifp))
  279. {
  280. /* operative -> inoperative? */
  281. ifp->flags = newflags;
  282. if (!if_is_operative (ifp))
  283. if_down (ifp);
  284. }
  285. else
  286. {
  287. /* inoperative -> operative? */
  288. ifp->flags = newflags;
  289. if (if_is_operative (ifp))
  290. if_up (ifp);
  291. }
  292. }
  293. /* Wake up configured address if it is not in current kernel
  294. address. */
  295. static void
  296. if_addr_wakeup (struct interface *ifp)
  297. {
  298. struct listnode *node, *nnode;
  299. struct connected *ifc;
  300. struct prefix *p;
  301. int ret;
  302. for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, ifc))
  303. {
  304. p = ifc->address;
  305. if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED)
  306. && ! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED))
  307. {
  308. /* Address check. */
  309. if (p->family == AF_INET)
  310. {
  311. if (! if_is_up (ifp))
  312. {
  313. /* Assume zebra is configured like following:
  314. *
  315. * interface gre0
  316. * ip addr 192.0.2.1/24
  317. * !
  318. *
  319. * As soon as zebra becomes first aware that gre0 exists in the
  320. * kernel, it will set gre0 up and configure its addresses.
  321. *
  322. * (This may happen at startup when the interface already exists
  323. * or during runtime when the interface is added to the kernel)
  324. *
  325. * XXX: IRDP code is calling here via if_add_update - this seems
  326. * somewhat weird.
  327. * XXX: RUNNING is not a settable flag on any system
  328. * I (paulj) am aware of.
  329. */
  330. if_set_flags (ifp, IFF_UP | IFF_RUNNING);
  331. if_refresh (ifp);
  332. }
  333. ret = if_set_prefix (ifp, ifc);
  334. if (ret < 0)
  335. {
  336. zlog_warn ("Can't set interface's address: %s",
  337. safe_strerror(errno));
  338. continue;
  339. }
  340. SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
  341. /* The address will be advertised to zebra clients when the notification
  342. * from the kernel has been received.
  343. * It will also be added to the interface's subnet list then. */
  344. }
  345. #ifdef HAVE_IPV6
  346. if (p->family == AF_INET6)
  347. {
  348. if (! if_is_up (ifp))
  349. {
  350. /* See long comment above */
  351. if_set_flags (ifp, IFF_UP | IFF_RUNNING);
  352. if_refresh (ifp);
  353. }
  354. ret = if_prefix_add_ipv6 (ifp, ifc);
  355. if (ret < 0)
  356. {
  357. zlog_warn ("Can't set interface's address: %s",
  358. safe_strerror(errno));
  359. continue;
  360. }
  361. SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
  362. /* The address will be advertised to zebra clients when the notification
  363. * from the kernel has been received. */
  364. }
  365. #endif /* HAVE_IPV6 */
  366. }
  367. }
  368. }
  369. static void if_count_up(struct zebra_if *zif)
  370. {
  371. event_counter_inc(&zif->up_events);
  372. }
  373. static void if_count_down(struct zebra_if *zif)
  374. {
  375. event_counter_inc(&zif->down_events);
  376. }
  377. void
  378. if_startup_count_up (void)
  379. {
  380. vrf_iter_t iter;
  381. struct interface *ifp;
  382. struct zebra_if *zif;
  383. struct listnode *node;
  384. for (iter = vrf_first(); iter != VRF_ITER_INVALID; iter = vrf_next(iter))
  385. {
  386. for (ALL_LIST_ELEMENTS_RO (vrf_iter2iflist(iter), node, ifp))
  387. {
  388. zif = ifp->info;
  389. if (!zif->up_events.count && if_is_operative(ifp))
  390. if_count_up(zif);
  391. }
  392. }
  393. }
  394. /* Handle interface addition */
  395. void
  396. if_add_update (struct interface *ifp)
  397. {
  398. struct zebra_if *if_data;
  399. if_data = ifp->info;
  400. assert(if_data);
  401. if (if_data->multicast == IF_ZEBRA_MULTICAST_ON)
  402. if_set_flags (ifp, IFF_MULTICAST);
  403. else if (if_data->multicast == IF_ZEBRA_MULTICAST_OFF)
  404. if_unset_flags (ifp, IFF_MULTICAST);
  405. zebra_interface_add_update (ifp);
  406. if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  407. {
  408. SET_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE);
  409. if (if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)
  410. {
  411. if (IS_ZEBRA_DEBUG_KERNEL)
  412. zlog_debug ("interface %s vrf %u index %d is shutdown. "
  413. "Won't wake it up.",
  414. ifp->name, ifp->vrf_id, ifp->ifindex);
  415. return;
  416. }
  417. if_addr_wakeup (ifp);
  418. if (IS_ZEBRA_DEBUG_KERNEL)
  419. zlog_debug ("interface %s vrf %u index %d becomes active.",
  420. ifp->name, ifp->vrf_id, ifp->ifindex);
  421. }
  422. else
  423. {
  424. if (IS_ZEBRA_DEBUG_KERNEL)
  425. zlog_debug ("interface %s vrf %u index %d is added.",
  426. ifp->name, ifp->vrf_id, ifp->ifindex);
  427. }
  428. if (host_config_get())
  429. {
  430. /* If configuration and therefore link-detect have already been
  431. * loaded, count an initial up event when new interfaces are added
  432. * in up state.
  433. * If configuration has not been loaded yet, this is handled by
  434. * if_startup_count_up which is called after reading the config. */
  435. if (!if_data->up_events.count && if_is_operative(ifp))
  436. if_count_up(if_data);
  437. }
  438. }
  439. /* Handle an interface delete event */
  440. void
  441. if_delete_update (struct interface *ifp)
  442. {
  443. struct connected *ifc;
  444. struct prefix *p;
  445. struct route_node *rn;
  446. struct zebra_if *zebra_if;
  447. zebra_if = ifp->info;
  448. if (if_is_up(ifp))
  449. {
  450. zlog_err ("interface %s vrf %u index %d is still up while being deleted.",
  451. ifp->name, ifp->vrf_id, ifp->ifindex);
  452. return;
  453. }
  454. /* Mark interface as inactive */
  455. UNSET_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE);
  456. if (IS_ZEBRA_DEBUG_KERNEL)
  457. zlog_debug ("interface %s vrf %u index %d is now inactive.",
  458. ifp->name, ifp->vrf_id, ifp->ifindex);
  459. /* Delete connected routes from the kernel. */
  460. if (ifp->connected)
  461. {
  462. struct listnode *node;
  463. struct listnode *last = NULL;
  464. while ((node = (last ? last->next : listhead (ifp->connected))))
  465. {
  466. ifc = listgetdata (node);
  467. p = ifc->address;
  468. if (p->family == AF_INET
  469. && (rn = route_node_lookup (zebra_if->ipv4_subnets, p)))
  470. {
  471. struct listnode *anode;
  472. struct listnode *next;
  473. struct listnode *first;
  474. struct list *addr_list;
  475. route_unlock_node (rn);
  476. addr_list = (struct list *) rn->info;
  477. /* Remove addresses, secondaries first. */
  478. first = listhead (addr_list);
  479. for (anode = first->next; anode || first; anode = next)
  480. {
  481. if (!anode)
  482. {
  483. anode = first;
  484. first = NULL;
  485. }
  486. next = anode->next;
  487. ifc = listgetdata (anode);
  488. p = ifc->address;
  489. connected_down_ipv4 (ifp, ifc);
  490. /* XXX: We have to send notifications here explicitly, because we destroy
  491. * the ifc before receiving the notification about the address being deleted.
  492. */
  493. zebra_interface_address_delete_update (ifp, ifc);
  494. UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
  495. UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
  496. /* Remove from subnet chain. */
  497. list_delete_node (addr_list, anode);
  498. route_unlock_node (rn);
  499. /* Remove from interface address list (unconditionally). */
  500. if (!CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  501. {
  502. listnode_delete (ifp->connected, ifc);
  503. connected_free (ifc);
  504. }
  505. else
  506. last = node;
  507. }
  508. /* Free chain list and respective route node. */
  509. list_delete (addr_list);
  510. rn->info = NULL;
  511. route_unlock_node (rn);
  512. }
  513. #ifdef HAVE_IPV6
  514. else if (p->family == AF_INET6)
  515. {
  516. connected_down_ipv6 (ifp, ifc);
  517. zebra_interface_address_delete_update (ifp, ifc);
  518. UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
  519. UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
  520. if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  521. last = node;
  522. else
  523. {
  524. listnode_delete (ifp->connected, ifc);
  525. connected_free (ifc);
  526. }
  527. }
  528. #endif /* HAVE_IPV6 */
  529. else
  530. {
  531. last = node;
  532. }
  533. }
  534. }
  535. zebra_interface_delete_update (ifp);
  536. /* Update ifindex after distributing the delete message. This is in
  537. case any client needs to have the old value of ifindex available
  538. while processing the deletion. Each client daemon is responsible
  539. for setting ifindex to IFINDEX_INTERNAL after processing the
  540. interface deletion message. */
  541. ifp->ifindex = IFINDEX_INTERNAL;
  542. }
  543. /* Interface is up. */
  544. void
  545. if_up (struct interface *ifp)
  546. {
  547. struct listnode *node;
  548. struct listnode *next;
  549. struct connected *ifc;
  550. struct prefix *p;
  551. if_count_up(ifp->info);
  552. /* Notify the protocol daemons. */
  553. zebra_interface_up_update (ifp);
  554. /* Install connected routes to the kernel. */
  555. if (ifp->connected)
  556. {
  557. for (ALL_LIST_ELEMENTS (ifp->connected, node, next, ifc))
  558. {
  559. p = ifc->address;
  560. if (p->family == AF_INET)
  561. connected_up_ipv4 (ifp, ifc);
  562. #ifdef HAVE_IPV6
  563. else if (p->family == AF_INET6)
  564. connected_up_ipv6 (ifp, ifc);
  565. #endif /* HAVE_IPV6 */
  566. }
  567. }
  568. /* Examine all static routes. */
  569. rib_update (ifp->vrf_id);
  570. }
  571. /* Interface goes down. We have to manage different behavior of based
  572. OS. */
  573. void
  574. if_down (struct interface *ifp)
  575. {
  576. struct listnode *node;
  577. struct listnode *next;
  578. struct connected *ifc;
  579. struct prefix *p;
  580. struct zebra_if *zif;
  581. zif = ifp->info;
  582. if (zif->up_events.count)
  583. if_count_down(zif);
  584. /* Notify to the protocol daemons. */
  585. zebra_interface_down_update (ifp);
  586. /* Delete connected routes from the kernel. */
  587. if (ifp->connected)
  588. {
  589. for (ALL_LIST_ELEMENTS (ifp->connected, node, next, ifc))
  590. {
  591. p = ifc->address;
  592. if (p->family == AF_INET)
  593. connected_down_ipv4 (ifp, ifc);
  594. #ifdef HAVE_IPV6
  595. else if (p->family == AF_INET6)
  596. connected_down_ipv6 (ifp, ifc);
  597. #endif /* HAVE_IPV6 */
  598. }
  599. }
  600. /* Examine all static routes which direct to the interface. */
  601. rib_update (ifp->vrf_id);
  602. }
  603. void
  604. if_refresh (struct interface *ifp)
  605. {
  606. if_get_flags (ifp);
  607. }
  608. /* Output prefix string to vty. */
  609. static int
  610. prefix_vty_out (struct vty *vty, struct prefix *p)
  611. {
  612. char str[INET6_ADDRSTRLEN];
  613. inet_ntop (p->family, &p->u.prefix, str, sizeof (str));
  614. vty_out (vty, "%s", str);
  615. return strlen (str);
  616. }
  617. /* Dump if address information to vty. */
  618. static void
  619. connected_dump_vty (struct vty *vty, struct connected *connected)
  620. {
  621. struct prefix *p;
  622. /* Print interface address. */
  623. p = connected->address;
  624. vty_out (vty, " %s ", prefix_family_str (p));
  625. prefix_vty_out (vty, p);
  626. vty_out (vty, "/%d", p->prefixlen);
  627. /* If there is destination address, print it. */
  628. if (connected->destination)
  629. {
  630. vty_out (vty, (CONNECTED_PEER(connected) ? " peer " : " broadcast "));
  631. prefix_vty_out (vty, connected->destination);
  632. }
  633. if (CHECK_FLAG (connected->flags, ZEBRA_IFA_SECONDARY))
  634. vty_out (vty, " secondary");
  635. if (CHECK_FLAG (connected->flags, ZEBRA_IFA_UNNUMBERED))
  636. vty_out (vty, " unnumbered");
  637. if (connected->label)
  638. vty_out (vty, " %s", connected->label);
  639. vty_out (vty, "%s", VTY_NEWLINE);
  640. }
  641. #if defined (HAVE_RTADV)
  642. /* Dump interface ND information to vty. */
  643. static void
  644. nd_dump_vty (struct vty *vty, struct interface *ifp)
  645. {
  646. struct zebra_if *zif;
  647. struct rtadvconf *rtadv;
  648. int interval;
  649. zif = (struct zebra_if *) ifp->info;
  650. rtadv = &zif->rtadv;
  651. if (rtadv->AdvSendAdvertisements)
  652. {
  653. vty_out (vty, " ND advertised reachable time is %d milliseconds%s",
  654. rtadv->AdvReachableTime, VTY_NEWLINE);
  655. vty_out (vty, " ND advertised retransmit interval is %d milliseconds%s",
  656. rtadv->AdvRetransTimer, VTY_NEWLINE);
  657. interval = rtadv->MaxRtrAdvInterval;
  658. if (interval % 1000)
  659. vty_out (vty, " ND router advertisements are sent every "
  660. "%d milliseconds%s", interval,
  661. VTY_NEWLINE);
  662. else
  663. vty_out (vty, " ND router advertisements are sent every "
  664. "%d seconds%s", interval / 1000,
  665. VTY_NEWLINE);
  666. if (rtadv->AdvDefaultLifetime != -1)
  667. vty_out (vty, " ND router advertisements live for %d seconds%s",
  668. rtadv->AdvDefaultLifetime, VTY_NEWLINE);
  669. else
  670. vty_out (vty, " ND router advertisements lifetime tracks ra-interval%s",
  671. VTY_NEWLINE);
  672. vty_out (vty, " ND router advertisement default router preference is "
  673. "%s%s", rtadv_pref_strs[rtadv->DefaultPreference],
  674. VTY_NEWLINE);
  675. if (rtadv->AdvManagedFlag)
  676. vty_out (vty, " Hosts use DHCP to obtain routable addresses.%s",
  677. VTY_NEWLINE);
  678. else
  679. vty_out (vty, " Hosts use stateless autoconfig for addresses.%s",
  680. VTY_NEWLINE);
  681. if (rtadv->AdvHomeAgentFlag)
  682. {
  683. vty_out (vty, " ND router advertisements with "
  684. "Home Agent flag bit set.%s",
  685. VTY_NEWLINE);
  686. if (rtadv->HomeAgentLifetime != -1)
  687. vty_out (vty, " Home Agent lifetime is %u seconds%s",
  688. rtadv->HomeAgentLifetime, VTY_NEWLINE);
  689. else
  690. vty_out (vty, " Home Agent lifetime tracks ra-lifetime%s",
  691. VTY_NEWLINE);
  692. vty_out (vty, " Home Agent preference is %u%s",
  693. rtadv->HomeAgentPreference, VTY_NEWLINE);
  694. }
  695. if (rtadv->AdvIntervalOption)
  696. vty_out (vty, " ND router advertisements with Adv. Interval option.%s",
  697. VTY_NEWLINE);
  698. }
  699. }
  700. #endif /* HAVE_RTADV */
  701. /* Interface's information print out to vty interface. */
  702. static void
  703. if_dump_vty (struct vty *vty, struct interface *ifp)
  704. {
  705. struct connected *connected;
  706. struct listnode *node;
  707. struct route_node *rn;
  708. struct zebra_if *zebra_if;
  709. zebra_if = ifp->info;
  710. vty_out (vty, "Interface %s is ", ifp->name);
  711. if (if_is_up(ifp)) {
  712. vty_out (vty, "up, line protocol ");
  713. if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)) {
  714. if (if_is_running(ifp))
  715. vty_out (vty, "is up%s", VTY_NEWLINE);
  716. else
  717. vty_out (vty, "is down%s", VTY_NEWLINE);
  718. } else {
  719. vty_out (vty, "detection is disabled%s", VTY_NEWLINE);
  720. }
  721. } else {
  722. vty_out (vty, "down%s", VTY_NEWLINE);
  723. }
  724. vty_out (vty, " Link ups: %s%s",
  725. event_counter_format(&zebra_if->up_events), VTY_NEWLINE);
  726. vty_out (vty, " Link downs: %s%s",
  727. event_counter_format(&zebra_if->down_events), VTY_NEWLINE);
  728. vty_out (vty, " vrf: %u%s", ifp->vrf_id, VTY_NEWLINE);
  729. if (ifp->desc)
  730. vty_out (vty, " Description: %s%s", ifp->desc,
  731. VTY_NEWLINE);
  732. if (ifp->ifindex == IFINDEX_INTERNAL)
  733. {
  734. vty_out(vty, " pseudo interface%s", VTY_NEWLINE);
  735. return;
  736. }
  737. else if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  738. {
  739. vty_out(vty, " index %d inactive interface%s",
  740. ifp->ifindex,
  741. VTY_NEWLINE);
  742. return;
  743. }
  744. vty_out (vty, " index %d metric %d mtu %d ",
  745. ifp->ifindex, ifp->metric, ifp->mtu);
  746. #ifdef HAVE_IPV6
  747. if (ifp->mtu6 != ifp->mtu)
  748. vty_out (vty, "mtu6 %d ", ifp->mtu6);
  749. #endif
  750. vty_out (vty, "%s flags: %s%s", VTY_NEWLINE,
  751. if_flag_dump (ifp->flags), VTY_NEWLINE);
  752. /* Hardware address. */
  753. vty_out (vty, " Type: %s%s", if_link_type_str (ifp->ll_type), VTY_NEWLINE);
  754. if (ifp->hw_addr_len != 0)
  755. {
  756. int i;
  757. vty_out (vty, " HWaddr: ");
  758. for (i = 0; i < ifp->hw_addr_len; i++)
  759. vty_out (vty, "%s%02x", i == 0 ? "" : ":", ifp->hw_addr[i]);
  760. vty_out (vty, "%s", VTY_NEWLINE);
  761. }
  762. /* Bandwidth in kbps */
  763. if (ifp->bandwidth != 0)
  764. {
  765. vty_out(vty, " bandwidth %u kbps", ifp->bandwidth);
  766. vty_out(vty, "%s", VTY_NEWLINE);
  767. }
  768. for (rn = route_top (zebra_if->ipv4_subnets); rn; rn = route_next (rn))
  769. {
  770. if (! rn->info)
  771. continue;
  772. for (ALL_LIST_ELEMENTS_RO ((struct list *)rn->info, node, connected))
  773. connected_dump_vty (vty, connected);
  774. }
  775. for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
  776. {
  777. if (CHECK_FLAG (connected->conf, ZEBRA_IFC_REAL) &&
  778. (connected->address->family == AF_INET6))
  779. connected_dump_vty (vty, connected);
  780. }
  781. if (HAS_LINK_PARAMS(ifp))
  782. {
  783. int i;
  784. struct if_link_params *iflp = ifp->link_params;
  785. vty_out(vty, " Traffic Engineering Link Parameters:%s", VTY_NEWLINE);
  786. if (IS_PARAM_SET(iflp, LP_TE))
  787. vty_out(vty, " TE metric %u%s",iflp->te_metric, VTY_NEWLINE);
  788. if (IS_PARAM_SET(iflp, LP_MAX_BW))
  789. vty_out(vty, " Maximum Bandwidth %g (Byte/s)%s", iflp->max_bw, VTY_NEWLINE);
  790. if (IS_PARAM_SET(iflp, LP_MAX_RSV_BW))
  791. vty_out(vty, " Maximum Reservable Bandwidth %g (Byte/s)%s", iflp->max_rsv_bw, VTY_NEWLINE);
  792. if (IS_PARAM_SET(iflp, LP_UNRSV_BW)) {
  793. vty_out(vty, " Unreserved Bandwidth per Class Type in Byte/s:%s", VTY_NEWLINE);
  794. for (i = 0; i < MAX_CLASS_TYPE; i+=2)
  795. vty_out(vty, " [%d]: %g (Bytes/sec),\t[%d]: %g (Bytes/sec)%s",
  796. i, iflp->unrsv_bw[i], i+1, iflp->unrsv_bw[i+1], VTY_NEWLINE);
  797. }
  798. if (IS_PARAM_SET(iflp, LP_ADM_GRP))
  799. vty_out(vty, " Administrative Group:%u%s", iflp->admin_grp, VTY_NEWLINE);
  800. if (IS_PARAM_SET(iflp, LP_DELAY))
  801. {
  802. vty_out(vty, " Link Delay Average: %u (micro-sec.)", iflp->av_delay);
  803. if (IS_PARAM_SET(iflp, LP_MM_DELAY))
  804. {
  805. vty_out(vty, " Min: %u (micro-sec.)", iflp->min_delay);
  806. vty_out(vty, " Max: %u (micro-sec.)", iflp->max_delay);
  807. }
  808. vty_out(vty, "%s", VTY_NEWLINE);
  809. }
  810. if (IS_PARAM_SET(iflp, LP_DELAY_VAR))
  811. vty_out(vty, " Link Delay Variation %u (micro-sec.)%s", iflp->delay_var, VTY_NEWLINE);
  812. if (IS_PARAM_SET(iflp, LP_PKT_LOSS))
  813. vty_out(vty, " Link Packet Loss %g (in %%)%s", iflp->pkt_loss, VTY_NEWLINE);
  814. if (IS_PARAM_SET(iflp, LP_AVA_BW))
  815. vty_out(vty, " Available Bandwidth %g (Byte/s)%s", iflp->ava_bw, VTY_NEWLINE);
  816. if (IS_PARAM_SET(iflp, LP_RES_BW))
  817. vty_out(vty, " Residual Bandwidth %g (Byte/s)%s", iflp->res_bw, VTY_NEWLINE);
  818. if (IS_PARAM_SET(iflp, LP_USE_BW))
  819. vty_out(vty, " Utilized Bandwidth %g (Byte/s)%s", iflp->use_bw, VTY_NEWLINE);
  820. if (IS_PARAM_SET(iflp, LP_RMT_AS))
  821. vty_out(vty, " Neighbor ASBR IP: %s AS: %u %s", inet_ntoa(iflp->rmt_ip), iflp->rmt_as, VTY_NEWLINE);
  822. }
  823. #ifdef RTADV
  824. nd_dump_vty (vty, ifp);
  825. #endif /* RTADV */
  826. #if defined (HAVE_RTADV)
  827. nd_dump_vty (vty, ifp);
  828. #endif /* HAVE_RTADV */
  829. #ifdef HAVE_PROC_NET_DEV
  830. /* Statistics print out using proc file system. */
  831. vty_out (vty, " %lu input packets (%lu multicast), %lu bytes, "
  832. "%lu dropped%s",
  833. ifp->stats.rx_packets, ifp->stats.rx_multicast,
  834. ifp->stats.rx_bytes, ifp->stats.rx_dropped, VTY_NEWLINE);
  835. vty_out (vty, " %lu input errors, %lu length, %lu overrun,"
  836. " %lu CRC, %lu frame%s",
  837. ifp->stats.rx_errors, ifp->stats.rx_length_errors,
  838. ifp->stats.rx_over_errors, ifp->stats.rx_crc_errors,
  839. ifp->stats.rx_frame_errors, VTY_NEWLINE);
  840. vty_out (vty, " %lu fifo, %lu missed%s", ifp->stats.rx_fifo_errors,
  841. ifp->stats.rx_missed_errors, VTY_NEWLINE);
  842. vty_out (vty, " %lu output packets, %lu bytes, %lu dropped%s",
  843. ifp->stats.tx_packets, ifp->stats.tx_bytes,
  844. ifp->stats.tx_dropped, VTY_NEWLINE);
  845. vty_out (vty, " %lu output errors, %lu aborted, %lu carrier,"
  846. " %lu fifo, %lu heartbeat%s",
  847. ifp->stats.tx_errors, ifp->stats.tx_aborted_errors,
  848. ifp->stats.tx_carrier_errors, ifp->stats.tx_fifo_errors,
  849. ifp->stats.tx_heartbeat_errors, VTY_NEWLINE);
  850. vty_out (vty, " %lu window, %lu collisions%s",
  851. ifp->stats.tx_window_errors, ifp->stats.collisions, VTY_NEWLINE);
  852. #endif /* HAVE_PROC_NET_DEV */
  853. #ifdef HAVE_NET_RT_IFLIST
  854. #if defined (__bsdi__) || defined (__NetBSD__)
  855. /* Statistics print out using sysctl (). */
  856. vty_out (vty, " input packets %llu, bytes %llu, dropped %llu,"
  857. " multicast packets %llu%s",
  858. (unsigned long long)ifp->stats.ifi_ipackets,
  859. (unsigned long long)ifp->stats.ifi_ibytes,
  860. (unsigned long long)ifp->stats.ifi_iqdrops,
  861. (unsigned long long)ifp->stats.ifi_imcasts,
  862. VTY_NEWLINE);
  863. vty_out (vty, " input errors %llu%s",
  864. (unsigned long long)ifp->stats.ifi_ierrors, VTY_NEWLINE);
  865. vty_out (vty, " output packets %llu, bytes %llu,"
  866. " multicast packets %llu%s",
  867. (unsigned long long)ifp->stats.ifi_opackets,
  868. (unsigned long long)ifp->stats.ifi_obytes,
  869. (unsigned long long)ifp->stats.ifi_omcasts,
  870. VTY_NEWLINE);
  871. vty_out (vty, " output errors %llu%s",
  872. (unsigned long long)ifp->stats.ifi_oerrors, VTY_NEWLINE);
  873. vty_out (vty, " collisions %llu%s",
  874. (unsigned long long)ifp->stats.ifi_collisions, VTY_NEWLINE);
  875. #else
  876. /* Statistics print out using sysctl (). */
  877. vty_out (vty, " input packets %lu, bytes %lu, dropped %lu,"
  878. " multicast packets %lu%s",
  879. ifp->stats.ifi_ipackets, ifp->stats.ifi_ibytes,
  880. ifp->stats.ifi_iqdrops, ifp->stats.ifi_imcasts,
  881. VTY_NEWLINE);
  882. vty_out (vty, " input errors %lu%s",
  883. ifp->stats.ifi_ierrors, VTY_NEWLINE);
  884. vty_out (vty, " output packets %lu, bytes %lu, multicast packets %lu%s",
  885. ifp->stats.ifi_opackets, ifp->stats.ifi_obytes,
  886. ifp->stats.ifi_omcasts, VTY_NEWLINE);
  887. vty_out (vty, " output errors %lu%s",
  888. ifp->stats.ifi_oerrors, VTY_NEWLINE);
  889. vty_out (vty, " collisions %lu%s",
  890. ifp->stats.ifi_collisions, VTY_NEWLINE);
  891. #endif /* __bsdi__ || __NetBSD__ */
  892. #endif /* HAVE_NET_RT_IFLIST */
  893. }
  894. /* Wrapper hook point for zebra daemon so that ifindex can be set
  895. * DEFUN macro not used as extract.pl HAS to ignore this
  896. * See also interface_cmd in lib/if.c
  897. */
  898. DEFUN_NOSH (zebra_interface,
  899. zebra_interface_cmd,
  900. "interface IFNAME",
  901. "Select an interface to configure\n"
  902. "Interface's name\n")
  903. {
  904. int ret;
  905. struct interface *ifp;
  906. /* Call lib interface() */
  907. if ((ret = interface_cmd.func (self, vty, argc, argv)) != CMD_SUCCESS)
  908. return ret;
  909. ifp = vty->index;
  910. if (ifp->ifindex == IFINDEX_INTERNAL)
  911. /* Is this really necessary? Shouldn't status be initialized to 0
  912. in that case? */
  913. UNSET_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE);
  914. return ret;
  915. }
  916. ALIAS (zebra_interface,
  917. zebra_interface_vrf_cmd,
  918. "interface IFNAME " VRF_CMD_STR,
  919. "Select an interface to configure\n"
  920. "Interface's name\n"
  921. VRF_CMD_HELP_STR)
  922. struct cmd_node interface_node =
  923. {
  924. INTERFACE_NODE,
  925. "%s(config-if)# ",
  926. 1
  927. };
  928. /* Show all interfaces to vty. */
  929. DEFUN (show_interface, show_interface_cmd,
  930. "show interface",
  931. SHOW_STR
  932. "Interface status and configuration\n")
  933. {
  934. struct listnode *node;
  935. struct interface *ifp;
  936. vrf_id_t vrf_id = VRF_DEFAULT;
  937. #ifdef HAVE_PROC_NET_DEV
  938. /* If system has interface statistics via proc file system, update
  939. statistics. */
  940. ifstat_update_proc ();
  941. #endif /* HAVE_PROC_NET_DEV */
  942. #ifdef HAVE_NET_RT_IFLIST
  943. ifstat_update_sysctl ();
  944. #endif /* HAVE_NET_RT_IFLIST */
  945. if (argc > 0)
  946. VTY_GET_INTEGER ("VRF ID", vrf_id, argv[0]);
  947. /* All interface print. */
  948. for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
  949. if_dump_vty (vty, ifp);
  950. return CMD_SUCCESS;
  951. }
  952. ALIAS (show_interface,
  953. show_interface_vrf_cmd,
  954. "show interface " VRF_CMD_STR,
  955. SHOW_STR
  956. "Interface status and configuration\n"
  957. VRF_CMD_HELP_STR)
  958. /* Show all interfaces to vty. */
  959. DEFUN (show_interface_vrf_all, show_interface_vrf_all_cmd,
  960. "show interface " VRF_ALL_CMD_STR,
  961. SHOW_STR
  962. "Interface status and configuration\n"
  963. VRF_ALL_CMD_HELP_STR)
  964. {
  965. struct listnode *node;
  966. struct interface *ifp;
  967. vrf_iter_t iter;
  968. #ifdef HAVE_PROC_NET_DEV
  969. /* If system has interface statistics via proc file system, update
  970. statistics. */
  971. ifstat_update_proc ();
  972. #endif /* HAVE_PROC_NET_DEV */
  973. #ifdef HAVE_NET_RT_IFLIST
  974. ifstat_update_sysctl ();
  975. #endif /* HAVE_NET_RT_IFLIST */
  976. /* All interface print. */
  977. for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
  978. for (ALL_LIST_ELEMENTS_RO (vrf_iter2iflist (iter), node, ifp))
  979. if_dump_vty (vty, ifp);
  980. return CMD_SUCCESS;
  981. }
  982. /* Show specified interface to vty. */
  983. DEFUN (show_interface_name, show_interface_name_cmd,
  984. "show interface IFNAME",
  985. SHOW_STR
  986. "Interface status and configuration\n"
  987. "Interface name\n")
  988. {
  989. struct interface *ifp;
  990. vrf_id_t vrf_id = VRF_DEFAULT;
  991. #ifdef HAVE_PROC_NET_DEV
  992. /* If system has interface statistics via proc file system, update
  993. statistics. */
  994. ifstat_update_proc ();
  995. #endif /* HAVE_PROC_NET_DEV */
  996. #ifdef HAVE_NET_RT_IFLIST
  997. ifstat_update_sysctl ();
  998. #endif /* HAVE_NET_RT_IFLIST */
  999. if (argc > 1)
  1000. VTY_GET_INTEGER ("VRF ID", vrf_id, argv[1]);
  1001. /* Specified interface print. */
  1002. ifp = if_lookup_by_name_vrf (argv[0], vrf_id);
  1003. if (ifp == NULL)
  1004. {
  1005. vty_out (vty, "%% Can't find interface %s%s", argv[0],
  1006. VTY_NEWLINE);
  1007. return CMD_WARNING;
  1008. }
  1009. if_dump_vty (vty, ifp);
  1010. return CMD_SUCCESS;
  1011. }
  1012. ALIAS (show_interface_name,
  1013. show_interface_name_vrf_cmd,
  1014. "show interface IFNAME " VRF_CMD_STR,
  1015. SHOW_STR
  1016. "Interface status and configuration\n"
  1017. "Interface name\n"
  1018. VRF_CMD_HELP_STR)
  1019. /* Show specified interface to vty. */
  1020. DEFUN (show_interface_name_vrf_all, show_interface_name_vrf_all_cmd,
  1021. "show interface IFNAME " VRF_ALL_CMD_STR,
  1022. SHOW_STR
  1023. "Interface status and configuration\n"
  1024. "Interface name\n"
  1025. VRF_ALL_CMD_HELP_STR)
  1026. {
  1027. struct interface *ifp;
  1028. vrf_iter_t iter;
  1029. int found = 0;
  1030. #ifdef HAVE_PROC_NET_DEV
  1031. /* If system has interface statistics via proc file system, update
  1032. statistics. */
  1033. ifstat_update_proc ();
  1034. #endif /* HAVE_PROC_NET_DEV */
  1035. #ifdef HAVE_NET_RT_IFLIST
  1036. ifstat_update_sysctl ();
  1037. #endif /* HAVE_NET_RT_IFLIST */
  1038. /* All interface print. */
  1039. for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
  1040. {
  1041. /* Specified interface print. */
  1042. ifp = if_lookup_by_name_vrf (argv[0], vrf_iter2id (iter));
  1043. if (ifp)
  1044. {
  1045. if_dump_vty (vty, ifp);
  1046. found++;
  1047. }
  1048. }
  1049. if (!found)
  1050. {
  1051. vty_out (vty, "%% Can't find interface %s%s", argv[0], VTY_NEWLINE);
  1052. return CMD_WARNING;
  1053. }
  1054. return CMD_SUCCESS;
  1055. }
  1056. static void
  1057. if_show_description (struct vty *vty, vrf_id_t vrf_id)
  1058. {
  1059. struct listnode *node;
  1060. struct interface *ifp;
  1061. vty_out (vty, "Interface Status Protocol Description%s", VTY_NEWLINE);
  1062. for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
  1063. {
  1064. int len;
  1065. len = vty_out (vty, "%s", ifp->name);
  1066. vty_out (vty, "%*s", (16 - len), " ");
  1067. if (if_is_up(ifp))
  1068. {
  1069. vty_out (vty, "up ");
  1070. if (CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION))
  1071. {
  1072. if (if_is_running(ifp))
  1073. vty_out (vty, "up ");
  1074. else
  1075. vty_out (vty, "down ");
  1076. }
  1077. else
  1078. {
  1079. vty_out (vty, "unknown ");
  1080. }
  1081. }
  1082. else
  1083. {
  1084. vty_out (vty, "down down ");
  1085. }
  1086. if (ifp->desc)
  1087. vty_out (vty, "%s", ifp->desc);
  1088. vty_out (vty, "%s", VTY_NEWLINE);
  1089. }
  1090. }
  1091. DEFUN (show_interface_desc,
  1092. show_interface_desc_cmd,
  1093. "show interface description",
  1094. SHOW_STR
  1095. "Interface status and configuration\n"
  1096. "Interface description\n")
  1097. {
  1098. vrf_id_t vrf_id = VRF_DEFAULT;
  1099. if (argc > 0)
  1100. VTY_GET_INTEGER ("VRF ID", vrf_id, argv[0]);
  1101. if_show_description (vty, vrf_id);
  1102. return CMD_SUCCESS;
  1103. }
  1104. ALIAS (show_interface_desc,
  1105. show_interface_desc_vrf_cmd,
  1106. "show interface description " VRF_CMD_STR,
  1107. SHOW_STR
  1108. "Interface status and configuration\n"
  1109. "Interface description\n"
  1110. VRF_CMD_HELP_STR)
  1111. DEFUN (show_interface_desc_vrf_all,
  1112. show_interface_desc_vrf_all_cmd,
  1113. "show interface description " VRF_ALL_CMD_STR,
  1114. SHOW_STR
  1115. "Interface status and configuration\n"
  1116. "Interface description\n"
  1117. VRF_ALL_CMD_HELP_STR)
  1118. {
  1119. vrf_iter_t iter;
  1120. for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
  1121. if (!list_isempty (vrf_iter2iflist (iter)))
  1122. {
  1123. vty_out (vty, "%s\tVRF %u%s%s", VTY_NEWLINE,
  1124. vrf_iter2id (iter),
  1125. VTY_NEWLINE, VTY_NEWLINE);
  1126. if_show_description (vty, vrf_iter2id (iter));
  1127. }
  1128. return CMD_SUCCESS;
  1129. }
  1130. DEFUN (multicast,
  1131. multicast_cmd,
  1132. "multicast",
  1133. "Set multicast flag to interface\n")
  1134. {
  1135. int ret;
  1136. struct interface *ifp;
  1137. struct zebra_if *if_data;
  1138. ifp = (struct interface *) vty->index;
  1139. if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  1140. {
  1141. ret = if_set_flags (ifp, IFF_MULTICAST);
  1142. if (ret < 0)
  1143. {
  1144. vty_out (vty, "Can't set multicast flag%s", VTY_NEWLINE);
  1145. return CMD_WARNING;
  1146. }
  1147. if_refresh (ifp);
  1148. }
  1149. if_data = ifp->info;
  1150. if_data->multicast = IF_ZEBRA_MULTICAST_ON;
  1151. return CMD_SUCCESS;
  1152. }
  1153. DEFUN (no_multicast,
  1154. no_multicast_cmd,
  1155. "no multicast",
  1156. NO_STR
  1157. "Unset multicast flag to interface\n")
  1158. {
  1159. int ret;
  1160. struct interface *ifp;
  1161. struct zebra_if *if_data;
  1162. ifp = (struct interface *) vty->index;
  1163. if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  1164. {
  1165. ret = if_unset_flags (ifp, IFF_MULTICAST);
  1166. if (ret < 0)
  1167. {
  1168. vty_out (vty, "Can't unset multicast flag%s", VTY_NEWLINE);
  1169. return CMD_WARNING;
  1170. }
  1171. if_refresh (ifp);
  1172. }
  1173. if_data = ifp->info;
  1174. if_data->multicast = IF_ZEBRA_MULTICAST_OFF;
  1175. return CMD_SUCCESS;
  1176. }
  1177. /* Hacky: create a dummy node just to hang a config-writer callback off it */
  1178. static struct cmd_node zebra_if_defaults_node = {
  1179. ZEBRA_IF_DEFAULTS_NODE,
  1180. "",
  1181. 1,
  1182. };
  1183. static int
  1184. config_write_zebra_if_defaults (struct vty *vty)
  1185. {
  1186. if (zif_defaults.linkdetect != IF_LINKDETECT_UNSPEC)
  1187. vty_out (vty, "default link-detect %s%s",
  1188. zif_defaults.linkdetect == IF_LINKDETECT_ON ? "on" : "off",
  1189. VTY_NEWLINE);
  1190. return 0;
  1191. }
  1192. DEFUN(default_linkdetect,
  1193. default_linkdetect_cmd,
  1194. "default link-detect (on|off)",
  1195. "Configure defaults of settings\n"
  1196. "Interface link detection\n"
  1197. "Interface link-detect defaults to enabled\n"
  1198. "Interface link-detect defaults to disabled\n")
  1199. {
  1200. zebra_if_linkdetect prev = zif_defaults.linkdetect;
  1201. struct listnode *node;
  1202. struct interface *ifp;
  1203. vrf_iter_t iter;
  1204. if (strcmp (argv[1], "on") == 0)
  1205. zif_defaults.linkdetect = IF_LINKDETECT_ON;
  1206. else
  1207. zif_defaults.linkdetect = IF_LINKDETECT_OFF;
  1208. if (zif_defaults.linkdetect != prev)
  1209. for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
  1210. for (ALL_LIST_ELEMENTS_RO (vrf_iter2iflist (iter), node, ifp))
  1211. if_zebra_linkdetect_set (ifp);
  1212. return CMD_SUCCESS;
  1213. }
  1214. DEFUN (linkdetect,
  1215. linkdetect_cmd,
  1216. "link-detect [default]",
  1217. "Enable link detection on interface\n"
  1218. "Leave link-detect to the default\n")
  1219. {
  1220. struct interface *ifp;
  1221. struct zebra_if *zif;
  1222. ifp = (struct interface *) vty->index;
  1223. zif = ifp->info;
  1224. assert (zif != NULL);
  1225. zif->linkdetect = IF_LINKDETECT_ON;
  1226. if_zebra_linkdetect_set (ifp);
  1227. /* FIXME: Will defer status change forwarding if interface
  1228. does not come down! */
  1229. return CMD_SUCCESS;
  1230. }
  1231. DEFUN (no_linkdetect,
  1232. no_linkdetect_cmd,
  1233. "no link-detect",
  1234. NO_STR
  1235. "Disable link detection on interface\n")
  1236. {
  1237. struct interface *ifp;
  1238. struct zebra_if *zif;
  1239. ifp = (struct interface *) vty->index;
  1240. zif = ifp->info;
  1241. assert (zif != NULL);
  1242. zif->linkdetect = IF_LINKDETECT_OFF;
  1243. if_zebra_linkdetect_set (ifp);
  1244. /* FIXME: see linkdetect_cmd */
  1245. return CMD_SUCCESS;
  1246. }
  1247. DEFUN (shutdown_if,
  1248. shutdown_if_cmd,
  1249. "shutdown",
  1250. "Shutdown the selected interface\n")
  1251. {
  1252. int ret;
  1253. struct interface *ifp;
  1254. struct zebra_if *if_data;
  1255. ifp = (struct interface *) vty->index;
  1256. if (ifp->ifindex != IFINDEX_INTERNAL)
  1257. {
  1258. ret = if_unset_flags (ifp, IFF_UP);
  1259. if (ret < 0)
  1260. {
  1261. vty_out (vty, "Can't shutdown interface%s", VTY_NEWLINE);
  1262. return CMD_WARNING;
  1263. }
  1264. if_refresh (ifp);
  1265. }
  1266. if_data = ifp->info;
  1267. if_data->shutdown = IF_ZEBRA_SHUTDOWN_ON;
  1268. return CMD_SUCCESS;
  1269. }
  1270. DEFUN (no_shutdown_if,
  1271. no_shutdown_if_cmd,
  1272. "no shutdown",
  1273. NO_STR
  1274. "Shutdown the selected interface\n")
  1275. {
  1276. int ret;
  1277. struct interface *ifp;
  1278. struct zebra_if *if_data;
  1279. ifp = (struct interface *) vty->index;
  1280. if (ifp->ifindex != IFINDEX_INTERNAL)
  1281. {
  1282. ret = if_set_flags (ifp, IFF_UP | IFF_RUNNING);
  1283. if (ret < 0)
  1284. {
  1285. vty_out (vty, "Can't up interface%s", VTY_NEWLINE);
  1286. return CMD_WARNING;
  1287. }
  1288. if_refresh (ifp);
  1289. /* Some addresses (in particular, IPv6 addresses on Linux) get
  1290. * removed when the interface goes down. They need to be readded.
  1291. */
  1292. if_addr_wakeup(ifp);
  1293. }
  1294. if_data = ifp->info;
  1295. if_data->shutdown = IF_ZEBRA_SHUTDOWN_OFF;
  1296. return CMD_SUCCESS;
  1297. }
  1298. DEFUN (bandwidth_if,
  1299. bandwidth_if_cmd,
  1300. "bandwidth <1-10000000>",
  1301. "Set bandwidth informational parameter\n"
  1302. "Bandwidth in kilobits\n")
  1303. {
  1304. struct interface *ifp;
  1305. unsigned int bandwidth;
  1306. ifp = (struct interface *) vty->index;
  1307. bandwidth = strtol(argv[0], NULL, 10);
  1308. /* bandwidth range is <1-10000000> */
  1309. if (bandwidth < 1 || bandwidth > 10000000)
  1310. {
  1311. vty_out (vty, "Bandwidth is invalid%s", VTY_NEWLINE);
  1312. return CMD_WARNING;
  1313. }
  1314. ifp->bandwidth = bandwidth;
  1315. /* force protocols to recalculate routes due to cost change */
  1316. if (if_is_operative (ifp))
  1317. zebra_interface_up_update (ifp);
  1318. return CMD_SUCCESS;
  1319. }
  1320. DEFUN (no_bandwidth_if,
  1321. no_bandwidth_if_cmd,
  1322. "no bandwidth",
  1323. NO_STR
  1324. "Set bandwidth informational parameter\n")
  1325. {
  1326. struct interface *ifp;
  1327. ifp = (struct interface *) vty->index;
  1328. ifp->bandwidth = 0;
  1329. /* force protocols to recalculate routes due to cost change */
  1330. if (if_is_operative (ifp))
  1331. zebra_interface_up_update (ifp);
  1332. return CMD_SUCCESS;
  1333. }
  1334. ALIAS (no_bandwidth_if,
  1335. no_bandwidth_if_val_cmd,
  1336. "no bandwidth <1-10000000>",
  1337. NO_STR
  1338. "Set bandwidth informational parameter\n"
  1339. "Bandwidth in kilobits\n")
  1340. struct cmd_node link_params_node =
  1341. {
  1342. LINK_PARAMS_NODE,
  1343. "%s(config-link-params)# ",
  1344. 1,
  1345. };
  1346. static void
  1347. link_param_cmd_set_uint32 (struct interface *ifp, uint32_t *field,
  1348. uint32_t type, uint32_t value)
  1349. {
  1350. /* Update field as needed */
  1351. if (IS_PARAM_UNSET(ifp->link_params, type) || *field != value)
  1352. {
  1353. *field = value;
  1354. SET_PARAM(ifp->link_params, type);
  1355. /* force protocols to update LINK STATE due to parameters change */
  1356. if (if_is_operative (ifp))
  1357. zebra_interface_parameters_update (ifp);
  1358. }
  1359. }
  1360. static void
  1361. link_param_cmd_set_float (struct interface *ifp, float *field,
  1362. uint32_t type, float value)
  1363. {
  1364. /* Update field as needed */
  1365. if (IS_PARAM_UNSET(ifp->link_params, type) || *field != value)
  1366. {
  1367. *field = value;
  1368. SET_PARAM(ifp->link_params, type);
  1369. /* force protocols to update LINK STATE due to parameters change */
  1370. if (if_is_operative (ifp))
  1371. zebra_interface_parameters_update (ifp);
  1372. }
  1373. }
  1374. static void
  1375. link_param_cmd_unset (struct interface *ifp, uint32_t type)
  1376. {
  1377. /* Unset field */
  1378. UNSET_PARAM(ifp->link_params, type);
  1379. /* force protocols to update LINK STATE due to parameters change */
  1380. if (if_is_operative (ifp))
  1381. zebra_interface_parameters_update (ifp);
  1382. }
  1383. DEFUN (link_params,
  1384. link_params_cmd,
  1385. "link-params",
  1386. LINK_PARAMS_STR)
  1387. {
  1388. vty->node = LINK_PARAMS_NODE;
  1389. return CMD_SUCCESS;
  1390. }
  1391. DEFUN (exit_link_params,
  1392. exit_link_params_cmd,
  1393. "exit-link-params",
  1394. "Exit from Link Params configuration mode\n")
  1395. {
  1396. if (vty->node == LINK_PARAMS_NODE)
  1397. vty->node = INTERFACE_NODE;
  1398. return CMD_SUCCESS;
  1399. }
  1400. /* Specific Traffic Engineering parameters commands */
  1401. DEFUN (link_params_enable,
  1402. link_params_enable_cmd,
  1403. "enable",
  1404. "Activate link parameters on this interface\n")
  1405. {
  1406. struct interface *ifp = (struct interface *) vty->index;
  1407. /* This command could be issue at startup, when activate MPLS TE */
  1408. /* on a new interface or after a ON / OFF / ON toggle */
  1409. /* In all case, TE parameters are reset to their default factory */
  1410. if (IS_ZEBRA_DEBUG_EVENT)
  1411. zlog_debug ("Link-params: enable TE link parameters on interface %s", ifp->name);
  1412. if (!if_link_params_get (ifp))
  1413. {
  1414. if (IS_ZEBRA_DEBUG_EVENT)
  1415. zlog_debug ("Link-params: failed to init TE link parameters %s", ifp->name);
  1416. return CMD_WARNING;
  1417. }
  1418. /* force protocols to update LINK STATE due to parameters change */
  1419. if (if_is_operative (ifp))
  1420. zebra_interface_parameters_update (ifp);
  1421. return CMD_SUCCESS;
  1422. }
  1423. DEFUN (no_link_params_enable,
  1424. no_link_params_enable_cmd,
  1425. "no enable",
  1426. NO_STR
  1427. "Disable link parameters on this interface\n")
  1428. {
  1429. struct interface *ifp = (struct interface *) vty->index;
  1430. zlog_debug ("MPLS-TE: disable TE link parameters on interface %s", ifp->name);
  1431. if_link_params_free (ifp);
  1432. /* force protocols to update LINK STATE due to parameters change */
  1433. if (if_is_operative (ifp))
  1434. zebra_interface_parameters_update (ifp);
  1435. return CMD_SUCCESS;
  1436. }
  1437. /* STANDARD TE metrics */
  1438. DEFUN (link_params_metric,
  1439. link_params_metric_cmd,
  1440. "metric <0-4294967295>",
  1441. "Link metric for MPLS-TE purpose\n"
  1442. "Metric value in decimal\n")
  1443. {
  1444. struct interface *ifp = (struct interface *) vty->index;
  1445. struct if_link_params *iflp = if_link_params_get (ifp);
  1446. u_int32_t metric;
  1447. VTY_GET_ULONG("metric", metric, argv[0]);
  1448. /* Update TE metric if needed */
  1449. link_param_cmd_set_uint32 (ifp, &iflp->te_metric, LP_TE, metric);
  1450. return CMD_SUCCESS;
  1451. }
  1452. DEFUN (no_link_params_metric,
  1453. no_link_params_metric_cmd,
  1454. "no metric",
  1455. NO_STR
  1456. "Disable Link Metric on this interface\n")
  1457. {
  1458. struct interface *ifp = (struct interface *) vty->index;
  1459. /* Unset TE Metric */
  1460. link_param_cmd_unset(ifp, LP_TE);
  1461. return CMD_SUCCESS;
  1462. }
  1463. DEFUN (link_params_maxbw,
  1464. link_params_maxbw_cmd,
  1465. "max-bw BANDWIDTH",
  1466. "Maximum bandwidth that can be used\n"
  1467. "Bytes/second (IEEE floating point format)\n")
  1468. {
  1469. struct interface *ifp = (struct interface *) vty->index;
  1470. struct if_link_params *iflp = if_link_params_get (ifp);
  1471. float bw;
  1472. if (sscanf (argv[0], "%g", &bw) != 1)
  1473. {
  1474. vty_out (vty, "link_params_maxbw: fscanf: %s%s", safe_strerror (errno),
  1475. VTY_NEWLINE);
  1476. return CMD_WARNING;
  1477. }
  1478. /* Check that Maximum bandwidth is not lower than other bandwidth parameters */
  1479. if ((bw <= iflp->max_rsv_bw)
  1480. || (bw <= iflp->unrsv_bw[0])
  1481. || (bw <= iflp->unrsv_bw[1])
  1482. || (bw <= iflp->unrsv_bw[2])
  1483. || (bw <= iflp->unrsv_bw[3])
  1484. || (bw <= iflp->unrsv_bw[4])
  1485. || (bw <= iflp->unrsv_bw[5])
  1486. || (bw <= iflp->unrsv_bw[6])
  1487. || (bw <= iflp->unrsv_bw[7])
  1488. || (bw <= iflp->ava_bw)
  1489. || (bw <= iflp->res_bw)
  1490. || (bw <= iflp->use_bw))
  1491. {
  1492. vty_out (vty,
  1493. "Maximum Bandwidth could not be lower than others bandwidth%s",
  1494. VTY_NEWLINE);
  1495. return CMD_WARNING;
  1496. }
  1497. /* Update Maximum Bandwidth if needed */
  1498. link_param_cmd_set_float (ifp, &iflp->max_bw, LP_MAX_BW, bw);
  1499. return CMD_SUCCESS;
  1500. }
  1501. DEFUN (link_params_max_rsv_bw,
  1502. link_params_max_rsv_bw_cmd,
  1503. "max-rsv-bw BANDWIDTH",
  1504. "Maximum bandwidth that may be reserved\n"
  1505. "Bytes/second (IEEE floating point format)\n")
  1506. {
  1507. struct interface *ifp = (struct interface *) vty->index;
  1508. struct if_link_params *iflp = if_link_params_get (ifp);
  1509. float bw;
  1510. if (sscanf (argv[0], "%g", &bw) != 1)
  1511. {
  1512. vty_out (vty, "link_params_max_rsv_bw: fscanf: %s%s", safe_strerror (errno),
  1513. VTY_NEWLINE);
  1514. return CMD_WARNING;
  1515. }
  1516. /* Check that bandwidth is not greater than maximum bandwidth parameter */
  1517. if (bw > iflp->max_bw)
  1518. {
  1519. vty_out (vty,
  1520. "Maximum Reservable Bandwidth could not be greater than Maximum Bandwidth (%g)%s",
  1521. iflp->max_bw, VTY_NEWLINE);
  1522. return CMD_WARNING;
  1523. }
  1524. /* Update Maximum Reservable Bandwidth if needed */
  1525. link_param_cmd_set_float (ifp, &iflp->max_rsv_bw, LP_MAX_RSV_BW, bw);
  1526. return CMD_SUCCESS;
  1527. }
  1528. DEFUN (link_params_unrsv_bw,
  1529. link_params_unrsv_bw_cmd,
  1530. "unrsv-bw <0-7> BANDWIDTH",
  1531. "Unreserved bandwidth at each priority level\n"
  1532. "Priority\n"
  1533. "Bytes/second (IEEE floating point format)\n")
  1534. {
  1535. struct interface *ifp = (struct interface *) vty->index;
  1536. struct if_link_params *iflp = if_link_params_get (ifp);
  1537. int priority;
  1538. float bw;
  1539. /* We don't have to consider about range check here. */
  1540. if (sscanf (argv[0], "%d", &priority) != 1)
  1541. {
  1542. vty_out (vty, "link_params_unrsv_bw: fscanf: %s%s", safe_strerror (errno),
  1543. VTY_NEWLINE);
  1544. return CMD_WARNING;
  1545. }
  1546. if (sscanf (argv[1], "%g", &bw) != 1)
  1547. {
  1548. vty_out (vty, "link_params_unrsv_bw: fscanf: %s%s", safe_strerror (errno),
  1549. VTY_NEWLINE);
  1550. return CMD_WARNING;
  1551. }
  1552. /* Check that bandwidth is not greater than maximum bandwidth parameter */
  1553. if (bw > iflp->max_bw)
  1554. {
  1555. vty_out (vty,
  1556. "UnReserved Bandwidth could not be greater than Maximum Bandwidth (%g)%s",
  1557. iflp->max_bw, VTY_NEWLINE);
  1558. return CMD_WARNING;
  1559. }
  1560. /* Update Unreserved Bandwidth if needed */
  1561. link_param_cmd_set_float (ifp, &iflp->unrsv_bw[priority], LP_UNRSV_BW, bw);
  1562. return CMD_SUCCESS;
  1563. }
  1564. DEFUN (link_params_admin_grp,
  1565. link_params_admin_grp_cmd,
  1566. "admin-grp BITPATTERN",
  1567. "Administrative group membership\n"
  1568. "32-bit Hexadecimal value (e.g. 0xa1)\n")
  1569. {
  1570. struct interface *ifp = (struct interface *) vty->index;
  1571. struct if_link_params *iflp = if_link_params_get (ifp);
  1572. unsigned long value;
  1573. if (sscanf (argv[0], "0x%lx", &value) != 1)
  1574. {
  1575. vty_out (vty, "link_params_admin_grp: fscanf: %s%s",
  1576. safe_strerror (errno), VTY_NEWLINE);
  1577. return CMD_WARNING;
  1578. }
  1579. /* Update Administrative Group if needed */
  1580. link_param_cmd_set_uint32 (ifp, &iflp->admin_grp, LP_ADM_GRP, value);
  1581. return CMD_SUCCESS;
  1582. }
  1583. DEFUN (no_link_params_admin_grp,
  1584. no_link_params_admin_grp_cmd,
  1585. "no admin-grp",
  1586. NO_STR
  1587. "Disable Administrative group membership on this interface\n")
  1588. {
  1589. struct interface *ifp = (struct interface *) vty->index;
  1590. /* Unset Admin Group */
  1591. link_param_cmd_unset(ifp, LP_ADM_GRP);
  1592. return CMD_SUCCESS;
  1593. }
  1594. /* RFC5392 & RFC5316: INTER-AS */
  1595. DEFUN (link_params_inter_as,
  1596. link_params_inter_as_cmd,
  1597. "neighbor A.B.C.D as <1-4294967295>",
  1598. "Configure remote ASBR information (Neighbor IP address and AS number)\n"
  1599. "Remote IP address in dot decimal A.B.C.D\n"
  1600. "Remote AS number\n"
  1601. "AS number in the range <1-4294967295>\n")
  1602. {
  1603. struct interface *ifp = (struct interface *) vty->index;
  1604. struct if_link_params *iflp = if_link_params_get (ifp);
  1605. struct in_addr addr;
  1606. u_int32_t as;
  1607. if (!inet_aton (argv[0], &addr))
  1608. {
  1609. vty_out (vty, "Please specify Router-Addr by A.B.C.D%s", VTY_NEWLINE);
  1610. return CMD_WARNING;
  1611. }
  1612. VTY_GET_ULONG("AS number", as, argv[1]);
  1613. /* Update Remote IP and Remote AS fields if needed */
  1614. if (IS_PARAM_UNSET(iflp, LP_RMT_AS)
  1615. || iflp->rmt_as != as
  1616. || iflp->rmt_ip.s_addr != addr.s_addr)
  1617. {
  1618. iflp->rmt_as = as;
  1619. iflp->rmt_ip.s_addr = addr.s_addr;
  1620. SET_PARAM(iflp, LP_RMT_AS);
  1621. /* force protocols to update LINK STATE due to parameters change */
  1622. if (if_is_operative (ifp))
  1623. zebra_interface_parameters_update (ifp);
  1624. }
  1625. return CMD_SUCCESS;
  1626. }
  1627. DEFUN (no_link_params_inter_as,
  1628. no_link_params_inter_as_cmd,
  1629. "no neighbor",
  1630. NO_STR
  1631. "Remove Neighbor IP address and AS number for Inter-AS TE\n")
  1632. {
  1633. struct interface *ifp = (struct interface *) vty->index;
  1634. struct if_link_params *iflp = if_link_params_get (ifp);
  1635. /* Reset Remote IP and AS neighbor */
  1636. iflp->rmt_as = 0;
  1637. iflp->rmt_ip.s_addr = 0;
  1638. UNSET_PARAM(iflp, LP_RMT_AS);
  1639. /* force protocols to update LINK STATE due to parameters change */
  1640. if (if_is_operative (ifp))
  1641. zebra_interface_parameters_update (ifp);
  1642. return CMD_SUCCESS;
  1643. }
  1644. /* RFC7471: OSPF Traffic Engineering (TE) Metric extensions & draft-ietf-isis-metric-extensions-07.txt */
  1645. DEFUN (link_params_delay,
  1646. link_params_delay_cmd,
  1647. "delay <0-16777215>",
  1648. "Unidirectional Average Link Delay\n"
  1649. "Average delay in micro-second as decimal (0...16777215)\n")
  1650. {
  1651. struct interface *ifp = (struct interface *) vty->index;
  1652. struct if_link_params *iflp = if_link_params_get (ifp);
  1653. u_int32_t delay = 0, low = 0, high = 0;
  1654. u_int8_t update = 0;
  1655. /* Get and Check new delay values */
  1656. VTY_GET_ULONG("delay", delay, argv[0]);
  1657. switch (argc)
  1658. {
  1659. case 1:
  1660. /* Check new delay value against old Min and Max delays if set */
  1661. if (IS_PARAM_SET(iflp, LP_MM_DELAY)
  1662. && (delay <= iflp->min_delay || delay >= iflp->max_delay))
  1663. {
  1664. vty_out (vty, "Average delay should be comprise between Min (%d) and Max (%d) delay%s",
  1665. iflp->min_delay, iflp->max_delay, VTY_NEWLINE);
  1666. return CMD_WARNING;
  1667. }
  1668. /* Update delay if value is not set or change */
  1669. if (IS_PARAM_UNSET(iflp, LP_DELAY)|| iflp->av_delay != delay)
  1670. {
  1671. iflp->av_delay = delay;
  1672. SET_PARAM(iflp, LP_DELAY);
  1673. update = 1;
  1674. }
  1675. /* Unset Min and Max delays if already set */
  1676. if (IS_PARAM_SET(iflp, LP_MM_DELAY))
  1677. {
  1678. iflp->min_delay = 0;
  1679. iflp->max_delay = 0;
  1680. UNSET_PARAM(iflp, LP_MM_DELAY);
  1681. update = 1;
  1682. }
  1683. break;
  1684. case 2:
  1685. vty_out (vty, "You should specify both Minimum and Maximum delay with Average delay%s",
  1686. VTY_NEWLINE);
  1687. return CMD_WARNING;
  1688. break;
  1689. case 3:
  1690. VTY_GET_ULONG("minimum delay", low, argv[1]);
  1691. VTY_GET_ULONG("maximum delay", high, argv[2]);
  1692. /* Check new delays value coherency */
  1693. if (delay <= low || delay >= high)
  1694. {
  1695. vty_out (vty, "Average delay should be comprise between Min (%d) and Max (%d) delay%s",
  1696. low, high, VTY_NEWLINE);
  1697. return CMD_WARNING;
  1698. }
  1699. /* Update Delays if needed */
  1700. if (IS_PARAM_UNSET(iflp, LP_DELAY)
  1701. || IS_PARAM_UNSET(iflp, LP_MM_DELAY)
  1702. || iflp->av_delay != delay
  1703. || iflp->min_delay != low
  1704. || iflp->max_delay != high)
  1705. {
  1706. iflp->av_delay = delay;
  1707. SET_PARAM(iflp, LP_DELAY);
  1708. iflp->min_delay = low;
  1709. iflp->max_delay = high;
  1710. SET_PARAM(iflp, LP_MM_DELAY);
  1711. update = 1;
  1712. }
  1713. break;
  1714. default:
  1715. return CMD_WARNING;
  1716. break;
  1717. }
  1718. /* force protocols to update LINK STATE due to parameters change */
  1719. if (update == 1 && if_is_operative (ifp))
  1720. zebra_interface_parameters_update (ifp);
  1721. return CMD_SUCCESS;
  1722. }
  1723. ALIAS (link_params_delay,
  1724. link_params_delay_mm_cmd,
  1725. "delay <0-16777215> min <0-16777215> max <0-16777215>",
  1726. "Unidirectional Average Link Delay (optionally Minimum and Maximum delays)\n"
  1727. "Average delay in micro-second as decimal (0...16777215)\n"
  1728. "Minimum delay\n"
  1729. "Minimum delay in micro-second as decimal (0...16777215)\n"
  1730. "Maximum delay\n"
  1731. "Maximum delay in micro-second as decimal (0...16777215)\n")
  1732. DEFUN (no_link_params_delay,
  1733. no_link_params_delay_cmd,
  1734. "no delay",
  1735. NO_STR
  1736. "Disable Unidirectional Average, Min & Max Link Delay on this interface\n")
  1737. {
  1738. struct interface *ifp = (struct interface *) vty->index;
  1739. struct if_link_params *iflp = if_link_params_get (ifp);
  1740. /* Unset Delays */
  1741. iflp->av_delay = 0;
  1742. UNSET_PARAM(iflp, LP_DELAY);
  1743. iflp->min_delay = 0;
  1744. iflp->max_delay = 0;
  1745. UNSET_PARAM(iflp, LP_MM_DELAY);
  1746. /* force protocols to update LINK STATE due to parameters change */
  1747. if (if_is_operative (ifp))
  1748. zebra_interface_parameters_update (ifp);
  1749. return CMD_SUCCESS;
  1750. }
  1751. DEFUN (link_params_delay_var,
  1752. link_params_delay_var_cmd,
  1753. "delay-variation <0-16777215>",
  1754. "Unidirectional Link Delay Variation\n"
  1755. "delay variation in micro-second as decimal (0...16777215)\n")
  1756. {
  1757. struct interface *ifp = (struct interface *) vty->index;
  1758. struct if_link_params *iflp = if_link_params_get (ifp);
  1759. u_int32_t value;
  1760. VTY_GET_ULONG("delay variation", value, argv[0]);
  1761. /* Update Delay Variation if needed */
  1762. link_param_cmd_set_uint32 (ifp, &iflp->delay_var, LP_DELAY_VAR, value);
  1763. return CMD_SUCCESS;
  1764. }
  1765. DEFUN (no_link_params_delay_var,
  1766. no_link_params_delay_var_cmd,
  1767. "no delay-variation",
  1768. NO_STR
  1769. "Disable Unidirectional Delay Variation on this interface\n")
  1770. {
  1771. struct interface *ifp = (struct interface *) vty->index;
  1772. /* Unset Delay Variation */
  1773. link_param_cmd_unset(ifp, LP_DELAY_VAR);
  1774. return CMD_SUCCESS;
  1775. }
  1776. DEFUN (link_params_pkt_loss,
  1777. link_params_pkt_loss_cmd,
  1778. "packet-loss PERCENTAGE",
  1779. "Unidirectional Link Packet Loss\n"
  1780. "percentage of total traffic by 0.000003% step and less than 50.331642%\n")
  1781. {
  1782. struct interface *ifp = (struct interface *) vty->index;
  1783. struct if_link_params *iflp = if_link_params_get (ifp);
  1784. float fval;
  1785. if (sscanf (argv[0], "%g", &fval) != 1)
  1786. {
  1787. vty_out (vty, "link_params_pkt_loss: fscanf: %s%s", safe_strerror (errno),
  1788. VTY_NEWLINE);
  1789. return CMD_WARNING;
  1790. }
  1791. if (fval > MAX_PKT_LOSS)
  1792. fval = MAX_PKT_LOSS;
  1793. /* Update Packet Loss if needed */
  1794. link_param_cmd_set_float (ifp, &iflp->pkt_loss, LP_PKT_LOSS, fval);
  1795. return CMD_SUCCESS;
  1796. }
  1797. DEFUN (no_link_params_pkt_loss,
  1798. no_link_params_pkt_loss_cmd,
  1799. "no packet-loss",
  1800. NO_STR
  1801. "Disable Unidirectional Link Packet Loss on this interface\n")
  1802. {
  1803. struct interface *ifp = (struct interface *) vty->index;
  1804. /* Unset Packet Loss */
  1805. link_param_cmd_unset(ifp, LP_PKT_LOSS);
  1806. return CMD_SUCCESS;
  1807. }
  1808. DEFUN (link_params_res_bw,
  1809. link_params_res_bw_cmd,
  1810. "res-bw BANDWIDTH",
  1811. "Unidirectional Residual Bandwidth\n"
  1812. "Bytes/second (IEEE floating point format)\n")
  1813. {
  1814. struct interface *ifp = (struct interface *) vty->index;
  1815. struct if_link_params *iflp = if_link_params_get (ifp);
  1816. float bw;
  1817. if (sscanf (argv[0], "%g", &bw) != 1)
  1818. {
  1819. vty_out (vty, "link_params_res_bw: fscanf: %s%s", safe_strerror (errno),
  1820. VTY_NEWLINE);
  1821. return CMD_WARNING;
  1822. }
  1823. /* Check that bandwidth is not greater than maximum bandwidth parameter */
  1824. if (bw > iflp->max_bw)
  1825. {
  1826. vty_out (vty,
  1827. "Residual Bandwidth could not be greater than Maximum Bandwidth (%g)%s",
  1828. iflp->max_bw, VTY_NEWLINE);
  1829. return CMD_WARNING;
  1830. }
  1831. /* Update Residual Bandwidth if needed */
  1832. link_param_cmd_set_float (ifp, &iflp->res_bw, LP_RES_BW, bw);
  1833. return CMD_SUCCESS;
  1834. }
  1835. DEFUN (no_link_params_res_bw,
  1836. no_link_params_res_bw_cmd,
  1837. "no res-bw",
  1838. NO_STR
  1839. "Disable Unidirectional Residual Bandwidth on this interface\n")
  1840. {
  1841. struct interface *ifp = (struct interface *) vty->index;
  1842. /* Unset Residual Bandwidth */
  1843. link_param_cmd_unset(ifp, LP_RES_BW);
  1844. return CMD_SUCCESS;
  1845. }
  1846. DEFUN (link_params_ava_bw,
  1847. link_params_ava_bw_cmd,
  1848. "ava-bw BANDWIDTH",
  1849. "Unidirectional Available Bandwidth\n"
  1850. "Bytes/second (IEEE floating point format)\n")
  1851. {
  1852. struct interface *ifp = (struct interface *) vty->index;
  1853. struct if_link_params *iflp = if_link_params_get (ifp);
  1854. float bw;
  1855. if (sscanf (argv[0], "%g", &bw) != 1)
  1856. {
  1857. vty_out (vty, "link_params_ava_bw: fscanf: %s%s", safe_strerror (errno),
  1858. VTY_NEWLINE);
  1859. return CMD_WARNING;
  1860. }
  1861. /* Check that bandwidth is not greater than maximum bandwidth parameter */
  1862. if (bw > iflp->max_bw)
  1863. {
  1864. vty_out (vty,
  1865. "Available Bandwidth could not be greater than Maximum Bandwidth (%g)%s",
  1866. iflp->max_bw, VTY_NEWLINE);
  1867. return CMD_WARNING;
  1868. }
  1869. /* Update Residual Bandwidth if needed */
  1870. link_param_cmd_set_float (ifp, &iflp->ava_bw, LP_AVA_BW, bw);
  1871. return CMD_SUCCESS;
  1872. }
  1873. DEFUN (no_link_params_ava_bw,
  1874. no_link_params_ava_bw_cmd,
  1875. "no ava-bw",
  1876. NO_STR
  1877. "Disable Unidirectional Available Bandwidth on this interface\n")
  1878. {
  1879. struct interface *ifp = (struct interface *) vty->index;
  1880. /* Unset Available Bandwidth */
  1881. link_param_cmd_unset(ifp, LP_AVA_BW);
  1882. return CMD_SUCCESS;
  1883. }
  1884. DEFUN (link_params_use_bw,
  1885. link_params_use_bw_cmd,
  1886. "use-bw BANDWIDTH",
  1887. "Unidirectional Utilised Bandwidth\n"
  1888. "Bytes/second (IEEE floating point format)\n")
  1889. {
  1890. struct interface *ifp = (struct interface *) vty->index;
  1891. struct if_link_params *iflp = if_link_params_get (ifp);
  1892. float bw;
  1893. if (sscanf (argv[0], "%g", &bw) != 1)
  1894. {
  1895. vty_out (vty, "link_params_use_bw: fscanf: %s%s", safe_strerror (errno),
  1896. VTY_NEWLINE);
  1897. return CMD_WARNING;
  1898. }
  1899. /* Check that bandwidth is not greater than maximum bandwidth parameter */
  1900. if (bw > iflp->max_bw)
  1901. {
  1902. vty_out (vty,
  1903. "Utilised Bandwidth could not be greater than Maximum Bandwidth (%g)%s",
  1904. iflp->max_bw, VTY_NEWLINE);
  1905. return CMD_WARNING;
  1906. }
  1907. /* Update Utilized Bandwidth if needed */
  1908. link_param_cmd_set_float (ifp, &iflp->use_bw, LP_USE_BW, bw);
  1909. return CMD_SUCCESS;
  1910. }
  1911. DEFUN (no_link_params_use_bw,
  1912. no_link_params_use_bw_cmd,
  1913. "no use-bw",
  1914. NO_STR
  1915. "Disable Unidirectional Utilised Bandwidth on this interface\n")
  1916. {
  1917. struct interface *ifp = (struct interface *) vty->index;
  1918. /* Unset Utilised Bandwidth */
  1919. link_param_cmd_unset(ifp, LP_USE_BW);
  1920. return CMD_SUCCESS;
  1921. }
  1922. static int
  1923. ip_address_install (struct vty *vty, struct interface *ifp,
  1924. const char *addr_str, const char *peer_str,
  1925. const char *label)
  1926. {
  1927. struct zebra_if *if_data;
  1928. struct prefix_ipv4 cp;
  1929. struct connected *ifc;
  1930. struct prefix_ipv4 *p;
  1931. int ret;
  1932. if_data = ifp->info;
  1933. ret = str2prefix_ipv4 (addr_str, &cp);
  1934. if (ret <= 0)
  1935. {
  1936. vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
  1937. return CMD_WARNING;
  1938. }
  1939. ifc = connected_check (ifp, (struct prefix *) &cp);
  1940. if (! ifc)
  1941. {
  1942. ifc = connected_new ();
  1943. ifc->ifp = ifp;
  1944. /* Address. */
  1945. p = prefix_ipv4_new ();
  1946. *p = cp;
  1947. ifc->address = (struct prefix *) p;
  1948. /* Broadcast. */
  1949. if (p->prefixlen <= IPV4_MAX_PREFIXLEN-2)
  1950. {
  1951. p = prefix_ipv4_new ();
  1952. *p = cp;
  1953. p->prefix.s_addr = ipv4_broadcast_addr(p->prefix.s_addr,p->prefixlen);
  1954. ifc->destination = (struct prefix *) p;
  1955. }
  1956. /* Label. */
  1957. if (label)
  1958. ifc->label = XSTRDUP (MTYPE_CONNECTED_LABEL, label);
  1959. /* Add to linked list. */
  1960. listnode_add (ifp->connected, ifc);
  1961. }
  1962. /* This address is configured from zebra. */
  1963. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  1964. SET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
  1965. /* In case of this route need to install kernel. */
  1966. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
  1967. && CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)
  1968. && !(if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON))
  1969. {
  1970. /* Some system need to up the interface to set IP address. */
  1971. if (! if_is_up (ifp))
  1972. {
  1973. if_set_flags (ifp, IFF_UP | IFF_RUNNING);
  1974. if_refresh (ifp);
  1975. }
  1976. ret = if_set_prefix (ifp, ifc);
  1977. if (ret < 0)
  1978. {
  1979. vty_out (vty, "%% Can't set interface IP address: %s.%s",
  1980. safe_strerror(errno), VTY_NEWLINE);
  1981. return CMD_WARNING;
  1982. }
  1983. SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
  1984. /* The address will be advertised to zebra clients when the notification
  1985. * from the kernel has been received.
  1986. * It will also be added to the subnet chain list, then. */
  1987. }
  1988. return CMD_SUCCESS;
  1989. }
  1990. static int
  1991. ip_address_uninstall (struct vty *vty, struct interface *ifp,
  1992. const char *addr_str, const char *peer_str,
  1993. const char *label)
  1994. {
  1995. struct prefix_ipv4 cp;
  1996. struct connected *ifc;
  1997. int ret;
  1998. /* Convert to prefix structure. */
  1999. ret = str2prefix_ipv4 (addr_str, &cp);
  2000. if (ret <= 0)
  2001. {
  2002. vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
  2003. return CMD_WARNING;
  2004. }
  2005. /* Check current interface address. */
  2006. ifc = connected_check (ifp, (struct prefix *) &cp);
  2007. if (! ifc)
  2008. {
  2009. vty_out (vty, "%% Can't find address%s", VTY_NEWLINE);
  2010. return CMD_WARNING;
  2011. }
  2012. /* This is not configured address. */
  2013. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  2014. return CMD_WARNING;
  2015. UNSET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
  2016. /* This is not real address or interface is not active. */
  2017. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
  2018. || ! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  2019. {
  2020. listnode_delete (ifp->connected, ifc);
  2021. connected_free (ifc);
  2022. return CMD_WARNING;
  2023. }
  2024. /* This is real route. */
  2025. ret = if_unset_prefix (ifp, ifc);
  2026. if (ret < 0)
  2027. {
  2028. vty_out (vty, "%% Can't unset interface IP address: %s.%s",
  2029. safe_strerror(errno), VTY_NEWLINE);
  2030. return CMD_WARNING;
  2031. }
  2032. UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
  2033. /* we will receive a kernel notification about this route being removed.
  2034. * this will trigger its removal from the connected list. */
  2035. return CMD_SUCCESS;
  2036. }
  2037. DEFUN (ip_address,
  2038. ip_address_cmd,
  2039. "ip address A.B.C.D/M",
  2040. "Interface Internet Protocol config commands\n"
  2041. "Set the IP address of an interface\n"
  2042. "IP address (e.g. 10.0.0.1/8)\n")
  2043. {
  2044. return ip_address_install (vty, vty->index, argv[0], NULL, NULL);
  2045. }
  2046. DEFUN (no_ip_address,
  2047. no_ip_address_cmd,
  2048. "no ip address A.B.C.D/M",
  2049. NO_STR
  2050. "Interface Internet Protocol config commands\n"
  2051. "Set the IP address of an interface\n"
  2052. "IP Address (e.g. 10.0.0.1/8)")
  2053. {
  2054. return ip_address_uninstall (vty, vty->index, argv[0], NULL, NULL);
  2055. }
  2056. #ifdef HAVE_NETLINK
  2057. DEFUN (ip_address_label,
  2058. ip_address_label_cmd,
  2059. "ip address A.B.C.D/M label LINE",
  2060. "Interface Internet Protocol config commands\n"
  2061. "Set the IP address of an interface\n"
  2062. "IP address (e.g. 10.0.0.1/8)\n"
  2063. "Label of this address\n"
  2064. "Label\n")
  2065. {
  2066. return ip_address_install (vty, vty->index, argv[0], NULL, argv[1]);
  2067. }
  2068. DEFUN (no_ip_address_label,
  2069. no_ip_address_label_cmd,
  2070. "no ip address A.B.C.D/M label LINE",
  2071. NO_STR
  2072. "Interface Internet Protocol config commands\n"
  2073. "Set the IP address of an interface\n"
  2074. "IP address (e.g. 10.0.0.1/8)\n"
  2075. "Label of this address\n"
  2076. "Label\n")
  2077. {
  2078. return ip_address_uninstall (vty, vty->index, argv[0], NULL, argv[1]);
  2079. }
  2080. #endif /* HAVE_NETLINK */
  2081. #ifdef HAVE_IPV6
  2082. static int
  2083. ipv6_address_install (struct vty *vty, struct interface *ifp,
  2084. const char *addr_str, const char *peer_str,
  2085. const char *label, int secondary)
  2086. {
  2087. struct zebra_if *if_data;
  2088. struct prefix_ipv6 cp;
  2089. struct connected *ifc;
  2090. struct prefix_ipv6 *p;
  2091. int ret;
  2092. if_data = ifp->info;
  2093. ret = str2prefix_ipv6 (addr_str, &cp);
  2094. if (ret <= 0)
  2095. {
  2096. vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
  2097. return CMD_WARNING;
  2098. }
  2099. ifc = connected_check (ifp, (struct prefix *) &cp);
  2100. if (! ifc)
  2101. {
  2102. ifc = connected_new ();
  2103. ifc->ifp = ifp;
  2104. /* Address. */
  2105. p = prefix_ipv6_new ();
  2106. *p = cp;
  2107. ifc->address = (struct prefix *) p;
  2108. /* Secondary. */
  2109. if (secondary)
  2110. SET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);
  2111. /* Label. */
  2112. if (label)
  2113. ifc->label = XSTRDUP (MTYPE_CONNECTED_LABEL, label);
  2114. /* Add to linked list. */
  2115. listnode_add (ifp->connected, ifc);
  2116. }
  2117. /* This address is configured from zebra. */
  2118. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  2119. SET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
  2120. /* In case of this route need to install kernel. */
  2121. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
  2122. && CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)
  2123. && !(if_data && if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON))
  2124. {
  2125. /* Some system need to up the interface to set IP address. */
  2126. if (! if_is_up (ifp))
  2127. {
  2128. if_set_flags (ifp, IFF_UP | IFF_RUNNING);
  2129. if_refresh (ifp);
  2130. }
  2131. ret = if_prefix_add_ipv6 (ifp, ifc);
  2132. if (ret < 0)
  2133. {
  2134. vty_out (vty, "%% Can't set interface IP address: %s.%s",
  2135. safe_strerror(errno), VTY_NEWLINE);
  2136. return CMD_WARNING;
  2137. }
  2138. SET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
  2139. /* The address will be advertised to zebra clients when the notification
  2140. * from the kernel has been received. */
  2141. }
  2142. return CMD_SUCCESS;
  2143. }
  2144. static int
  2145. ipv6_address_uninstall (struct vty *vty, struct interface *ifp,
  2146. const char *addr_str, const char *peer_str,
  2147. const char *label, int secondry)
  2148. {
  2149. struct prefix_ipv6 cp;
  2150. struct connected *ifc;
  2151. int ret;
  2152. /* Convert to prefix structure. */
  2153. ret = str2prefix_ipv6 (addr_str, &cp);
  2154. if (ret <= 0)
  2155. {
  2156. vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);
  2157. return CMD_WARNING;
  2158. }
  2159. /* Check current interface address. */
  2160. ifc = connected_check (ifp, (struct prefix *) &cp);
  2161. if (! ifc)
  2162. {
  2163. vty_out (vty, "%% Can't find address%s", VTY_NEWLINE);
  2164. return CMD_WARNING;
  2165. }
  2166. /* This is not configured address. */
  2167. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  2168. return CMD_WARNING;
  2169. UNSET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);
  2170. /* This is not real address or interface is not active. */
  2171. if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_QUEUED)
  2172. || ! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  2173. {
  2174. listnode_delete (ifp->connected, ifc);
  2175. connected_free (ifc);
  2176. return CMD_WARNING;
  2177. }
  2178. /* This is real route. */
  2179. ret = if_prefix_delete_ipv6 (ifp, ifc);
  2180. if (ret < 0)
  2181. {
  2182. vty_out (vty, "%% Can't unset interface IP address: %s.%s",
  2183. safe_strerror(errno), VTY_NEWLINE);
  2184. return CMD_WARNING;
  2185. }
  2186. UNSET_FLAG (ifc->conf, ZEBRA_IFC_QUEUED);
  2187. /* This information will be propagated to the zclients when the
  2188. * kernel notification is received. */
  2189. return CMD_SUCCESS;
  2190. }
  2191. DEFUN (ipv6_address,
  2192. ipv6_address_cmd,
  2193. "ipv6 address X:X::X:X/M",
  2194. "Interface IPv6 config commands\n"
  2195. "Set the IP address of an interface\n"
  2196. "IPv6 address (e.g. 3ffe:506::1/48)\n")
  2197. {
  2198. return ipv6_address_install (vty, vty->index, argv[0], NULL, NULL, 0);
  2199. }
  2200. DEFUN (no_ipv6_address,
  2201. no_ipv6_address_cmd,
  2202. "no ipv6 address X:X::X:X/M",
  2203. NO_STR
  2204. "Interface IPv6 config commands\n"
  2205. "Set the IP address of an interface\n"
  2206. "IPv6 address (e.g. 3ffe:506::1/48)\n")
  2207. {
  2208. return ipv6_address_uninstall (vty, vty->index, argv[0], NULL, NULL, 0);
  2209. }
  2210. #endif /* HAVE_IPV6 */
  2211. static int
  2212. link_params_config_write (struct vty *vty, struct interface *ifp)
  2213. {
  2214. int i;
  2215. if ((ifp == NULL) || !HAS_LINK_PARAMS(ifp))
  2216. return -1;
  2217. struct if_link_params *iflp = ifp->link_params;
  2218. vty_out (vty, " link-params%s", VTY_NEWLINE);
  2219. vty_out(vty, " enable%s", VTY_NEWLINE);
  2220. if (IS_PARAM_SET(iflp, LP_TE))
  2221. vty_out(vty, " metric %u%s",iflp->te_metric, VTY_NEWLINE);
  2222. if (IS_PARAM_SET(iflp, LP_MAX_BW))
  2223. vty_out(vty, " max-bw %g%s", iflp->max_bw, VTY_NEWLINE);
  2224. if (IS_PARAM_SET(iflp, LP_MAX_RSV_BW))
  2225. vty_out(vty, " max-rsv-bw %g%s", iflp->max_rsv_bw, VTY_NEWLINE);
  2226. if (IS_PARAM_SET(iflp, LP_UNRSV_BW))
  2227. {
  2228. for (i = 0; i < 8; i++)
  2229. vty_out(vty, " unrsv-bw %d %g%s",
  2230. i, iflp->unrsv_bw[i], VTY_NEWLINE);
  2231. }
  2232. if (IS_PARAM_SET(iflp, LP_ADM_GRP))
  2233. vty_out(vty, " admin-grp %u%s", iflp->admin_grp, VTY_NEWLINE);
  2234. if (IS_PARAM_SET(iflp, LP_DELAY))
  2235. {
  2236. vty_out(vty, " delay %u", iflp->av_delay);
  2237. if (IS_PARAM_SET(iflp, LP_MM_DELAY))
  2238. {
  2239. vty_out(vty, " min %u", iflp->min_delay);
  2240. vty_out(vty, " max %u", iflp->max_delay);
  2241. }
  2242. vty_out(vty, "%s", VTY_NEWLINE);
  2243. }
  2244. if (IS_PARAM_SET(iflp, LP_DELAY_VAR))
  2245. vty_out(vty, " delay-variation %u%s", iflp->delay_var, VTY_NEWLINE);
  2246. if (IS_PARAM_SET(iflp, LP_PKT_LOSS))
  2247. vty_out(vty, " packet-loss %g%s", iflp->pkt_loss, VTY_NEWLINE);
  2248. if (IS_PARAM_SET(iflp, LP_AVA_BW))
  2249. vty_out(vty, " ava-bw %g%s", iflp->ava_bw, VTY_NEWLINE);
  2250. if (IS_PARAM_SET(iflp, LP_RES_BW))
  2251. vty_out(vty, " res-bw %g%s", iflp->res_bw, VTY_NEWLINE);
  2252. if (IS_PARAM_SET(iflp, LP_USE_BW))
  2253. vty_out(vty, " use-bw %g%s", iflp->use_bw, VTY_NEWLINE);
  2254. if (IS_PARAM_SET(iflp, LP_RMT_AS))
  2255. vty_out(vty, " neighbor %s as %u%s", inet_ntoa(iflp->rmt_ip),
  2256. iflp->rmt_as, VTY_NEWLINE);
  2257. vty_out(vty, " exit-link-params%s", VTY_NEWLINE);
  2258. return 0;
  2259. }
  2260. static int
  2261. if_config_write (struct vty *vty)
  2262. {
  2263. struct listnode *node;
  2264. struct interface *ifp;
  2265. vrf_iter_t iter;
  2266. for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
  2267. for (ALL_LIST_ELEMENTS_RO (vrf_iter2iflist (iter), node, ifp))
  2268. {
  2269. struct zebra_if *if_data;
  2270. struct listnode *addrnode;
  2271. struct connected *ifc;
  2272. struct prefix *p;
  2273. if_data = ifp->info;
  2274. if (ifp->vrf_id == VRF_DEFAULT)
  2275. vty_out (vty, "interface %s%s", ifp->name, VTY_NEWLINE);
  2276. else
  2277. vty_out (vty, "interface %s vrf %u%s", ifp->name, ifp->vrf_id,
  2278. VTY_NEWLINE);
  2279. if (if_data)
  2280. {
  2281. if (if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)
  2282. vty_out (vty, " shutdown%s", VTY_NEWLINE);
  2283. }
  2284. if (ifp->desc)
  2285. vty_out (vty, " description %s%s", ifp->desc,
  2286. VTY_NEWLINE);
  2287. /* Assign bandwidth here to avoid unnecessary interface flap
  2288. while processing config script */
  2289. if (ifp->bandwidth != 0)
  2290. vty_out(vty, " bandwidth %u%s", ifp->bandwidth, VTY_NEWLINE);
  2291. switch (if_data->linkdetect)
  2292. {
  2293. case IF_LINKDETECT_ON:
  2294. vty_out(vty, " link-detect%s", VTY_NEWLINE);
  2295. break;
  2296. case IF_LINKDETECT_OFF:
  2297. vty_out(vty, " no link-detect%s", VTY_NEWLINE);
  2298. break;
  2299. default: break;
  2300. }
  2301. for (ALL_LIST_ELEMENTS_RO (ifp->connected, addrnode, ifc))
  2302. {
  2303. if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
  2304. {
  2305. char buf[INET6_ADDRSTRLEN];
  2306. p = ifc->address;
  2307. vty_out (vty, " ip%s address %s",
  2308. p->family == AF_INET ? "" : "v6",
  2309. prefix2str (p, buf, sizeof(buf)));
  2310. if (ifc->label)
  2311. vty_out (vty, " label %s", ifc->label);
  2312. vty_out (vty, "%s", VTY_NEWLINE);
  2313. }
  2314. }
  2315. if (if_data)
  2316. {
  2317. if (if_data->multicast != IF_ZEBRA_MULTICAST_UNSPEC)
  2318. vty_out (vty, " %smulticast%s",
  2319. if_data->multicast == IF_ZEBRA_MULTICAST_ON ? "" : "no ",
  2320. VTY_NEWLINE);
  2321. }
  2322. #if defined (HAVE_RTADV)
  2323. rtadv_config_write (vty, ifp);
  2324. #endif /* HAVE_RTADV */
  2325. #ifdef HAVE_IRDP
  2326. irdp_config_write (vty, ifp);
  2327. #endif /* IRDP */
  2328. link_params_config_write (vty, ifp);
  2329. vty_out (vty, "!%s", VTY_NEWLINE);
  2330. }
  2331. return 0;
  2332. }
  2333. /* Allocate and initialize interface vector. */
  2334. void
  2335. zebra_if_init (void)
  2336. {
  2337. /* Initialize interface and new hook. */
  2338. if_add_hook (IF_NEW_HOOK, if_zebra_new_hook);
  2339. if_add_hook (IF_DELETE_HOOK, if_zebra_delete_hook);
  2340. /* Install configuration write function. */
  2341. install_node (&interface_node, if_config_write);
  2342. install_node (&zebra_if_defaults_node, config_write_zebra_if_defaults);
  2343. install_node (&link_params_node, NULL);
  2344. install_element (VIEW_NODE, &show_interface_cmd);
  2345. install_element (VIEW_NODE, &show_interface_vrf_cmd);
  2346. install_element (VIEW_NODE, &show_interface_vrf_all_cmd);
  2347. install_element (VIEW_NODE, &show_interface_name_cmd);
  2348. install_element (VIEW_NODE, &show_interface_name_vrf_cmd);
  2349. install_element (VIEW_NODE, &show_interface_name_vrf_all_cmd);
  2350. install_element (CONFIG_NODE, &zebra_interface_cmd);
  2351. install_element (CONFIG_NODE, &zebra_interface_vrf_cmd);
  2352. install_element (CONFIG_NODE, &no_interface_cmd);
  2353. install_element (CONFIG_NODE, &no_interface_vrf_cmd);
  2354. install_element (CONFIG_NODE, &default_linkdetect_cmd);
  2355. install_default (INTERFACE_NODE);
  2356. install_element (INTERFACE_NODE, &interface_desc_cmd);
  2357. install_element (INTERFACE_NODE, &no_interface_desc_cmd);
  2358. install_element (INTERFACE_NODE, &multicast_cmd);
  2359. install_element (INTERFACE_NODE, &no_multicast_cmd);
  2360. install_element (INTERFACE_NODE, &linkdetect_cmd);
  2361. install_element (INTERFACE_NODE, &no_linkdetect_cmd);
  2362. install_element (INTERFACE_NODE, &shutdown_if_cmd);
  2363. install_element (INTERFACE_NODE, &no_shutdown_if_cmd);
  2364. install_element (INTERFACE_NODE, &bandwidth_if_cmd);
  2365. install_element (INTERFACE_NODE, &no_bandwidth_if_cmd);
  2366. install_element (INTERFACE_NODE, &no_bandwidth_if_val_cmd);
  2367. install_element (INTERFACE_NODE, &ip_address_cmd);
  2368. install_element (INTERFACE_NODE, &no_ip_address_cmd);
  2369. #ifdef HAVE_IPV6
  2370. install_element (INTERFACE_NODE, &ipv6_address_cmd);
  2371. install_element (INTERFACE_NODE, &no_ipv6_address_cmd);
  2372. #endif /* HAVE_IPV6 */
  2373. #ifdef HAVE_NETLINK
  2374. install_element (INTERFACE_NODE, &ip_address_label_cmd);
  2375. install_element (INTERFACE_NODE, &no_ip_address_label_cmd);
  2376. #endif /* HAVE_NETLINK */
  2377. install_element(INTERFACE_NODE, &link_params_cmd);
  2378. install_default(LINK_PARAMS_NODE);
  2379. install_element(LINK_PARAMS_NODE, &link_params_enable_cmd);
  2380. install_element(LINK_PARAMS_NODE, &no_link_params_enable_cmd);
  2381. install_element(LINK_PARAMS_NODE, &link_params_metric_cmd);
  2382. install_element(LINK_PARAMS_NODE, &link_params_maxbw_cmd);
  2383. install_element(LINK_PARAMS_NODE, &link_params_max_rsv_bw_cmd);
  2384. install_element(LINK_PARAMS_NODE, &link_params_unrsv_bw_cmd);
  2385. install_element(LINK_PARAMS_NODE, &link_params_admin_grp_cmd);
  2386. install_element(LINK_PARAMS_NODE, &no_link_params_admin_grp_cmd);
  2387. install_element(LINK_PARAMS_NODE, &link_params_inter_as_cmd);
  2388. install_element(LINK_PARAMS_NODE, &no_link_params_inter_as_cmd);
  2389. install_element(LINK_PARAMS_NODE, &link_params_delay_cmd);
  2390. install_element(LINK_PARAMS_NODE, &no_link_params_delay_cmd);
  2391. install_element(LINK_PARAMS_NODE, &link_params_delay_mm_cmd);
  2392. install_element(LINK_PARAMS_NODE, &link_params_delay_var_cmd);
  2393. install_element(LINK_PARAMS_NODE, &no_link_params_delay_var_cmd);
  2394. install_element(LINK_PARAMS_NODE, &link_params_pkt_loss_cmd);
  2395. install_element(LINK_PARAMS_NODE, &no_link_params_pkt_loss_cmd);
  2396. install_element(LINK_PARAMS_NODE, &link_params_ava_bw_cmd);
  2397. install_element(LINK_PARAMS_NODE, &no_link_params_ava_bw_cmd);
  2398. install_element(LINK_PARAMS_NODE, &link_params_res_bw_cmd);
  2399. install_element(LINK_PARAMS_NODE, &no_link_params_res_bw_cmd);
  2400. install_element(LINK_PARAMS_NODE, &link_params_use_bw_cmd);
  2401. install_element(LINK_PARAMS_NODE, &no_link_params_use_bw_cmd);
  2402. install_element(LINK_PARAMS_NODE, &exit_link_params_cmd);
  2403. }