if.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. /*
  2. * Interface functions.
  3. * Copyright (C) 1997, 98 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
  8. * it under the terms of the GNU General Public License as published
  9. * by the Free Software Foundation; either version 2, or (at your
  10. * option) any 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
  19. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. * Boston, MA 02111-1307, USA.
  21. */
  22. #include <zebra.h>
  23. #include "linklist.h"
  24. #include "vector.h"
  25. #include "vty.h"
  26. #include "command.h"
  27. #include "vrf.h"
  28. #include "if.h"
  29. #include "sockunion.h"
  30. #include "prefix.h"
  31. #include "memory.h"
  32. #include "table.h"
  33. #include "buffer.h"
  34. #include "str.h"
  35. #include "log.h"
  36. /* List of interfaces in only the default VRF */
  37. struct list *iflist;
  38. /* One for each program. This structure is needed to store hooks. */
  39. struct if_master
  40. {
  41. int (*if_new_hook) (struct interface *);
  42. int (*if_delete_hook) (struct interface *);
  43. } if_master = {0,};
  44. /* Compare interface names, returning an integer greater than, equal to, or
  45. * less than 0, (following the strcmp convention), according to the
  46. * relationship between ifp1 and ifp2. Interface names consist of an
  47. * alphabetic prefix and a numeric suffix. The primary sort key is
  48. * lexicographic by name, and then numeric by number. No number sorts
  49. * before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
  50. * devpty0, de0 < del0
  51. */
  52. int
  53. if_cmp_func (struct interface *ifp1, struct interface *ifp2)
  54. {
  55. unsigned int l1, l2;
  56. long int x1, x2;
  57. char *p1, *p2;
  58. int res;
  59. p1 = ifp1->name;
  60. p2 = ifp2->name;
  61. while (*p1 && *p2) {
  62. /* look up to any number */
  63. l1 = strcspn(p1, "0123456789");
  64. l2 = strcspn(p2, "0123456789");
  65. /* name lengths are different -> compare names */
  66. if (l1 != l2)
  67. return (strcmp(p1, p2));
  68. /* Note that this relies on all numbers being less than all letters, so
  69. * that de0 < del0.
  70. */
  71. res = strncmp(p1, p2, l1);
  72. /* names are different -> compare them */
  73. if (res)
  74. return res;
  75. /* with identical name part, go to numeric part */
  76. p1 += l1;
  77. p2 += l1;
  78. if (!*p1)
  79. return -1;
  80. if (!*p2)
  81. return 1;
  82. x1 = strtol(p1, &p1, 10);
  83. x2 = strtol(p2, &p2, 10);
  84. /* let's compare numbers now */
  85. if (x1 < x2)
  86. return -1;
  87. if (x1 > x2)
  88. return 1;
  89. /* numbers were equal, lets do it again..
  90. (it happens with name like "eth123.456:789") */
  91. }
  92. if (*p1)
  93. return 1;
  94. if (*p2)
  95. return -1;
  96. return 0;
  97. }
  98. /* Create new interface structure. */
  99. struct interface *
  100. if_create_vrf (const char *name, int namelen, vrf_id_t vrf_id)
  101. {
  102. struct interface *ifp;
  103. struct list *intf_list = vrf_iflist_get (vrf_id);
  104. ifp = XCALLOC (MTYPE_IF, sizeof (struct interface));
  105. ifp->ifindex = IFINDEX_INTERNAL;
  106. assert (name);
  107. assert (namelen <= INTERFACE_NAMSIZ); /* Need space for '\0' at end. */
  108. strncpy (ifp->name, name, namelen);
  109. ifp->name[namelen] = '\0';
  110. ifp->vrf_id = vrf_id;
  111. if (if_lookup_by_name_vrf (ifp->name, vrf_id) == NULL)
  112. listnode_add_sort (intf_list, ifp);
  113. else
  114. zlog_err("if_create(%s): corruption detected -- interface with this "
  115. "name exists already in VRF %u!", ifp->name, vrf_id);
  116. ifp->connected = list_new ();
  117. ifp->connected->del = (void (*) (void *)) connected_free;
  118. if (if_master.if_new_hook)
  119. (*if_master.if_new_hook) (ifp);
  120. return ifp;
  121. }
  122. struct interface *
  123. if_create (const char *name, int namelen)
  124. {
  125. return if_create_vrf (name, namelen, VRF_DEFAULT);
  126. }
  127. /* Delete interface structure. */
  128. void
  129. if_delete_retain (struct interface *ifp)
  130. {
  131. if (if_master.if_delete_hook)
  132. (*if_master.if_delete_hook) (ifp);
  133. /* Free connected address list */
  134. list_delete_all_node (ifp->connected);
  135. }
  136. /* Delete and free interface structure. */
  137. void
  138. if_delete (struct interface *ifp)
  139. {
  140. listnode_delete (vrf_iflist (ifp->vrf_id), ifp);
  141. if_delete_retain(ifp);
  142. list_free (ifp->connected);
  143. XFREE (MTYPE_IF, ifp);
  144. }
  145. /* Add hook to interface master. */
  146. void
  147. if_add_hook (int type, int (*func)(struct interface *ifp))
  148. {
  149. switch (type) {
  150. case IF_NEW_HOOK:
  151. if_master.if_new_hook = func;
  152. break;
  153. case IF_DELETE_HOOK:
  154. if_master.if_delete_hook = func;
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. /* Interface existance check by index. */
  161. struct interface *
  162. if_lookup_by_index_vrf (ifindex_t ifindex, vrf_id_t vrf_id)
  163. {
  164. struct listnode *node;
  165. struct interface *ifp;
  166. for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
  167. {
  168. if (ifp->ifindex == ifindex)
  169. return ifp;
  170. }
  171. return NULL;
  172. }
  173. struct interface *
  174. if_lookup_by_index (ifindex_t ifindex)
  175. {
  176. return if_lookup_by_index_vrf (ifindex, VRF_DEFAULT);
  177. }
  178. const char *
  179. ifindex2ifname_vrf (ifindex_t ifindex, vrf_id_t vrf_id)
  180. {
  181. struct interface *ifp;
  182. return ((ifp = if_lookup_by_index_vrf (ifindex, vrf_id)) != NULL) ?
  183. ifp->name : "unknown";
  184. }
  185. const char *
  186. ifindex2ifname (ifindex_t ifindex)
  187. {
  188. return ifindex2ifname_vrf (ifindex, VRF_DEFAULT);
  189. }
  190. ifindex_t
  191. ifname2ifindex_vrf (const char *name, vrf_id_t vrf_id)
  192. {
  193. struct interface *ifp;
  194. return ((ifp = if_lookup_by_name_vrf (name, vrf_id)) != NULL) ? ifp->ifindex
  195. : IFINDEX_INTERNAL;
  196. }
  197. ifindex_t
  198. ifname2ifindex (const char *name)
  199. {
  200. return ifname2ifindex_vrf (name, VRF_DEFAULT);
  201. }
  202. /* Interface existance check by interface name. */
  203. struct interface *
  204. if_lookup_by_name_vrf (const char *name, vrf_id_t vrf_id)
  205. {
  206. struct listnode *node;
  207. struct interface *ifp;
  208. if (name)
  209. for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
  210. {
  211. if (strcmp(name, ifp->name) == 0)
  212. return ifp;
  213. }
  214. return NULL;
  215. }
  216. struct interface *
  217. if_lookup_by_name (const char *name)
  218. {
  219. return if_lookup_by_name_vrf (name, VRF_DEFAULT);
  220. }
  221. struct interface *
  222. if_lookup_by_name_len_vrf (const char *name, size_t namelen, vrf_id_t vrf_id)
  223. {
  224. struct listnode *node;
  225. struct interface *ifp;
  226. if (namelen > INTERFACE_NAMSIZ)
  227. return NULL;
  228. for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
  229. {
  230. if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0'))
  231. return ifp;
  232. }
  233. return NULL;
  234. }
  235. struct interface *
  236. if_lookup_by_name_len(const char *name, size_t namelen)
  237. {
  238. return if_lookup_by_name_len_vrf (name, namelen, VRF_DEFAULT);
  239. }
  240. /* Lookup interface by IPv4 address. */
  241. struct interface *
  242. if_lookup_exact_address_vrf (struct in_addr src, vrf_id_t vrf_id)
  243. {
  244. struct listnode *node;
  245. struct listnode *cnode;
  246. struct interface *ifp;
  247. struct prefix *p;
  248. struct connected *c;
  249. for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
  250. {
  251. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
  252. {
  253. p = c->address;
  254. if (p && p->family == AF_INET)
  255. {
  256. if (IPV4_ADDR_SAME (&p->u.prefix4, &src))
  257. return ifp;
  258. }
  259. }
  260. }
  261. return NULL;
  262. }
  263. struct interface *
  264. if_lookup_exact_address (struct in_addr src)
  265. {
  266. return if_lookup_exact_address_vrf (src, VRF_DEFAULT);
  267. }
  268. /* Lookup interface by IPv4 address. */
  269. struct interface *
  270. if_lookup_address_vrf (struct in_addr src, vrf_id_t vrf_id)
  271. {
  272. struct listnode *node;
  273. struct prefix addr;
  274. int bestlen = 0;
  275. struct listnode *cnode;
  276. struct interface *ifp;
  277. struct connected *c;
  278. struct interface *match;
  279. addr.family = AF_INET;
  280. addr.u.prefix4 = src;
  281. addr.prefixlen = IPV4_MAX_BITLEN;
  282. match = NULL;
  283. for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
  284. {
  285. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
  286. {
  287. if (c->address && (c->address->family == AF_INET) &&
  288. prefix_match(CONNECTED_PREFIX(c), &addr) &&
  289. (c->address->prefixlen > bestlen))
  290. {
  291. bestlen = c->address->prefixlen;
  292. match = ifp;
  293. }
  294. }
  295. }
  296. return match;
  297. }
  298. struct interface *
  299. if_lookup_address (struct in_addr src)
  300. {
  301. return if_lookup_address_vrf (src, VRF_DEFAULT);
  302. }
  303. /* Lookup interface by prefix */
  304. struct interface *
  305. if_lookup_prefix_vrf (struct prefix *prefix, vrf_id_t vrf_id)
  306. {
  307. struct listnode *node;
  308. struct listnode *cnode;
  309. struct interface *ifp;
  310. struct connected *c;
  311. for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
  312. {
  313. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
  314. {
  315. if (prefix_cmp(c->address, prefix) == 0)
  316. {
  317. return ifp;
  318. }
  319. }
  320. }
  321. return NULL;
  322. }
  323. struct interface *
  324. if_lookup_prefix (struct prefix *prefix)
  325. {
  326. return if_lookup_prefix_vrf (prefix, VRF_DEFAULT);
  327. }
  328. /* Get interface by name if given name interface doesn't exist create
  329. one. */
  330. struct interface *
  331. if_get_by_name_vrf (const char *name, vrf_id_t vrf_id)
  332. {
  333. struct interface *ifp;
  334. return ((ifp = if_lookup_by_name_vrf (name, vrf_id)) != NULL) ? ifp :
  335. if_create_vrf (name, strlen(name), vrf_id);
  336. }
  337. struct interface *
  338. if_get_by_name (const char *name)
  339. {
  340. return if_get_by_name_vrf (name, VRF_DEFAULT);
  341. }
  342. struct interface *
  343. if_get_by_name_len_vrf (const char *name, size_t namelen, vrf_id_t vrf_id)
  344. {
  345. struct interface *ifp;
  346. return ((ifp = if_lookup_by_name_len_vrf (name, namelen, vrf_id)) != NULL) ? \
  347. ifp : if_create_vrf (name, namelen, vrf_id);
  348. }
  349. struct interface *
  350. if_get_by_name_len (const char *name, size_t namelen)
  351. {
  352. return if_get_by_name_len_vrf (name, namelen, VRF_DEFAULT);
  353. }
  354. /* Does interface up ? */
  355. int
  356. if_is_up (struct interface *ifp)
  357. {
  358. return ifp->flags & IFF_UP;
  359. }
  360. /* Is interface running? */
  361. int
  362. if_is_running (struct interface *ifp)
  363. {
  364. return ifp->flags & IFF_RUNNING;
  365. }
  366. /* Is the interface operative, eg. either UP & RUNNING
  367. or UP & !ZEBRA_INTERFACE_LINK_DETECTION */
  368. int
  369. if_is_operative (struct interface *ifp)
  370. {
  371. return ((ifp->flags & IFF_UP) &&
  372. (ifp->flags & IFF_RUNNING || !CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
  373. }
  374. /* Is this loopback interface ? */
  375. int
  376. if_is_loopback (struct interface *ifp)
  377. {
  378. /* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
  379. * but Y on platform N?
  380. */
  381. return (ifp->flags & (IFF_LOOPBACK|IFF_NOXMIT|IFF_VIRTUAL));
  382. }
  383. /* Does this interface support broadcast ? */
  384. int
  385. if_is_broadcast (struct interface *ifp)
  386. {
  387. return ifp->flags & IFF_BROADCAST;
  388. }
  389. /* Does this interface support broadcast ? */
  390. int
  391. if_is_pointopoint (struct interface *ifp)
  392. {
  393. return ifp->flags & IFF_POINTOPOINT;
  394. }
  395. /* Does this interface support multicast ? */
  396. int
  397. if_is_multicast (struct interface *ifp)
  398. {
  399. return ifp->flags & IFF_MULTICAST;
  400. }
  401. /* Printout flag information into log */
  402. const char *
  403. if_flag_dump (unsigned long flag)
  404. {
  405. int separator = 0;
  406. static char logbuf[BUFSIZ];
  407. #define IFF_OUT_LOG(X,STR) \
  408. if (flag & (X)) \
  409. { \
  410. if (separator) \
  411. strlcat (logbuf, ",", BUFSIZ); \
  412. else \
  413. separator = 1; \
  414. strlcat (logbuf, STR, BUFSIZ); \
  415. }
  416. strlcpy (logbuf, "<", BUFSIZ);
  417. IFF_OUT_LOG (IFF_UP, "UP");
  418. IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST");
  419. IFF_OUT_LOG (IFF_DEBUG, "DEBUG");
  420. IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK");
  421. IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT");
  422. IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS");
  423. IFF_OUT_LOG (IFF_RUNNING, "RUNNING");
  424. IFF_OUT_LOG (IFF_NOARP, "NOARP");
  425. IFF_OUT_LOG (IFF_PROMISC, "PROMISC");
  426. IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI");
  427. IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE");
  428. IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX");
  429. IFF_OUT_LOG (IFF_LINK0, "LINK0");
  430. IFF_OUT_LOG (IFF_LINK1, "LINK1");
  431. IFF_OUT_LOG (IFF_LINK2, "LINK2");
  432. IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST");
  433. IFF_OUT_LOG (IFF_NOXMIT, "NOXMIT");
  434. IFF_OUT_LOG (IFF_NORTEXCH, "NORTEXCH");
  435. IFF_OUT_LOG (IFF_VIRTUAL, "VIRTUAL");
  436. IFF_OUT_LOG (IFF_IPV4, "IPv4");
  437. IFF_OUT_LOG (IFF_IPV6, "IPv6");
  438. strlcat (logbuf, ">", BUFSIZ);
  439. return logbuf;
  440. #undef IFF_OUT_LOG
  441. }
  442. /* For debugging */
  443. static void
  444. if_dump (const struct interface *ifp)
  445. {
  446. struct listnode *node;
  447. struct connected *c __attribute__((unused));
  448. for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, c))
  449. zlog_info ("Interface %s vrf %u index %d metric %d mtu %d "
  450. #ifdef HAVE_IPV6
  451. "mtu6 %d "
  452. #endif /* HAVE_IPV6 */
  453. "%s",
  454. ifp->name, ifp->vrf_id, ifp->ifindex, ifp->metric, ifp->mtu,
  455. #ifdef HAVE_IPV6
  456. ifp->mtu6,
  457. #endif /* HAVE_IPV6 */
  458. if_flag_dump (ifp->flags));
  459. }
  460. /* Interface printing for all interface. */
  461. void
  462. if_dump_all (void)
  463. {
  464. struct list *intf_list;
  465. struct listnode *node;
  466. void *p;
  467. vrf_iter_t iter;
  468. for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
  469. if ((intf_list = vrf_iter2iflist (iter)) != NULL)
  470. for (ALL_LIST_ELEMENTS_RO (intf_list, node, p))
  471. if_dump (p);
  472. }
  473. DEFUN (interface_desc,
  474. interface_desc_cmd,
  475. "description .LINE",
  476. "Interface specific description\n"
  477. "Characters describing this interface\n")
  478. {
  479. struct interface *ifp;
  480. if (argc == 0)
  481. return CMD_SUCCESS;
  482. ifp = vty->index;
  483. if (ifp->desc)
  484. XFREE (MTYPE_TMP, ifp->desc);
  485. ifp->desc = argv_concat(argv, argc, 0);
  486. return CMD_SUCCESS;
  487. }
  488. DEFUN (no_interface_desc,
  489. no_interface_desc_cmd,
  490. "no description",
  491. NO_STR
  492. "Interface specific description\n")
  493. {
  494. struct interface *ifp;
  495. ifp = vty->index;
  496. if (ifp->desc)
  497. XFREE (MTYPE_TMP, ifp->desc);
  498. ifp->desc = NULL;
  499. return CMD_SUCCESS;
  500. }
  501. #ifdef SUNOS_5
  502. /* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
  503. * a seperate struct interface for each logical interface, so config
  504. * file may be full of 'interface fooX:Y'. Solaris however does not
  505. * expose logical interfaces via PF_ROUTE, so trying to track logical
  506. * interfaces can be fruitless, for that reason Quagga only tracks
  507. * the primary IP interface.
  508. *
  509. * We try accomodate SUNWzebra by:
  510. * - looking up the interface name, to see whether it exists, if so
  511. * its useable
  512. * - for protocol daemons, this could only because zebra told us of
  513. * the interface
  514. * - for zebra, only because it learnt from kernel
  515. * - if not:
  516. * - search the name to see if it contains a sub-ipif / logical interface
  517. * seperator, the ':' char. If it does:
  518. * - text up to that char must be the primary name - get that name.
  519. * if not:
  520. * - no idea, just get the name in its entirety.
  521. */
  522. static struct interface *
  523. if_sunwzebra_get (const char *name, size_t nlen, vrf_id_t vrf_id)
  524. {
  525. struct interface *ifp;
  526. size_t seppos = 0;
  527. if ( (ifp = if_lookup_by_name_len_vrf (name, nlen, vrf_id)) != NULL)
  528. return ifp;
  529. /* hunt the primary interface name... */
  530. while (seppos < nlen && name[seppos] != ':')
  531. seppos++;
  532. /* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */
  533. if (seppos < nlen)
  534. return if_get_by_name_len_vrf (name, seppos, vrf_id);
  535. else
  536. return if_get_by_name_len_vrf (name, nlen, vrf_id);
  537. }
  538. #endif /* SUNOS_5 */
  539. DEFUN (interface,
  540. interface_cmd,
  541. "interface IFNAME",
  542. "Select an interface to configure\n"
  543. "Interface's name\n")
  544. {
  545. struct interface *ifp;
  546. size_t sl;
  547. vrf_id_t vrf_id = VRF_DEFAULT;
  548. if ((sl = strlen(argv[0])) > INTERFACE_NAMSIZ)
  549. {
  550. vty_out (vty, "%% Interface name %s is invalid: length exceeds "
  551. "%d characters%s",
  552. argv[0], INTERFACE_NAMSIZ, VTY_NEWLINE);
  553. return CMD_WARNING;
  554. }
  555. if (argc > 1)
  556. VTY_GET_INTEGER ("VRF ID", vrf_id, argv[1]);
  557. #ifdef SUNOS_5
  558. ifp = if_sunwzebra_get (argv[0], sl, vrf_id);
  559. #else
  560. ifp = if_get_by_name_len_vrf (argv[0], sl, vrf_id);
  561. #endif /* SUNOS_5 */
  562. vty->index = ifp;
  563. vty->node = INTERFACE_NODE;
  564. return CMD_SUCCESS;
  565. }
  566. ALIAS (interface,
  567. interface_vrf_cmd,
  568. "interface IFNAME " VRF_CMD_STR,
  569. "Select an interface to configure\n"
  570. "Interface's name\n"
  571. VRF_CMD_HELP_STR)
  572. DEFUN_NOSH (no_interface,
  573. no_interface_cmd,
  574. "no interface IFNAME",
  575. NO_STR
  576. "Delete a pseudo interface's configuration\n"
  577. "Interface's name\n")
  578. {
  579. // deleting interface
  580. struct interface *ifp;
  581. vrf_id_t vrf_id = VRF_DEFAULT;
  582. if (argc > 1)
  583. VTY_GET_INTEGER ("VRF ID", vrf_id, argv[1]);
  584. ifp = if_lookup_by_name_vrf (argv[0], vrf_id);
  585. if (ifp == NULL)
  586. {
  587. vty_out (vty, "%% Interface %s does not exist%s", argv[0], VTY_NEWLINE);
  588. return CMD_WARNING;
  589. }
  590. if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
  591. {
  592. vty_out (vty, "%% Only inactive interfaces can be deleted%s",
  593. VTY_NEWLINE);
  594. return CMD_WARNING;
  595. }
  596. if_delete(ifp);
  597. return CMD_SUCCESS;
  598. }
  599. ALIAS (no_interface,
  600. no_interface_vrf_cmd,
  601. "no interface IFNAME " VRF_CMD_STR,
  602. NO_STR
  603. "Delete a pseudo interface's configuration\n"
  604. "Interface's name\n"
  605. VRF_CMD_HELP_STR)
  606. /* For debug purpose. */
  607. DEFUN (show_address,
  608. show_address_cmd,
  609. "show address",
  610. SHOW_STR
  611. "address\n")
  612. {
  613. struct listnode *node;
  614. struct listnode *node2;
  615. struct interface *ifp;
  616. struct connected *ifc;
  617. struct prefix *p;
  618. vrf_id_t vrf_id = VRF_DEFAULT;
  619. if (argc > 0)
  620. VTY_GET_INTEGER ("VRF ID", vrf_id, argv[0]);
  621. for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
  622. {
  623. for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
  624. {
  625. p = ifc->address;
  626. if (p->family == AF_INET)
  627. vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
  628. VTY_NEWLINE);
  629. }
  630. }
  631. return CMD_SUCCESS;
  632. }
  633. ALIAS (show_address,
  634. show_address_vrf_cmd,
  635. "show address " VRF_CMD_STR,
  636. SHOW_STR
  637. "address\n"
  638. VRF_CMD_HELP_STR)
  639. DEFUN (show_address_vrf_all,
  640. show_address_vrf_all_cmd,
  641. "show address " VRF_ALL_CMD_STR,
  642. SHOW_STR
  643. "address\n"
  644. VRF_ALL_CMD_HELP_STR)
  645. {
  646. struct list *intf_list;
  647. struct listnode *node;
  648. struct listnode *node2;
  649. struct interface *ifp;
  650. struct connected *ifc;
  651. struct prefix *p;
  652. vrf_iter_t iter;
  653. for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
  654. {
  655. intf_list = vrf_iter2iflist (iter);
  656. if (!intf_list || !listcount (intf_list))
  657. continue;
  658. vty_out (vty, "%sVRF %u%s%s", VTY_NEWLINE, vrf_iter2id (iter),
  659. VTY_NEWLINE, VTY_NEWLINE);
  660. for (ALL_LIST_ELEMENTS_RO (intf_list, node, ifp))
  661. {
  662. for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
  663. {
  664. p = ifc->address;
  665. if (p->family == AF_INET)
  666. vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
  667. VTY_NEWLINE);
  668. }
  669. }
  670. }
  671. return CMD_SUCCESS;
  672. }
  673. /* Allocate connected structure. */
  674. struct connected *
  675. connected_new (void)
  676. {
  677. return XCALLOC (MTYPE_CONNECTED, sizeof (struct connected));
  678. }
  679. /* Free connected structure. */
  680. void
  681. connected_free (struct connected *connected)
  682. {
  683. if (connected->address)
  684. prefix_free (connected->address);
  685. if (connected->destination)
  686. prefix_free (connected->destination);
  687. if (connected->label)
  688. XFREE (MTYPE_CONNECTED_LABEL, connected->label);
  689. XFREE (MTYPE_CONNECTED, connected);
  690. }
  691. /* Print if_addr structure. */
  692. static void __attribute__ ((unused))
  693. connected_log (struct connected *connected, char *str)
  694. {
  695. struct prefix *p;
  696. struct interface *ifp;
  697. char logbuf[BUFSIZ];
  698. char buf[BUFSIZ];
  699. ifp = connected->ifp;
  700. p = connected->address;
  701. snprintf (logbuf, BUFSIZ, "%s interface %s vrf %u %s %s/%d ",
  702. str, ifp->name, ifp->vrf_id, prefix_family_str (p),
  703. inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
  704. p->prefixlen);
  705. p = connected->destination;
  706. if (p)
  707. {
  708. strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
  709. BUFSIZ - strlen(logbuf));
  710. }
  711. zlog (NULL, LOG_INFO, "%s", logbuf);
  712. }
  713. /* If two connected address has same prefix return 1. */
  714. static int
  715. connected_same_prefix (struct prefix *p1, struct prefix *p2)
  716. {
  717. if (p1->family == p2->family)
  718. {
  719. if (p1->family == AF_INET &&
  720. IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4))
  721. return 1;
  722. #ifdef HAVE_IPV6
  723. if (p1->family == AF_INET6 &&
  724. IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6))
  725. return 1;
  726. #endif /* HAVE_IPV6 */
  727. }
  728. return 0;
  729. }
  730. struct connected *
  731. connected_delete_by_prefix (struct interface *ifp, struct prefix *p)
  732. {
  733. struct listnode *node;
  734. struct listnode *next;
  735. struct connected *ifc;
  736. /* In case of same prefix come, replace it with new one. */
  737. for (node = listhead (ifp->connected); node; node = next)
  738. {
  739. ifc = listgetdata (node);
  740. next = node->next;
  741. if (connected_same_prefix (ifc->address, p))
  742. {
  743. listnode_delete (ifp->connected, ifc);
  744. return ifc;
  745. }
  746. }
  747. return NULL;
  748. }
  749. /* Find the IPv4 address on our side that will be used when packets
  750. are sent to dst. */
  751. struct connected *
  752. connected_lookup_address (struct interface *ifp, struct in_addr dst)
  753. {
  754. struct prefix addr;
  755. struct listnode *cnode;
  756. struct connected *c;
  757. struct connected *match;
  758. addr.family = AF_INET;
  759. addr.u.prefix4 = dst;
  760. addr.prefixlen = IPV4_MAX_BITLEN;
  761. match = NULL;
  762. for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
  763. {
  764. if (c->address && (c->address->family == AF_INET) &&
  765. prefix_match(CONNECTED_PREFIX(c), &addr) &&
  766. (!match || (c->address->prefixlen > match->address->prefixlen)))
  767. match = c;
  768. }
  769. return match;
  770. }
  771. struct connected *
  772. connected_add_by_prefix (struct interface *ifp, struct prefix *p,
  773. struct prefix *destination)
  774. {
  775. struct connected *ifc;
  776. /* Allocate new connected address. */
  777. ifc = connected_new ();
  778. ifc->ifp = ifp;
  779. /* Fetch interface address */
  780. ifc->address = prefix_new();
  781. memcpy (ifc->address, p, sizeof(struct prefix));
  782. /* Fetch dest address */
  783. if (destination)
  784. {
  785. ifc->destination = prefix_new();
  786. memcpy (ifc->destination, destination, sizeof(struct prefix));
  787. }
  788. /* Add connected address to the interface. */
  789. listnode_add (ifp->connected, ifc);
  790. return ifc;
  791. }
  792. #ifndef HAVE_IF_NAMETOINDEX
  793. ifindex_t
  794. if_nametoindex (const char *name)
  795. {
  796. struct interface *ifp;
  797. return ((ifp = if_lookup_by_name_len(name, strnlen(name, IFNAMSIZ))) != NULL)
  798. ? ifp->ifindex : 0;
  799. }
  800. #endif
  801. #ifndef HAVE_IF_INDEXTONAME
  802. char *
  803. if_indextoname (ifindex_t ifindex, char *name)
  804. {
  805. struct interface *ifp;
  806. if (!(ifp = if_lookup_by_index(ifindex)))
  807. return NULL;
  808. strncpy (name, ifp->name, IFNAMSIZ);
  809. return ifp->name;
  810. }
  811. #endif
  812. #if 0 /* this route_table of struct connected's is unused
  813. * however, it would be good to use a route_table rather than
  814. * a list..
  815. */
  816. /* Interface looking up by interface's address. */
  817. /* Interface's IPv4 address reverse lookup table. */
  818. struct route_table *ifaddr_ipv4_table;
  819. /* struct route_table *ifaddr_ipv6_table; */
  820. static void
  821. ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
  822. {
  823. struct route_node *rn;
  824. struct prefix_ipv4 p;
  825. p.family = AF_INET;
  826. p.prefixlen = IPV4_MAX_PREFIXLEN;
  827. p.prefix = *ifaddr;
  828. rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
  829. if (rn)
  830. {
  831. route_unlock_node (rn);
  832. zlog_info ("ifaddr_ipv4_add(): address %s is already added",
  833. inet_ntoa (*ifaddr));
  834. return;
  835. }
  836. rn->info = ifp;
  837. }
  838. static void
  839. ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
  840. {
  841. struct route_node *rn;
  842. struct prefix_ipv4 p;
  843. p.family = AF_INET;
  844. p.prefixlen = IPV4_MAX_PREFIXLEN;
  845. p.prefix = *ifaddr;
  846. rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
  847. if (! rn)
  848. {
  849. zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
  850. inet_ntoa (*ifaddr));
  851. return;
  852. }
  853. rn->info = NULL;
  854. route_unlock_node (rn);
  855. route_unlock_node (rn);
  856. }
  857. /* Lookup interface by interface's IP address or interface index. */
  858. static struct interface *
  859. ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex)
  860. {
  861. struct prefix_ipv4 p;
  862. struct route_node *rn;
  863. struct interface *ifp;
  864. if (addr)
  865. {
  866. p.family = AF_INET;
  867. p.prefixlen = IPV4_MAX_PREFIXLEN;
  868. p.prefix = *addr;
  869. rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
  870. if (! rn)
  871. return NULL;
  872. ifp = rn->info;
  873. route_unlock_node (rn);
  874. return ifp;
  875. }
  876. else
  877. return if_lookup_by_index(ifindex);
  878. }
  879. #endif /* ifaddr_ipv4_table */
  880. /* Initialize interface list. */
  881. void
  882. if_init (vrf_id_t vrf_id, struct list **intf_list)
  883. {
  884. *intf_list = list_new ();
  885. #if 0
  886. ifaddr_ipv4_table = route_table_init ();
  887. #endif /* ifaddr_ipv4_table */
  888. (*intf_list)->cmp = (int (*)(void *, void *))if_cmp_func;
  889. if (vrf_id == VRF_DEFAULT)
  890. iflist = *intf_list;
  891. }
  892. void
  893. if_terminate (vrf_id_t vrf_id, struct list **intf_list)
  894. {
  895. for (;;)
  896. {
  897. struct interface *ifp;
  898. ifp = listnode_head (*intf_list);
  899. if (ifp == NULL)
  900. break;
  901. if_delete (ifp);
  902. }
  903. list_delete (*intf_list);
  904. *intf_list = NULL;
  905. if (vrf_id == VRF_DEFAULT)
  906. iflist = NULL;
  907. }
  908. const char *
  909. if_link_type_str (enum zebra_link_type llt)
  910. {
  911. switch (llt)
  912. {
  913. #define llts(T,S) case (T): return (S)
  914. llts(ZEBRA_LLT_UNKNOWN, "Unknown");
  915. llts(ZEBRA_LLT_ETHER, "Ethernet");
  916. llts(ZEBRA_LLT_EETHER, "Experimental Ethernet");
  917. llts(ZEBRA_LLT_AX25, "AX.25 Level 2");
  918. llts(ZEBRA_LLT_PRONET, "PROnet token ring");
  919. llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB");
  920. llts(ZEBRA_LLT_ARCNET, "ARCnet");
  921. llts(ZEBRA_LLT_APPLETLK, "AppleTalk");
  922. llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI");
  923. llts(ZEBRA_LLT_ATM, "ATM");
  924. llts(ZEBRA_LLT_METRICOM, "Metricom STRIP");
  925. llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4");
  926. llts(ZEBRA_LLT_EUI64, "EUI-64");
  927. llts(ZEBRA_LLT_INFINIBAND, "InfiniBand");
  928. llts(ZEBRA_LLT_SLIP, "SLIP");
  929. llts(ZEBRA_LLT_CSLIP, "Compressed SLIP");
  930. llts(ZEBRA_LLT_SLIP6, "SLIPv6");
  931. llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6");
  932. llts(ZEBRA_LLT_ROSE, "ROSE packet radio");
  933. llts(ZEBRA_LLT_X25, "CCITT X.25");
  934. llts(ZEBRA_LLT_PPP, "PPP");
  935. llts(ZEBRA_LLT_CHDLC, "Cisco HDLC");
  936. llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC");
  937. llts(ZEBRA_LLT_LAPB, "LAPB");
  938. llts(ZEBRA_LLT_IPIP, "IPIP Tunnel");
  939. llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel");
  940. llts(ZEBRA_LLT_FRAD, "FRAD");
  941. llts(ZEBRA_LLT_SKIP, "SKIP vif");
  942. llts(ZEBRA_LLT_LOOPBACK, "Loopback");
  943. llts(ZEBRA_LLT_LOCALTLK, "Localtalk");
  944. llts(ZEBRA_LLT_FDDI, "FDDI");
  945. llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT");
  946. llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel");
  947. llts(ZEBRA_LLT_IPGRE, "GRE over IP");
  948. llts(ZEBRA_LLT_PIMREG, "PIMSM registration");
  949. llts(ZEBRA_LLT_HIPPI, "HiPPI");
  950. llts(ZEBRA_LLT_IRDA, "IrDA");
  951. llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP");
  952. llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop");
  953. llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop");
  954. llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric");
  955. llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring");
  956. llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11");
  957. llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap");
  958. llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4");
  959. llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy");
  960. default:
  961. zlog_warn ("Unknown value %d", llt);
  962. return "Unknown type!";
  963. #undef llts
  964. }
  965. return NULL;
  966. }