ioctl_solaris.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Common ioctl functions for 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 "linklist.h"
  24. #include "if.h"
  25. #include "prefix.h"
  26. #include "ioctl.h"
  27. #include "log.h"
  28. #include "privs.h"
  29. #include "zebra/rib.h"
  30. #include "zebra/rt.h"
  31. extern struct zebra_privs_t zserv_privs;
  32. /* clear and set interface name string */
  33. void
  34. lifreq_set_name (struct lifreq *lifreq, struct interface *ifp)
  35. {
  36. strncpy (lifreq->lifr_name, ifp->name, IFNAMSIZ);
  37. }
  38. /* call ioctl system call */
  39. int
  40. if_ioctl (u_long request, caddr_t buffer)
  41. {
  42. int sock;
  43. int ret = 0;
  44. int err = 0;
  45. if (zserv_privs.change(ZPRIVS_RAISE))
  46. zlog (NULL, LOG_ERR, "Can't raise privileges");
  47. sock = socket (AF_INET, SOCK_DGRAM, 0);
  48. if (sock < 0)
  49. {
  50. if (zserv_privs.change(ZPRIVS_LOWER))
  51. zlog (NULL, LOG_ERR, "Can't lower privileges");
  52. perror ("socket");
  53. exit (1);
  54. }
  55. ret = ioctl (sock, request, buffer);
  56. if (zserv_privs.change(ZPRIVS_LOWER))
  57. zlog (NULL, LOG_ERR, "Can't lower privileges");
  58. if (ret < 0)
  59. {
  60. err = errno;
  61. }
  62. close (sock);
  63. if (ret < 0)
  64. {
  65. errno = err;
  66. return ret;
  67. }
  68. return 0;
  69. }
  70. int
  71. if_ioctl_ipv6 (u_long request, caddr_t buffer)
  72. {
  73. #ifdef HAVE_IPV6
  74. int sock;
  75. int ret = 0;
  76. int err = 0;
  77. if (zserv_privs.change(ZPRIVS_RAISE))
  78. zlog (NULL, LOG_ERR, "Can't raise privileges");
  79. sock = socket (AF_INET6, SOCK_DGRAM, 0);
  80. if (sock < 0)
  81. {
  82. if (zserv_privs.change(ZPRIVS_LOWER))
  83. zlog (NULL, LOG_ERR, "Can't lower privileges");
  84. perror ("socket");
  85. exit (1);
  86. }
  87. ret = ioctl (sock, request, buffer);
  88. if (zserv_privs.change(ZPRIVS_LOWER))
  89. zlog (NULL, LOG_ERR, "Can't lower privileges");
  90. if (ret < 0)
  91. {
  92. err = errno;
  93. }
  94. close (sock);
  95. if (ret < 0)
  96. {
  97. errno = err;
  98. return ret;
  99. }
  100. #endif /* HAVE_IPV6 */
  101. return 0;
  102. }
  103. /*
  104. * get interface metric
  105. * -- if value is not avaliable set -1
  106. */
  107. void
  108. if_get_metric (struct interface *ifp)
  109. {
  110. struct lifreq lifreq;
  111. int ret;
  112. lifreq_set_name (&lifreq, ifp);
  113. if (ifp->flags & IFF_IPV4)
  114. ret = AF_IOCTL (AF_INET, SIOCGLIFMETRIC, (caddr_t) & lifreq);
  115. #ifdef SOLARIS_IPV6
  116. else if (ifp->flags & IFF_IPV6)
  117. ret = AF_IOCTL (AF_INET6, SIOCGLIFMETRIC, (caddr_t) & lifreq);
  118. #endif /* SOLARIS_IPV6 */
  119. else
  120. ret = -1;
  121. if (ret < 0)
  122. return;
  123. ifp->metric = lifreq.lifr_metric;
  124. if (ifp->metric == 0)
  125. ifp->metric = 1;
  126. }
  127. /* get interface MTU */
  128. void
  129. if_get_mtu (struct interface *ifp)
  130. {
  131. struct lifreq lifreq;
  132. int ret;
  133. if (ifp->flags & IFF_IPV4)
  134. {
  135. lifreq_set_name (&lifreq, ifp);
  136. ret = AF_IOCTL (AF_INET, SIOCGLIFMTU, (caddr_t) & lifreq);
  137. if (ret < 0)
  138. {
  139. zlog_info ("Can't lookup mtu on %s by ioctl(SIOCGLIFMTU)",
  140. ifp->name);
  141. ifp->mtu = -1;
  142. }
  143. else
  144. {
  145. ifp->mtu = lifreq.lifr_metric;
  146. }
  147. }
  148. #ifdef HAVE_IPV6
  149. if ((ifp->flags & IFF_IPV6) == 0)
  150. return;
  151. memset(&lifreq, 0, sizeof(lifreq));
  152. lifreq_set_name (&lifreq, ifp);
  153. ret = AF_IOCTL (AF_INET6, SIOCGLIFMTU, (caddr_t) & lifreq);
  154. if (ret < 0)
  155. {
  156. zlog_info ("Can't lookup mtu6 on %s by ioctl(SIOCGIFMTU)", ifp->name);
  157. ifp->mtu6 = -1;
  158. }
  159. else
  160. {
  161. ifp->mtu6 = lifreq.lifr_metric;
  162. }
  163. #endif /* HAVE_IPV6 */
  164. }
  165. /* Set up interface's address, netmask (and broadcast? ).
  166. Solaris uses ifname:number semantics to set IP address aliases. */
  167. int
  168. if_set_prefix (struct interface *ifp, struct connected *ifc)
  169. {
  170. int ret;
  171. struct ifreq ifreq;
  172. struct sockaddr_in addr;
  173. struct sockaddr_in broad;
  174. struct sockaddr_in mask;
  175. struct prefix_ipv4 ifaddr;
  176. struct prefix_ipv4 *p;
  177. p = (struct prefix_ipv4 *) ifc->address;
  178. ifaddr = *p;
  179. strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
  180. addr.sin_addr = p->prefix;
  181. addr.sin_family = p->family;
  182. memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
  183. ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
  184. if (ret < 0)
  185. return ret;
  186. /* We need mask for make broadcast addr. */
  187. masklen2ip (p->prefixlen, &mask.sin_addr);
  188. if (if_is_broadcast (ifp))
  189. {
  190. apply_mask_ipv4 (&ifaddr);
  191. addr.sin_addr = ifaddr.prefix;
  192. broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
  193. broad.sin_family = p->family;
  194. memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
  195. ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) & ifreq);
  196. if (ret < 0)
  197. return ret;
  198. }
  199. mask.sin_family = p->family;
  200. #ifdef SUNOS_5
  201. memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
  202. #else
  203. memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
  204. #endif /* SUNOS_5 */
  205. ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) & ifreq);
  206. return ((ret < 0) ? ret : 0);
  207. }
  208. /* Set up interface's address, netmask (and broadcast).
  209. Solaris uses ifname:number semantics to set IP address aliases. */
  210. int
  211. if_unset_prefix (struct interface *ifp, struct connected *ifc)
  212. {
  213. int ret;
  214. struct ifreq ifreq;
  215. struct sockaddr_in addr;
  216. struct prefix_ipv4 *p;
  217. p = (struct prefix_ipv4 *) ifc->address;
  218. strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
  219. memset (&addr, 0, sizeof (struct sockaddr_in));
  220. addr.sin_family = p->family;
  221. memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
  222. ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
  223. if (ret < 0)
  224. return ret;
  225. return 0;
  226. }
  227. /* get interface flags */
  228. void
  229. if_get_flags (struct interface *ifp)
  230. {
  231. int ret;
  232. struct lifreq lifreq;
  233. unsigned long flags4 = 0, flags6 = 0;
  234. if (ifp->flags & IFF_IPV4)
  235. {
  236. lifreq_set_name (&lifreq, ifp);
  237. ret = AF_IOCTL (AF_INET, SIOCGLIFFLAGS, (caddr_t) & lifreq);
  238. flags4 = (lifreq.lifr_flags & 0xffffffff);
  239. if (!(flags4 & IFF_UP))
  240. flags4 &= ~IFF_IPV4;
  241. }
  242. if (ifp->flags & IFF_IPV6)
  243. {
  244. lifreq_set_name (&lifreq, ifp);
  245. ret = AF_IOCTL (AF_INET6, SIOCGLIFFLAGS, (caddr_t) & lifreq);
  246. flags6 = (lifreq.lifr_flags & 0xffffffff);
  247. if (!(flags6 & IFF_UP))
  248. flags6 &= ~IFF_IPV6;
  249. }
  250. ifp->flags = (flags4 | flags6);
  251. }
  252. /* Set interface flags */
  253. int
  254. if_set_flags (struct interface *ifp, unsigned long flags)
  255. {
  256. int ret;
  257. struct lifreq lifreq;
  258. lifreq_set_name (&lifreq, ifp);
  259. lifreq.lifr_flags = ifp->flags;
  260. lifreq.lifr_flags |= flags;
  261. if (ifp->flags & IFF_IPV4)
  262. ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq);
  263. else if (ifp->flags & IFF_IPV6)
  264. ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq);
  265. else
  266. ret = -1;
  267. if (ret < 0)
  268. zlog_info ("can't set interface flags on %s: %s", ifp->name,
  269. strerror (errno));
  270. else
  271. ret = 0;
  272. return ret;
  273. }
  274. /* Unset interface's flag. */
  275. int
  276. if_unset_flags (struct interface *ifp, unsigned long flags)
  277. {
  278. int ret;
  279. struct lifreq lifreq;
  280. lifreq_set_name (&lifreq, ifp);
  281. lifreq.lifr_flags = ifp->flags;
  282. lifreq.lifr_flags &= ~flags;
  283. if (ifp->flags & IFF_IPV4)
  284. ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq);
  285. else if (ifp->flags & IFF_IPV6)
  286. ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq);
  287. else
  288. ret = -1;
  289. if (ret < 0)
  290. zlog_info ("can't unset interface flags");
  291. else
  292. ret = 0;
  293. return ret;
  294. }
  295. #ifdef HAVE_IPV6
  296. /* Interface's address add/delete functions. */
  297. int
  298. if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
  299. {
  300. char addrbuf[INET_ADDRSTRLEN];
  301. inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix),
  302. addrbuf, sizeof (addrbuf));
  303. zlog_warn ("Can't set %s on interface %s", addrbuf, ifp->name);
  304. return 0;
  305. }
  306. int
  307. if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
  308. {
  309. char addrbuf[INET_ADDRSTRLEN];
  310. inet_ntop (AF_INET6, &(((struct prefix_ipv6 *) (ifc->address))->prefix),
  311. addrbuf, sizeof (addrbuf));
  312. zlog_warn ("Can't delete %s on interface %s", addrbuf, ifp->name);
  313. return 0;
  314. }
  315. #endif /* HAVE_IPV6 */