ioctl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * Common ioctl 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 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. ifreq_set_name (struct ifreq *ifreq, struct interface *ifp)
  35. {
  36. strncpy (ifreq->ifr_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. #ifdef HAVE_IPV6
  71. int
  72. if_ioctl_ipv6 (u_long request, caddr_t buffer)
  73. {
  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. return 0;
  101. }
  102. #endif /* HAVE_IPV6 */
  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. #ifdef SIOCGIFMETRIC
  111. struct ifreq ifreq;
  112. ifreq_set_name (&ifreq, ifp);
  113. if (if_ioctl (SIOCGIFMETRIC, (caddr_t) &ifreq) < 0)
  114. return;
  115. ifp->metric = ifreq.ifr_metric;
  116. if (ifp->metric == 0)
  117. ifp->metric = 1;
  118. #else /* SIOCGIFMETRIC */
  119. ifp->metric = -1;
  120. #endif /* SIOCGIFMETRIC */
  121. }
  122. /* get interface MTU */
  123. void
  124. if_get_mtu (struct interface *ifp)
  125. {
  126. struct ifreq ifreq;
  127. ifreq_set_name (&ifreq, ifp);
  128. #if defined(SIOCGIFMTU)
  129. if (if_ioctl (SIOCGIFMTU, (caddr_t) & ifreq) < 0)
  130. {
  131. zlog_info ("Can't lookup mtu by ioctl(SIOCGIFMTU)");
  132. ifp->mtu6 = ifp->mtu = -1;
  133. return;
  134. }
  135. #ifdef SUNOS_5
  136. ifp->mtu6 = ifp->mtu = ifreq.ifr_metric;
  137. #else
  138. ifp->mtu6 = ifp->mtu = ifreq.ifr_mtu;
  139. #endif /* SUNOS_5 */
  140. #else
  141. zlog (NULL, LOG_INFO, "Can't lookup mtu on this system");
  142. ifp->mtu6 = ifp->mtu = -1;
  143. #endif
  144. }
  145. #ifdef HAVE_NETLINK
  146. /* Interface address setting via netlink interface. */
  147. int
  148. if_set_prefix (struct interface *ifp, struct connected *ifc)
  149. {
  150. return kernel_address_add_ipv4 (ifp, ifc);
  151. }
  152. /* Interface address is removed using netlink interface. */
  153. int
  154. if_unset_prefix (struct interface *ifp, struct connected *ifc)
  155. {
  156. return kernel_address_delete_ipv4 (ifp, ifc);
  157. }
  158. #else /* ! HAVE_NETLINK */
  159. #ifdef HAVE_IFALIASREQ
  160. /* Set up interface's IP address, netmask (and broadcas? ). *BSD may
  161. has ifaliasreq structure. */
  162. int
  163. if_set_prefix (struct interface *ifp, struct connected *ifc)
  164. {
  165. int ret;
  166. struct ifaliasreq addreq;
  167. struct sockaddr_in addr;
  168. struct sockaddr_in mask;
  169. struct prefix_ipv4 *p;
  170. p = (struct prefix_ipv4 *) ifc->address;
  171. memset (&addreq, 0, sizeof addreq);
  172. strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
  173. memset (&addr, 0, sizeof (struct sockaddr_in));
  174. addr.sin_addr = p->prefix;
  175. addr.sin_family = p->family;
  176. #ifdef HAVE_SIN_LEN
  177. addr.sin_len = sizeof (struct sockaddr_in);
  178. #endif
  179. memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in));
  180. memset (&mask, 0, sizeof (struct sockaddr_in));
  181. masklen2ip (p->prefixlen, &mask.sin_addr);
  182. mask.sin_family = p->family;
  183. #ifdef HAVE_SIN_LEN
  184. mask.sin_len = sizeof (struct sockaddr_in);
  185. #endif
  186. memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in));
  187. ret = if_ioctl (SIOCAIFADDR, (caddr_t) &addreq);
  188. if (ret < 0)
  189. return ret;
  190. return 0;
  191. }
  192. /* Set up interface's IP address, netmask (and broadcas? ). *BSD may
  193. has ifaliasreq structure. */
  194. int
  195. if_unset_prefix (struct interface *ifp, struct connected *ifc)
  196. {
  197. int ret;
  198. struct ifaliasreq addreq;
  199. struct sockaddr_in addr;
  200. struct sockaddr_in mask;
  201. struct prefix_ipv4 *p;
  202. p = (struct prefix_ipv4 *)ifc->address;
  203. memset (&addreq, 0, sizeof addreq);
  204. strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
  205. memset (&addr, 0, sizeof (struct sockaddr_in));
  206. addr.sin_addr = p->prefix;
  207. addr.sin_family = p->family;
  208. #ifdef HAVE_SIN_LEN
  209. addr.sin_len = sizeof (struct sockaddr_in);
  210. #endif
  211. memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in));
  212. memset (&mask, 0, sizeof (struct sockaddr_in));
  213. masklen2ip (p->prefixlen, &mask.sin_addr);
  214. mask.sin_family = p->family;
  215. #ifdef HAVE_SIN_LEN
  216. mask.sin_len = sizeof (struct sockaddr_in);
  217. #endif
  218. memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in));
  219. ret = if_ioctl (SIOCDIFADDR, (caddr_t) &addreq);
  220. if (ret < 0)
  221. return ret;
  222. return 0;
  223. }
  224. #else
  225. /* Set up interface's address, netmask (and broadcas? ). Linux or
  226. Solaris uses ifname:number semantics to set IP address aliases. */
  227. int
  228. if_set_prefix (struct interface *ifp, struct connected *ifc)
  229. {
  230. int ret;
  231. struct ifreq ifreq;
  232. struct sockaddr_in addr;
  233. struct sockaddr_in broad;
  234. struct sockaddr_in mask;
  235. struct prefix_ipv4 ifaddr;
  236. struct prefix_ipv4 *p;
  237. p = (struct prefix_ipv4 *) ifc->address;
  238. ifaddr = *p;
  239. ifreq_set_name (&ifreq, ifp);
  240. addr.sin_addr = p->prefix;
  241. addr.sin_family = p->family;
  242. memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
  243. ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq);
  244. if (ret < 0)
  245. return ret;
  246. /* We need mask for make broadcast addr. */
  247. masklen2ip (p->prefixlen, &mask.sin_addr);
  248. if (if_is_broadcast (ifp))
  249. {
  250. apply_mask_ipv4 (&ifaddr);
  251. addr.sin_addr = ifaddr.prefix;
  252. broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
  253. broad.sin_family = p->family;
  254. memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
  255. ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) &ifreq);
  256. if (ret < 0)
  257. return ret;
  258. }
  259. mask.sin_family = p->family;
  260. #ifdef SUNOS_5
  261. memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
  262. #else
  263. memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
  264. #endif /* SUNOS5 */
  265. ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) &ifreq);
  266. if (ret < 0)
  267. return ret;
  268. return 0;
  269. }
  270. /* Set up interface's address, netmask (and broadcas? ). Linux or
  271. Solaris uses ifname:number semantics to set IP address aliases. */
  272. int
  273. if_unset_prefix (struct interface *ifp, struct connected *ifc)
  274. {
  275. int ret;
  276. struct ifreq ifreq;
  277. struct sockaddr_in addr;
  278. struct prefix_ipv4 *p;
  279. p = (struct prefix_ipv4 *) ifc->address;
  280. ifreq_set_name (&ifreq, ifp);
  281. memset (&addr, 0, sizeof (struct sockaddr_in));
  282. addr.sin_family = p->family;
  283. memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
  284. ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq);
  285. if (ret < 0)
  286. return ret;
  287. return 0;
  288. }
  289. #endif /* HAVE_IFALIASREQ */
  290. #endif /* HAVE_NETLINK */
  291. /* get interface flags */
  292. void
  293. if_get_flags (struct interface *ifp)
  294. {
  295. int ret;
  296. struct ifreq ifreq;
  297. ifreq_set_name (&ifreq, ifp);
  298. ret = if_ioctl (SIOCGIFFLAGS, (caddr_t) &ifreq);
  299. if (ret < 0)
  300. {
  301. perror ("ioctl");
  302. return;
  303. }
  304. ifp->flags = ifreq.ifr_flags & 0x0000ffff;
  305. }
  306. /* Set interface flags */
  307. int
  308. if_set_flags (struct interface *ifp, unsigned long flags)
  309. {
  310. int ret;
  311. struct ifreq ifreq;
  312. memset (&ifreq, 0, sizeof(struct ifreq));
  313. ifreq_set_name (&ifreq, ifp);
  314. ifreq.ifr_flags = ifp->flags;
  315. ifreq.ifr_flags |= flags;
  316. ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
  317. if (ret < 0)
  318. {
  319. zlog_info ("can't set interface flags");
  320. return ret;
  321. }
  322. return 0;
  323. }
  324. /* Unset interface's flag. */
  325. int
  326. if_unset_flags (struct interface *ifp, unsigned long flags)
  327. {
  328. int ret;
  329. struct ifreq ifreq;
  330. memset (&ifreq, 0, sizeof(struct ifreq));
  331. ifreq_set_name (&ifreq, ifp);
  332. ifreq.ifr_flags = ifp->flags;
  333. ifreq.ifr_flags &= ~flags;
  334. ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
  335. if (ret < 0)
  336. {
  337. zlog_info ("can't unset interface flags");
  338. return ret;
  339. }
  340. return 0;
  341. }
  342. #ifdef HAVE_IPV6
  343. #ifdef LINUX_IPV6
  344. #ifndef _LINUX_IN6_H
  345. /* linux/include/net/ipv6.h */
  346. struct in6_ifreq
  347. {
  348. struct in6_addr ifr6_addr;
  349. u_int32_t ifr6_prefixlen;
  350. int ifr6_ifindex;
  351. };
  352. #endif /* _LINUX_IN6_H */
  353. /* Interface's address add/delete functions. */
  354. int
  355. if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
  356. {
  357. int ret;
  358. struct prefix_ipv6 *p;
  359. struct in6_ifreq ifreq;
  360. p = (struct prefix_ipv6 *) ifc->address;
  361. memset (&ifreq, 0, sizeof (struct in6_ifreq));
  362. memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
  363. ifreq.ifr6_ifindex = ifp->ifindex;
  364. ifreq.ifr6_prefixlen = p->prefixlen;
  365. ret = if_ioctl_ipv6 (SIOCSIFADDR, (caddr_t) &ifreq);
  366. return ret;
  367. }
  368. int
  369. if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
  370. {
  371. int ret;
  372. struct prefix_ipv6 *p;
  373. struct in6_ifreq ifreq;
  374. p = (struct prefix_ipv6 *) ifc->address;
  375. memset (&ifreq, 0, sizeof (struct in6_ifreq));
  376. memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
  377. ifreq.ifr6_ifindex = ifp->ifindex;
  378. ifreq.ifr6_prefixlen = p->prefixlen;
  379. ret = if_ioctl_ipv6 (SIOCDIFADDR, (caddr_t) &ifreq);
  380. return ret;
  381. }
  382. #else /* LINUX_IPV6 */
  383. #ifdef HAVE_IN6_ALIASREQ
  384. #ifndef ND6_INFINITE_LIFETIME
  385. #define ND6_INFINITE_LIFETIME 0xffffffffL
  386. #endif /* ND6_INFINITE_LIFETIME */
  387. int
  388. if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
  389. {
  390. int ret;
  391. struct in6_aliasreq addreq;
  392. struct sockaddr_in6 addr;
  393. struct sockaddr_in6 mask;
  394. struct prefix_ipv6 *p;
  395. p = (struct prefix_ipv6 * ) ifc->address;
  396. memset (&addreq, 0, sizeof addreq);
  397. strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
  398. memset (&addr, 0, sizeof (struct sockaddr_in6));
  399. addr.sin6_addr = p->prefix;
  400. addr.sin6_family = p->family;
  401. #ifdef HAVE_SIN_LEN
  402. addr.sin6_len = sizeof (struct sockaddr_in6);
  403. #endif
  404. memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
  405. memset (&mask, 0, sizeof (struct sockaddr_in6));
  406. masklen2ip6 (p->prefixlen, &mask.sin6_addr);
  407. mask.sin6_family = p->family;
  408. #ifdef HAVE_SIN_LEN
  409. mask.sin6_len = sizeof (struct sockaddr_in6);
  410. #endif
  411. memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
  412. addreq.ifra_lifetime.ia6t_vltime = 0xffffffff;
  413. addreq.ifra_lifetime.ia6t_pltime = 0xffffffff;
  414. #ifdef HAVE_IFRA_LIFETIME
  415. addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
  416. addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
  417. #endif
  418. ret = if_ioctl_ipv6 (SIOCAIFADDR_IN6, (caddr_t) &addreq);
  419. if (ret < 0)
  420. return ret;
  421. return 0;
  422. }
  423. int
  424. if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
  425. {
  426. int ret;
  427. struct in6_aliasreq addreq;
  428. struct sockaddr_in6 addr;
  429. struct sockaddr_in6 mask;
  430. struct prefix_ipv6 *p;
  431. p = (struct prefix_ipv6 *) ifc->address;
  432. memset (&addreq, 0, sizeof addreq);
  433. strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
  434. memset (&addr, 0, sizeof (struct sockaddr_in6));
  435. addr.sin6_addr = p->prefix;
  436. addr.sin6_family = p->family;
  437. #ifdef HAVE_SIN_LEN
  438. addr.sin6_len = sizeof (struct sockaddr_in6);
  439. #endif
  440. memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
  441. memset (&mask, 0, sizeof (struct sockaddr_in6));
  442. masklen2ip6 (p->prefixlen, &mask.sin6_addr);
  443. mask.sin6_family = p->family;
  444. #ifdef HAVE_SIN_LEN
  445. mask.sin6_len = sizeof (struct sockaddr_in6);
  446. #endif
  447. memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
  448. #ifdef HAVE_IFRA_LIFETIME
  449. addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
  450. addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
  451. #endif
  452. ret = if_ioctl_ipv6 (SIOCDIFADDR_IN6, (caddr_t) &addreq);
  453. if (ret < 0)
  454. return ret;
  455. return 0;
  456. }
  457. #else
  458. int
  459. if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
  460. {
  461. return 0;
  462. }
  463. int
  464. if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
  465. {
  466. return 0;
  467. }
  468. #endif /* HAVE_IN6_ALIASREQ */
  469. #endif /* LINUX_IPV6 */
  470. #endif /* HAVE_IPV6 */