interface.c 42 KB

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