interface.c 37 KB

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