interface.c 41 KB

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