ioctl_solaris.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 "vty.h"
  30. #include "zebra/rib.h"
  31. #include "zebra/rt.h"
  32. #include "zebra/interface.h"
  33. #include "zebra/ioctl_solaris.h"
  34. extern struct zebra_privs_t zserv_privs;
  35. /* clear and set interface name string */
  36. void
  37. lifreq_set_name (struct lifreq *lifreq, const char *ifname)
  38. {
  39. strncpy (lifreq->lifr_name, ifname, IFNAMSIZ);
  40. }
  41. /* call ioctl system call */
  42. int
  43. if_ioctl (u_long request, caddr_t buffer)
  44. {
  45. int sock;
  46. int ret;
  47. int err;
  48. if (zserv_privs.change(ZPRIVS_RAISE))
  49. zlog (NULL, LOG_ERR, "Can't raise privileges");
  50. sock = socket (AF_INET, SOCK_DGRAM, 0);
  51. if (sock < 0)
  52. {
  53. int save_errno = errno;
  54. if (zserv_privs.change(ZPRIVS_LOWER))
  55. zlog (NULL, LOG_ERR, "Can't lower privileges");
  56. zlog_err("Cannot create UDP socket: %s", safe_strerror(save_errno));
  57. exit (1);
  58. }
  59. if ((ret = ioctl (sock, request, buffer)) < 0)
  60. err = errno;
  61. if (zserv_privs.change(ZPRIVS_LOWER))
  62. zlog (NULL, LOG_ERR, "Can't lower privileges");
  63. close (sock);
  64. if (ret < 0)
  65. {
  66. errno = err;
  67. return ret;
  68. }
  69. return 0;
  70. }
  71. int
  72. if_ioctl_ipv6 (u_long request, caddr_t buffer)
  73. {
  74. #ifdef HAVE_IPV6
  75. int sock;
  76. int ret;
  77. int err;
  78. if (zserv_privs.change(ZPRIVS_RAISE))
  79. zlog (NULL, LOG_ERR, "Can't raise privileges");
  80. sock = socket (AF_INET6, SOCK_DGRAM, 0);
  81. if (sock < 0)
  82. {
  83. int save_errno = errno;
  84. if (zserv_privs.change(ZPRIVS_LOWER))
  85. zlog (NULL, LOG_ERR, "Can't lower privileges");
  86. zlog_err("Cannot create IPv6 datagram socket: %s",
  87. safe_strerror(save_errno));
  88. exit (1);
  89. }
  90. if ((ret = ioctl (sock, request, buffer)) < 0)
  91. err = errno;
  92. if (zserv_privs.change(ZPRIVS_LOWER))
  93. zlog (NULL, LOG_ERR, "Can't lower privileges");
  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->name);
  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. u_char changed = 0;
  134. if (ifp->flags & IFF_IPV4)
  135. {
  136. lifreq_set_name (&lifreq, ifp->name);
  137. ret = AF_IOCTL (AF_INET, SIOCGLIFMTU, (caddr_t) & lifreq);
  138. if (ret < 0)
  139. {
  140. zlog_info ("Can't lookup mtu on %s by ioctl(SIOCGLIFMTU)",
  141. ifp->name);
  142. ifp->mtu = -1;
  143. }
  144. else
  145. {
  146. ifp->mtu = lifreq.lifr_metric;
  147. changed = 1;
  148. }
  149. }
  150. #ifdef HAVE_IPV6
  151. if (ifp->flags & IFF_IPV6)
  152. {
  153. memset(&lifreq, 0, sizeof(lifreq));
  154. lifreq_set_name (&lifreq, ifp->name);
  155. ret = AF_IOCTL (AF_INET6, SIOCGLIFMTU, (caddr_t) & lifreq);
  156. if (ret < 0)
  157. {
  158. zlog_info ("Can't lookup mtu6 on %s by ioctl(SIOCGIFMTU)", ifp->name);
  159. ifp->mtu6 = -1;
  160. }
  161. else
  162. {
  163. ifp->mtu6 = lifreq.lifr_metric;
  164. changed = 1;
  165. }
  166. }
  167. #endif /* HAVE_IPV6 */
  168. if (changed)
  169. zebra_interface_up_update(ifp);
  170. }
  171. /* Set up interface's address, netmask (and broadcast? ).
  172. Solaris uses ifname:number semantics to set IP address aliases. */
  173. int
  174. if_set_prefix (struct interface *ifp, struct connected *ifc)
  175. {
  176. int ret;
  177. struct ifreq ifreq;
  178. struct sockaddr_in addr;
  179. struct sockaddr_in broad;
  180. struct sockaddr_in mask;
  181. struct prefix_ipv4 ifaddr;
  182. struct prefix_ipv4 *p;
  183. p = (struct prefix_ipv4 *) ifc->address;
  184. ifaddr = *p;
  185. strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
  186. addr.sin_addr = p->prefix;
  187. addr.sin_family = p->family;
  188. memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
  189. ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
  190. if (ret < 0)
  191. return ret;
  192. /* We need mask for make broadcast addr. */
  193. masklen2ip (p->prefixlen, &mask.sin_addr);
  194. if (if_is_broadcast (ifp))
  195. {
  196. apply_mask_ipv4 (&ifaddr);
  197. addr.sin_addr = ifaddr.prefix;
  198. broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
  199. broad.sin_family = p->family;
  200. memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
  201. ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) & ifreq);
  202. if (ret < 0)
  203. return ret;
  204. }
  205. mask.sin_family = p->family;
  206. #ifdef SUNOS_5
  207. memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
  208. #else
  209. memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
  210. #endif /* SUNOS_5 */
  211. ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) & ifreq);
  212. return ((ret < 0) ? ret : 0);
  213. }
  214. /* Set up interface's address, netmask (and broadcast).
  215. Solaris uses ifname:number semantics to set IP address aliases. */
  216. int
  217. if_unset_prefix (struct interface *ifp, struct connected *ifc)
  218. {
  219. int ret;
  220. struct ifreq ifreq;
  221. struct sockaddr_in addr;
  222. struct prefix_ipv4 *p;
  223. p = (struct prefix_ipv4 *) ifc->address;
  224. strncpy (ifreq.ifr_name, ifp->name, IFNAMSIZ);
  225. memset (&addr, 0, sizeof (struct sockaddr_in));
  226. addr.sin_family = p->family;
  227. memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
  228. ret = if_ioctl (SIOCSIFADDR, (caddr_t) & ifreq);
  229. if (ret < 0)
  230. return ret;
  231. return 0;
  232. }
  233. /* Get just the flags for the given name.
  234. * Used by the normal 'if_get_flags' function, as well
  235. * as the bootup interface-list code, which has to peek at per-address
  236. * flags in order to figure out which ones should be ignored..
  237. */
  238. int
  239. if_get_flags_direct (const char *ifname, uint64_t *flags, unsigned int af)
  240. {
  241. struct lifreq lifreq;
  242. int ret;
  243. lifreq_set_name (&lifreq, ifname);
  244. ret = AF_IOCTL (af, SIOCGLIFFLAGS, (caddr_t) &lifreq);
  245. if (ret)
  246. zlog_debug ("%s: ifname %s, error %s (%d)",
  247. __func__, ifname, safe_strerror (errno), errno);
  248. *flags = lifreq.lifr_flags;
  249. return ret;
  250. }
  251. /* get interface flags */
  252. void
  253. if_get_flags (struct interface *ifp)
  254. {
  255. int ret4 = 0, ret6 = 0;
  256. uint64_t newflags = 0;
  257. uint64_t tmpflags;
  258. if (ifp->flags & IFF_IPV4)
  259. {
  260. ret4 = if_get_flags_direct (ifp->name, &tmpflags, AF_INET);
  261. if (!ret4)
  262. newflags |= tmpflags;
  263. else if (errno == ENXIO)
  264. {
  265. /* it's gone */
  266. UNSET_FLAG (ifp->flags, IFF_UP);
  267. if_flags_update (ifp, ifp->flags);
  268. }
  269. }
  270. if (ifp->flags & IFF_IPV6)
  271. {
  272. ret6 = if_get_flags_direct (ifp->name, &tmpflags, AF_INET6);
  273. if (!ret6)
  274. newflags |= tmpflags;
  275. else if (errno == ENXIO)
  276. {
  277. /* it's gone */
  278. UNSET_FLAG (ifp->flags, IFF_UP);
  279. if_flags_update (ifp, ifp->flags);
  280. }
  281. }
  282. /* only update flags if one of above succeeded */
  283. if ( !(ret4 && ret6) )
  284. if_flags_update (ifp, newflags);
  285. }
  286. /* Set interface flags */
  287. int
  288. if_set_flags (struct interface *ifp, uint64_t flags)
  289. {
  290. int ret;
  291. struct lifreq lifreq;
  292. lifreq_set_name (&lifreq, ifp->name);
  293. lifreq.lifr_flags = ifp->flags;
  294. lifreq.lifr_flags |= flags;
  295. if (ifp->flags & IFF_IPV4)
  296. ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq);
  297. else if (ifp->flags & IFF_IPV6)
  298. ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq);
  299. else
  300. ret = -1;
  301. if (ret < 0)
  302. zlog_info ("can't set interface flags on %s: %s", ifp->name,
  303. safe_strerror (errno));
  304. else
  305. ret = 0;
  306. return ret;
  307. }
  308. /* Unset interface's flag. */
  309. int
  310. if_unset_flags (struct interface *ifp, uint64_t flags)
  311. {
  312. int ret;
  313. struct lifreq lifreq;
  314. lifreq_set_name (&lifreq, ifp->name);
  315. lifreq.lifr_flags = ifp->flags;
  316. lifreq.lifr_flags &= ~flags;
  317. if (ifp->flags & IFF_IPV4)
  318. ret = AF_IOCTL (AF_INET, SIOCSLIFFLAGS, (caddr_t) & lifreq);
  319. else if (ifp->flags & IFF_IPV6)
  320. ret = AF_IOCTL (AF_INET6, SIOCSLIFFLAGS, (caddr_t) & lifreq);
  321. else
  322. ret = -1;
  323. if (ret < 0)
  324. zlog_info ("can't unset interface flags");
  325. else
  326. ret = 0;
  327. return ret;
  328. }
  329. #ifdef HAVE_IPV6
  330. /* Interface's address add/delete functions. */
  331. int
  332. if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
  333. {
  334. char addrbuf[PREFIX_STRLEN];
  335. zlog_warn ("Can't set %s on interface %s",
  336. prefix2str(ifc->address, addrbuf, sizeof(addrbuf)),
  337. ifp->name);
  338. return 0;
  339. }
  340. int
  341. if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
  342. {
  343. char addrbuf[PREFIX_STRLEN];
  344. zlog_warn ("Can't delete %s on interface %s",
  345. prefix2str(ifc->address, addrbuf, sizeof(addrbuf)),
  346. ifp->name);
  347. return 0;
  348. }
  349. #endif /* HAVE_IPV6 */