if_ioctl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Interface looking up by ioctl ().
  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 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 "sockunion.h"
  25. #include "prefix.h"
  26. #include "ioctl.h"
  27. #include "connected.h"
  28. #include "memory.h"
  29. #include "log.h"
  30. #include "vrf.h"
  31. #include "vty.h"
  32. #include "zebra/interface.h"
  33. #include "zebra/rib.h"
  34. /* Interface looking up using infamous SIOCGIFCONF. */
  35. static int
  36. interface_list_ioctl (void)
  37. {
  38. int ret;
  39. int sock;
  40. #define IFNUM_BASE 32
  41. int ifnum;
  42. struct ifreq *ifreq;
  43. struct ifconf ifconf;
  44. struct interface *ifp;
  45. int n;
  46. int lastlen;
  47. /* Normally SIOCGIFCONF works with AF_INET socket. */
  48. sock = socket (AF_INET, SOCK_DGRAM, 0);
  49. if (sock < 0)
  50. {
  51. zlog_warn ("Can't make AF_INET socket stream: %s", safe_strerror (errno));
  52. return -1;
  53. }
  54. /* Set initial ifreq count. This will be double when SIOCGIFCONF
  55. fail. Solaris has SIOCGIFNUM. */
  56. #ifdef SIOCGIFNUM
  57. ret = ioctl (sock, SIOCGIFNUM, &ifnum);
  58. if (ret < 0)
  59. ifnum = IFNUM_BASE;
  60. else
  61. ifnum++;
  62. #else
  63. ifnum = IFNUM_BASE;
  64. #endif /* SIOCGIFNUM */
  65. ifconf.ifc_buf = NULL;
  66. lastlen = 0;
  67. /* Loop until SIOCGIFCONF success. */
  68. for (;;)
  69. {
  70. ifconf.ifc_len = sizeof (struct ifreq) * ifnum;
  71. ifconf.ifc_buf = XREALLOC(MTYPE_TMP, ifconf.ifc_buf, ifconf.ifc_len);
  72. ret = ioctl(sock, SIOCGIFCONF, &ifconf);
  73. if (ret < 0)
  74. {
  75. zlog_warn ("SIOCGIFCONF: %s", safe_strerror(errno));
  76. goto end;
  77. }
  78. /* Repeatedly get info til buffer fails to grow. */
  79. if (ifconf.ifc_len > lastlen)
  80. {
  81. lastlen = ifconf.ifc_len;
  82. ifnum += 10;
  83. continue;
  84. }
  85. /* Success. */
  86. break;
  87. }
  88. /* Allocate interface. */
  89. ifreq = ifconf.ifc_req;
  90. #ifdef OPEN_BSD
  91. for (n = 0; n < ifconf.ifc_len; )
  92. {
  93. int size;
  94. ifreq = (struct ifreq *)((caddr_t) ifconf.ifc_req + n);
  95. ifp = if_get_by_name_len(ifreq->ifr_name,
  96. strnlen(ifreq->ifr_name,
  97. sizeof(ifreq->ifr_name)));
  98. if_add_update (ifp);
  99. size = ifreq->ifr_addr.sa_len;
  100. if (size < sizeof (ifreq->ifr_addr))
  101. size = sizeof (ifreq->ifr_addr);
  102. size += sizeof (ifreq->ifr_name);
  103. n += size;
  104. }
  105. #else
  106. for (n = 0; n < ifconf.ifc_len; n += sizeof(struct ifreq))
  107. {
  108. ifp = if_get_by_name_len(ifreq->ifr_name,
  109. strnlen(ifreq->ifr_name,
  110. sizeof(ifreq->ifr_name)));
  111. if_add_update (ifp);
  112. ifreq++;
  113. }
  114. #endif /* OPEN_BSD */
  115. end:
  116. close (sock);
  117. XFREE (MTYPE_TMP, ifconf.ifc_buf);
  118. return ret;
  119. }
  120. /* Get interface's index by ioctl. */
  121. static int
  122. if_get_index (struct interface *ifp)
  123. {
  124. #if defined(HAVE_IF_NAMETOINDEX)
  125. /* Modern systems should have if_nametoindex(3). */
  126. ifp->ifindex = if_nametoindex(ifp->name);
  127. #elif defined(SIOCGIFINDEX) && !defined(HAVE_BROKEN_ALIASES)
  128. /* Fall-back for older linuxes. */
  129. int ret;
  130. struct ifreq ifreq;
  131. static int if_fake_index;
  132. ifreq_set_name (&ifreq, ifp);
  133. ret = if_ioctl (SIOCGIFINDEX, (caddr_t) &ifreq);
  134. if (ret < 0)
  135. {
  136. /* Linux 2.0.X does not have interface index. */
  137. ifp->ifindex = if_fake_index++;
  138. return ifp->ifindex;
  139. }
  140. /* OK we got interface index. */
  141. #ifdef ifr_ifindex
  142. ifp->ifindex = ifreq.ifr_ifindex;
  143. #else
  144. ifp->ifindex = ifreq.ifr_index;
  145. #endif
  146. #else
  147. /* Linux 2.2.X does not provide individual interface index
  148. for aliases and we know it. For others issue a warning. */
  149. #if !defined(HAVE_BROKEN_ALIASES)
  150. #warning "Using if_fake_index. You may want to add appropriate"
  151. #warning "mapping from ifname to ifindex for your system..."
  152. #endif
  153. /* This branch probably won't provide usable results, but anyway... */
  154. static int if_fake_index = 1;
  155. ifp->ifindex = if_fake_index++;
  156. #endif
  157. return ifp->ifindex;
  158. }
  159. #ifdef SIOCGIFHWADDR
  160. static int
  161. if_get_hwaddr (struct interface *ifp)
  162. {
  163. int ret;
  164. struct ifreq ifreq;
  165. int i;
  166. strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
  167. ifreq.ifr_addr.sa_family = AF_INET;
  168. /* Fetch Hardware address if available. */
  169. ret = if_ioctl (SIOCGIFHWADDR, (caddr_t) &ifreq);
  170. if (ret < 0)
  171. ifp->hw_addr_len = 0;
  172. else
  173. {
  174. memcpy (ifp->hw_addr, ifreq.ifr_hwaddr.sa_data, 6);
  175. for (i = 0; i < 6; i++)
  176. if (ifp->hw_addr[i] != 0)
  177. break;
  178. if (i == 6)
  179. ifp->hw_addr_len = 0;
  180. else
  181. ifp->hw_addr_len = 6;
  182. }
  183. return 0;
  184. }
  185. #endif /* SIOCGIFHWADDR */
  186. #ifdef HAVE_GETIFADDRS
  187. #include <ifaddrs.h>
  188. static int
  189. if_getaddrs (void)
  190. {
  191. int ret;
  192. struct ifaddrs *ifap;
  193. struct ifaddrs *ifapfree;
  194. struct interface *ifp;
  195. int prefixlen;
  196. ret = getifaddrs (&ifap);
  197. if (ret != 0)
  198. {
  199. zlog_err ("getifaddrs(): %s", safe_strerror (errno));
  200. return -1;
  201. }
  202. for (ifapfree = ifap; ifap; ifap = ifap->ifa_next)
  203. {
  204. if (ifap->ifa_addr == NULL)
  205. {
  206. zlog_err ("%s: nonsensical ifaddr with NULL ifa_addr, ifname %s",
  207. __func__, (ifap->ifa_name ? ifap->ifa_name : "(null)"));
  208. continue;
  209. }
  210. ifp = if_lookup_by_name (ifap->ifa_name);
  211. if (ifp == NULL)
  212. {
  213. zlog_err ("if_getaddrs(): Can't lookup interface %s\n",
  214. ifap->ifa_name);
  215. continue;
  216. }
  217. if (ifap->ifa_addr->sa_family == AF_INET)
  218. {
  219. struct sockaddr_in *addr;
  220. struct sockaddr_in *mask;
  221. struct sockaddr_in *dest;
  222. struct in_addr *dest_pnt;
  223. int flags = 0;
  224. addr = (struct sockaddr_in *) ifap->ifa_addr;
  225. mask = (struct sockaddr_in *) ifap->ifa_netmask;
  226. prefixlen = ip_masklen (mask->sin_addr);
  227. dest_pnt = NULL;
  228. if (ifap->ifa_dstaddr &&
  229. !IPV4_ADDR_SAME(&addr->sin_addr,
  230. &((struct sockaddr_in *)
  231. ifap->ifa_dstaddr)->sin_addr))
  232. {
  233. dest = (struct sockaddr_in *) ifap->ifa_dstaddr;
  234. dest_pnt = &dest->sin_addr;
  235. flags = ZEBRA_IFA_PEER;
  236. }
  237. else if (ifap->ifa_broadaddr &&
  238. !IPV4_ADDR_SAME(&addr->sin_addr,
  239. &((struct sockaddr_in *)
  240. ifap->ifa_broadaddr)->sin_addr))
  241. {
  242. dest = (struct sockaddr_in *) ifap->ifa_broadaddr;
  243. dest_pnt = &dest->sin_addr;
  244. }
  245. connected_add_ipv4 (ifp, flags, &addr->sin_addr,
  246. prefixlen, dest_pnt, NULL);
  247. }
  248. #ifdef HAVE_IPV6
  249. if (ifap->ifa_addr->sa_family == AF_INET6)
  250. {
  251. struct sockaddr_in6 *addr;
  252. struct sockaddr_in6 *mask;
  253. struct sockaddr_in6 *dest;
  254. struct in6_addr *dest_pnt;
  255. int flags = 0;
  256. addr = (struct sockaddr_in6 *) ifap->ifa_addr;
  257. mask = (struct sockaddr_in6 *) ifap->ifa_netmask;
  258. prefixlen = ip6_masklen (mask->sin6_addr);
  259. dest_pnt = NULL;
  260. if (ifap->ifa_dstaddr &&
  261. !IPV6_ADDR_SAME(&addr->sin6_addr,
  262. &((struct sockaddr_in6 *)
  263. ifap->ifa_dstaddr)->sin6_addr))
  264. {
  265. dest = (struct sockaddr_in6 *) ifap->ifa_dstaddr;
  266. dest_pnt = &dest->sin6_addr;
  267. flags = ZEBRA_IFA_PEER;
  268. }
  269. else if (ifap->ifa_broadaddr &&
  270. !IPV6_ADDR_SAME(&addr->sin6_addr,
  271. &((struct sockaddr_in6 *)
  272. ifap->ifa_broadaddr)->sin6_addr))
  273. {
  274. dest = (struct sockaddr_in6 *) ifap->ifa_broadaddr;
  275. dest_pnt = &dest->sin6_addr;
  276. }
  277. #if defined(KAME)
  278. if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr))
  279. {
  280. addr->sin6_scope_id =
  281. ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]);
  282. addr->sin6_addr.s6_addr[2] = addr->sin6_addr.s6_addr[3] = 0;
  283. }
  284. #endif
  285. connected_add_ipv6 (ifp, flags, &addr->sin6_addr, prefixlen,
  286. dest_pnt, NULL);
  287. }
  288. #endif /* HAVE_IPV6 */
  289. }
  290. freeifaddrs (ifapfree);
  291. return 0;
  292. }
  293. #else /* HAVE_GETIFADDRS */
  294. /* Interface address lookup by ioctl. This function only looks up
  295. IPv4 address. */
  296. int
  297. if_get_addr (struct interface *ifp)
  298. {
  299. int ret;
  300. struct ifreq ifreq;
  301. struct sockaddr_in addr;
  302. struct sockaddr_in mask;
  303. struct sockaddr_in dest;
  304. struct in_addr *dest_pnt;
  305. u_char prefixlen;
  306. int flags = 0;
  307. /* Interface's name and address family. */
  308. strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
  309. ifreq.ifr_addr.sa_family = AF_INET;
  310. /* Interface's address. */
  311. ret = if_ioctl (SIOCGIFADDR, (caddr_t) &ifreq);
  312. if (ret < 0)
  313. {
  314. if (errno != EADDRNOTAVAIL)
  315. {
  316. zlog_warn ("SIOCGIFADDR fail: %s", safe_strerror (errno));
  317. return ret;
  318. }
  319. return 0;
  320. }
  321. memcpy (&addr, &ifreq.ifr_addr, sizeof (struct sockaddr_in));
  322. /* Interface's network mask. */
  323. ret = if_ioctl (SIOCGIFNETMASK, (caddr_t) &ifreq);
  324. if (ret < 0)
  325. {
  326. if (errno != EADDRNOTAVAIL)
  327. {
  328. zlog_warn ("SIOCGIFNETMASK fail: %s", safe_strerror (errno));
  329. return ret;
  330. }
  331. return 0;
  332. }
  333. #ifdef ifr_netmask
  334. memcpy (&mask, &ifreq.ifr_netmask, sizeof (struct sockaddr_in));
  335. #else
  336. memcpy (&mask, &ifreq.ifr_addr, sizeof (struct sockaddr_in));
  337. #endif /* ifr_netmask */
  338. prefixlen = ip_masklen (mask.sin_addr);
  339. /* Point to point or borad cast address pointer init. */
  340. dest_pnt = NULL;
  341. ret = if_ioctl (SIOCGIFDSTADDR, (caddr_t) &ifreq);
  342. if (ret < 0)
  343. {
  344. if (errno != EADDRNOTAVAIL)
  345. zlog_warn ("SIOCGIFDSTADDR fail: %s", safe_strerror (errno));
  346. }
  347. else if (!IPV4_ADDR_SAME(&addr.sin_addr, &ifreq.ifr_dstaddr.sin_addr))
  348. {
  349. memcpy (&dest, &ifreq.ifr_dstaddr, sizeof (struct sockaddr_in));
  350. dest_pnt = &dest.sin_addr;
  351. flags = ZEBRA_IFA_PEER;
  352. }
  353. if (!dest_pnt)
  354. {
  355. ret = if_ioctl (SIOCGIFBRDADDR, (caddr_t) &ifreq);
  356. if (ret < 0)
  357. {
  358. if (errno != EADDRNOTAVAIL)
  359. zlog_warn ("SIOCGIFBRDADDR fail: %s", safe_strerror (errno));
  360. }
  361. else if (!IPV4_ADDR_SAME(&addr.sin_addr, &ifreq.ifr_broadaddr.sin_addr))
  362. {
  363. memcpy (&dest, &ifreq.ifr_broadaddr, sizeof (struct sockaddr_in));
  364. dest_pnt = &dest.sin_addr;
  365. }
  366. }
  367. /* Set address to the interface. */
  368. connected_add_ipv4 (ifp, flags, &addr.sin_addr, prefixlen, dest_pnt, NULL);
  369. return 0;
  370. }
  371. #endif /* HAVE_GETIFADDRS */
  372. /* Fetch interface information via ioctl(). */
  373. static void
  374. interface_info_ioctl ()
  375. {
  376. struct listnode *node, *nnode;
  377. struct interface *ifp;
  378. for (ALL_LIST_ELEMENTS (iflist, node, nnode, ifp))
  379. {
  380. if_get_index (ifp);
  381. #ifdef SIOCGIFHWADDR
  382. if_get_hwaddr (ifp);
  383. #endif /* SIOCGIFHWADDR */
  384. if_get_flags (ifp);
  385. #ifndef HAVE_GETIFADDRS
  386. if_get_addr (ifp);
  387. #endif /* ! HAVE_GETIFADDRS */
  388. if_get_mtu (ifp);
  389. if_get_metric (ifp);
  390. }
  391. }
  392. /* Lookup all interface information. */
  393. void
  394. interface_list (struct zebra_vrf *zvrf)
  395. {
  396. if (zvrf->vrf_id != VRF_DEFAULT)
  397. {
  398. zlog_warn ("interface_list: ignore VRF %u", zvrf->vrf_id);
  399. return;
  400. }
  401. /* Linux can do both proc & ioctl, ioctl is the only way to get
  402. interface aliases in 2.2 series kernels. */
  403. #ifdef HAVE_PROC_NET_DEV
  404. interface_list_proc ();
  405. #endif /* HAVE_PROC_NET_DEV */
  406. interface_list_ioctl ();
  407. /* After listing is done, get index, address, flags and other
  408. interface's information. */
  409. interface_info_ioctl ();
  410. #ifdef HAVE_GETIFADDRS
  411. if_getaddrs ();
  412. #endif /* HAVE_GETIFADDRS */
  413. #if defined(HAVE_IPV6) && defined(HAVE_PROC_NET_IF_INET6)
  414. /* Linux provides interface's IPv6 address via
  415. /proc/net/if_inet6. */
  416. ifaddr_proc_ipv6 ();
  417. #endif /* HAVE_IPV6 && HAVE_PROC_NET_IF_INET6 */
  418. }