interface.c 44 KB

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