if_ioctl_solaris.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Interface looking up by ioctl () on Solaris.
  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 "privs.h"
  31. #include "zebra/interface.h"
  32. void lifreq_set_name (struct lifreq *, struct interface *);
  33. static int if_get_addr (struct interface *, struct sockaddr *);
  34. static void interface_info_ioctl (struct interface *);
  35. extern struct zebra_privs_t zserv_privs;
  36. int
  37. interface_list_ioctl (int af)
  38. {
  39. int ret;
  40. int sock;
  41. #define IFNUM_BASE 32
  42. struct lifnum lifn;
  43. int ifnum;
  44. struct lifreq *lifreq;
  45. struct lifconf lifconf;
  46. struct interface *ifp;
  47. int n;
  48. size_t needed, lastneeded = 0;
  49. char *buf = NULL;
  50. if (zserv_privs.change(ZPRIVS_RAISE))
  51. zlog (NULL, LOG_ERR, "Can't raise privileges");
  52. sock = socket (af, SOCK_DGRAM, 0);
  53. if (sock < 0)
  54. {
  55. zlog_warn ("Can't make %s socket stream: %s",
  56. (af == AF_INET ? "AF_INET" : "AF_INET6"), strerror (errno));
  57. if (zserv_privs.change(ZPRIVS_LOWER))
  58. zlog (NULL, LOG_ERR, "Can't lower privileges");
  59. return -1;
  60. }
  61. calculate_lifc_len: /* must hold privileges to enter here */
  62. lifn.lifn_family = af;
  63. lifn.lifn_flags = 0;
  64. ret = ioctl (sock, SIOCGLIFNUM, &lifn);
  65. if (zserv_privs.change(ZPRIVS_LOWER))
  66. zlog (NULL, LOG_ERR, "Can't lower privileges");
  67. if (ret < 0)
  68. {
  69. zlog_warn ("interface_list_ioctl: SIOCGLIFNUM failed %s",
  70. strerror (errno));
  71. close (sock);
  72. return -1;
  73. }
  74. ifnum = lifn.lifn_count;
  75. /*
  76. * When calculating the buffer size needed, add a small number
  77. * of interfaces to those we counted. We do this to capture
  78. * the interface status of potential interfaces which may have
  79. * been plumbed between the SIOCGLIFNUM and the SIOCGLIFCONF.
  80. */
  81. needed = (ifnum + 4) * sizeof (struct lifreq);
  82. if (needed > lastneeded || needed < lastneeded / 2)
  83. {
  84. if (buf != NULL)
  85. XFREE (MTYPE_TMP, buf);
  86. if ((buf = XMALLOC (MTYPE_TMP, needed)) == NULL)
  87. {
  88. zlog_warn ("interface_list_ioctl: malloc failed");
  89. close (sock);
  90. return -1;
  91. }
  92. }
  93. lastneeded = needed;
  94. lifconf.lifc_family = af;
  95. lifconf.lifc_flags = 0;
  96. lifconf.lifc_len = needed;
  97. lifconf.lifc_buf = buf;
  98. if (zserv_privs.change(ZPRIVS_RAISE))
  99. zlog (NULL, LOG_ERR, "Can't raise privileges");
  100. ret = ioctl (sock, SIOCGLIFCONF, &lifconf);
  101. if (ret < 0)
  102. {
  103. if (errno == EINVAL)
  104. goto calculate_lifc_len; /* deliberately hold privileges */
  105. zlog_warn ("SIOCGLIFCONF: %s", strerror (errno));
  106. if (zserv_privs.change(ZPRIVS_LOWER))
  107. zlog (NULL, LOG_ERR, "Can't lower privileges");
  108. goto end;
  109. }
  110. if (zserv_privs.change(ZPRIVS_LOWER))
  111. zlog (NULL, LOG_ERR, "Can't lower privileges");
  112. /* Allocate interface. */
  113. lifreq = lifconf.lifc_req;
  114. for (n = 0; n < lifconf.lifc_len; n += sizeof (struct lifreq))
  115. {
  116. ifp = if_get_by_name (lifreq->lifr_name);
  117. if (lifreq->lifr_addr.ss_family == AF_INET)
  118. ifp->flags |= IFF_IPV4;
  119. if (lifreq->lifr_addr.ss_family == AF_INET6)
  120. {
  121. #ifdef HAVE_IPV6
  122. ifp->flags |= IFF_IPV6;
  123. #else
  124. lifreq++;
  125. continue;
  126. #endif /* HAVE_IPV6 */
  127. }
  128. if_add_update (ifp);
  129. interface_info_ioctl (ifp);
  130. if_get_addr (ifp, (struct sockaddr *) &lifreq->lifr_addr);
  131. lifreq++;
  132. }
  133. end:
  134. close (sock);
  135. XFREE (MTYPE_TMP, lifconf.lifc_buf);
  136. return ret;
  137. }
  138. /* Get interface's index by ioctl. */
  139. int
  140. if_get_index (struct interface *ifp)
  141. {
  142. int ret;
  143. struct lifreq lifreq;
  144. lifreq_set_name (&lifreq, ifp);
  145. if (ifp->flags & IFF_IPV4)
  146. ret = AF_IOCTL (AF_INET, SIOCGLIFINDEX, (caddr_t) & lifreq);
  147. else if (ifp->flags & IFF_IPV6)
  148. ret = AF_IOCTL (AF_INET6, SIOCGLIFINDEX, (caddr_t) & lifreq);
  149. else
  150. ret = -1;
  151. if (ret < 0)
  152. {
  153. zlog_warn ("SIOCGLIFINDEX(%s) failed", ifp->name);
  154. return ret;
  155. }
  156. /* OK we got interface index. */
  157. #ifdef ifr_ifindex
  158. ifp->ifindex = lifreq.lifr_ifindex;
  159. #else
  160. ifp->ifindex = lifreq.lifr_index;
  161. #endif
  162. return ifp->ifindex;
  163. }
  164. /* Interface address lookup by ioctl. This function only looks up
  165. IPv4 address. */
  166. #define ADDRLEN(sa) (((sa)->sa_family == AF_INET ? \
  167. sizeof (struct sockaddr_in) : sizeof (struct sockaddr_in6)))
  168. #define SIN(s) ((struct sockaddr_in *)(s))
  169. #define SIN6(s) ((struct sockaddr_in6 *)(s))
  170. static int
  171. if_get_addr (struct interface *ifp, struct sockaddr *addr)
  172. {
  173. int ret;
  174. struct lifreq lifreq;
  175. struct sockaddr_storage mask, dest;
  176. char *dest_pnt = NULL;
  177. u_char prefixlen = 0;
  178. afi_t af;
  179. /* Interface's name and address family. */
  180. strncpy (lifreq.lifr_name, ifp->name, IFNAMSIZ);
  181. /* Interface's address. */
  182. memcpy (&lifreq.lifr_addr, addr, ADDRLEN (addr));
  183. af = addr->sa_family;
  184. /* Point to point or broad cast address pointer init. */
  185. dest_pnt = NULL;
  186. if (ifp->flags & IFF_POINTOPOINT)
  187. {
  188. ret = AF_IOCTL (af, SIOCGLIFDSTADDR, (caddr_t) & lifreq);
  189. if (ret < 0)
  190. {
  191. zlog_warn ("SIOCGLIFDSTADDR (%s) fail: %s",
  192. ifp->name, strerror (errno));
  193. return ret;
  194. }
  195. memcpy (&dest, &lifreq.lifr_dstaddr, ADDRLEN (addr));
  196. if (af == AF_INET)
  197. dest_pnt = (char *) &(SIN (&dest)->sin_addr);
  198. else
  199. dest_pnt = (char *) &(SIN6 (&dest)->sin6_addr);
  200. }
  201. if (af == AF_INET)
  202. {
  203. ret = if_ioctl (SIOCGLIFNETMASK, (caddr_t) & lifreq);
  204. if (ret < 0)
  205. {
  206. if (errno != EADDRNOTAVAIL)
  207. {
  208. zlog_warn ("SIOCGLIFNETMASK (%s) fail: %s", ifp->name,
  209. strerror (errno));
  210. return ret;
  211. }
  212. return 0;
  213. }
  214. memcpy (&mask, &lifreq.lifr_addr, ADDRLEN (addr));
  215. prefixlen = ip_masklen (SIN (&mask)->sin_addr);
  216. if (ifp->flags & IFF_BROADCAST)
  217. {
  218. ret = if_ioctl (SIOCGLIFBRDADDR, (caddr_t) & lifreq);
  219. if (ret < 0)
  220. {
  221. if (errno != EADDRNOTAVAIL)
  222. {
  223. zlog_warn ("SIOCGLIFBRDADDR (%s) fail: %s",
  224. ifp->name, strerror (errno));
  225. return ret;
  226. }
  227. return 0;
  228. }
  229. memcpy (&dest, &lifreq.lifr_broadaddr, sizeof (struct sockaddr_in));
  230. dest_pnt = (char *) &SIN (&dest)->sin_addr;
  231. }
  232. }
  233. #ifdef HAVE_IPV6
  234. else if (af == AF_INET6)
  235. {
  236. if (ifp->flags & IFF_POINTOPOINT)
  237. {
  238. prefixlen = IPV6_MAX_BITLEN;
  239. }
  240. else
  241. {
  242. ret = if_ioctl_ipv6 (SIOCGLIFSUBNET, (caddr_t) & lifreq);
  243. if (ret < 0)
  244. {
  245. zlog_warn ("SIOCGLIFSUBNET (%s) fail: %s",
  246. ifp->name, strerror (errno));
  247. }
  248. else
  249. {
  250. prefixlen = lifreq.lifr_addrlen;
  251. }
  252. }
  253. }
  254. #endif /* HAVE_IPV6 */
  255. /* Set address to the interface. */
  256. if (af == AF_INET)
  257. connected_add_ipv4 (ifp, 0, &SIN (addr)->sin_addr, prefixlen,
  258. (struct in_addr *) dest_pnt, NULL);
  259. #ifdef HAVE_IPV6
  260. else if (af == AF_INET6)
  261. connected_add_ipv6 (ifp, &SIN6 (addr)->sin6_addr, prefixlen,
  262. (struct in6_addr *) dest_pnt);
  263. #endif /* HAVE_IPV6 */
  264. return 0;
  265. }
  266. /* Fetch interface information via ioctl(). */
  267. static void
  268. interface_info_ioctl (struct interface *ifp)
  269. {
  270. if_get_index (ifp);
  271. if_get_flags (ifp);
  272. if_get_mtu (ifp);
  273. if_get_metric (ifp);
  274. }
  275. /* Lookup all interface information. */
  276. void
  277. interface_list ()
  278. {
  279. interface_list_ioctl (AF_INET);
  280. interface_list_ioctl (AF_INET6);
  281. }
  282. struct connected *
  283. if_lookup_linklocal (struct interface *ifp)
  284. {
  285. #ifdef HAVE_IPV6
  286. listnode node;
  287. struct connected *ifc;
  288. if (ifp == NULL)
  289. return NULL;
  290. for (node = listhead (ifp->connected); node; node = nextnode (node))
  291. {
  292. ifc = getdata (node);
  293. if ((ifc->address->family == AF_INET6) &&
  294. (IN6_IS_ADDR_LINKLOCAL (&ifc->address->u.prefix6)))
  295. return ifc;
  296. }
  297. #endif /* HAVE_IPV6 */
  298. return NULL;
  299. }