ioctl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. #include "zebra/interface.h"
  32. #ifdef HAVE_BSD_LINK_DETECT
  33. #include <net/if_media.h>
  34. #endif /* HAVE_BSD_LINK_DETECT*/
  35. extern struct zebra_privs_t zserv_privs;
  36. /* clear and set interface name string */
  37. void
  38. ifreq_set_name (struct ifreq *ifreq, struct interface *ifp)
  39. {
  40. strncpy (ifreq->ifr_name, ifp->name, IFNAMSIZ);
  41. }
  42. /* call ioctl system call */
  43. int
  44. if_ioctl (u_long request, caddr_t buffer)
  45. {
  46. int sock;
  47. int ret;
  48. int err = 0;
  49. if (zserv_privs.change(ZPRIVS_RAISE))
  50. zlog (NULL, LOG_ERR, "Can't raise privileges");
  51. sock = socket (AF_INET, SOCK_DGRAM, 0);
  52. if (sock < 0)
  53. {
  54. int save_errno = errno;
  55. if (zserv_privs.change(ZPRIVS_LOWER))
  56. zlog (NULL, LOG_ERR, "Can't lower privileges");
  57. zlog_err("Cannot create UDP socket: %s", safe_strerror(save_errno));
  58. exit (1);
  59. }
  60. if ((ret = ioctl (sock, request, buffer)) < 0)
  61. err = errno;
  62. if (zserv_privs.change(ZPRIVS_LOWER))
  63. zlog (NULL, LOG_ERR, "Can't lower privileges");
  64. close (sock);
  65. if (ret < 0)
  66. {
  67. errno = err;
  68. return ret;
  69. }
  70. return 0;
  71. }
  72. #ifdef HAVE_IPV6
  73. static int
  74. if_ioctl_ipv6 (u_long request, caddr_t buffer)
  75. {
  76. int sock;
  77. int ret;
  78. int err = 0;
  79. if (zserv_privs.change(ZPRIVS_RAISE))
  80. zlog (NULL, LOG_ERR, "Can't raise privileges");
  81. sock = socket (AF_INET6, SOCK_DGRAM, 0);
  82. if (sock < 0)
  83. {
  84. int save_errno = errno;
  85. if (zserv_privs.change(ZPRIVS_LOWER))
  86. zlog (NULL, LOG_ERR, "Can't lower privileges");
  87. zlog_err("Cannot create IPv6 datagram socket: %s",
  88. safe_strerror(save_errno));
  89. exit (1);
  90. }
  91. if ((ret = ioctl (sock, request, buffer)) < 0)
  92. err = errno;
  93. if (zserv_privs.change(ZPRIVS_LOWER))
  94. zlog (NULL, LOG_ERR, "Can't lower privileges");
  95. close (sock);
  96. if (ret < 0)
  97. {
  98. errno = err;
  99. return ret;
  100. }
  101. return 0;
  102. }
  103. #endif /* HAVE_IPV6 */
  104. /*
  105. * get interface metric
  106. * -- if value is not avaliable set -1
  107. */
  108. void
  109. if_get_metric (struct interface *ifp)
  110. {
  111. #ifdef SIOCGIFMETRIC
  112. struct ifreq ifreq;
  113. ifreq_set_name (&ifreq, ifp);
  114. if (if_ioctl (SIOCGIFMETRIC, (caddr_t) &ifreq) < 0)
  115. return;
  116. ifp->metric = ifreq.ifr_metric;
  117. if (ifp->metric == 0)
  118. ifp->metric = 1;
  119. #else /* SIOCGIFMETRIC */
  120. ifp->metric = -1;
  121. #endif /* SIOCGIFMETRIC */
  122. }
  123. /* get interface MTU */
  124. void
  125. if_get_mtu (struct interface *ifp)
  126. {
  127. struct ifreq ifreq;
  128. ifreq_set_name (&ifreq, ifp);
  129. #if defined(SIOCGIFMTU)
  130. if (if_ioctl (SIOCGIFMTU, (caddr_t) & ifreq) < 0)
  131. {
  132. zlog_info ("Can't lookup mtu by ioctl(SIOCGIFMTU)");
  133. ifp->mtu6 = ifp->mtu = -1;
  134. return;
  135. }
  136. #ifdef SUNOS_5
  137. ifp->mtu6 = ifp->mtu = ifreq.ifr_metric;
  138. #else
  139. ifp->mtu6 = ifp->mtu = ifreq.ifr_mtu;
  140. #endif /* SUNOS_5 */
  141. /* propogate */
  142. zebra_interface_up_update(ifp);
  143. #else
  144. zlog (NULL, LOG_INFO, "Can't lookup mtu on this system");
  145. ifp->mtu6 = ifp->mtu = -1;
  146. #endif
  147. }
  148. #ifdef HAVE_NETLINK
  149. /* Interface address setting via netlink interface. */
  150. int
  151. if_set_prefix (struct interface *ifp, struct connected *ifc)
  152. {
  153. return kernel_address_add_ipv4 (ifp, ifc);
  154. }
  155. /* Interface address is removed using netlink interface. */
  156. int
  157. if_unset_prefix (struct interface *ifp, struct connected *ifc)
  158. {
  159. return kernel_address_delete_ipv4 (ifp, ifc);
  160. }
  161. #else /* ! HAVE_NETLINK */
  162. #ifdef HAVE_STRUCT_IFALIASREQ
  163. /* Set up interface's IP address, netmask (and broadcas? ). *BSD may
  164. has ifaliasreq structure. */
  165. int
  166. if_set_prefix (struct interface *ifp, struct connected *ifc)
  167. {
  168. int ret;
  169. struct ifaliasreq addreq;
  170. struct sockaddr_in addr;
  171. struct sockaddr_in mask;
  172. struct prefix_ipv4 *p;
  173. p = (struct prefix_ipv4 *) ifc->address;
  174. memset (&addreq, 0, sizeof addreq);
  175. strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
  176. memset (&addr, 0, sizeof (struct sockaddr_in));
  177. addr.sin_addr = p->prefix;
  178. addr.sin_family = p->family;
  179. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  180. addr.sin_len = sizeof (struct sockaddr_in);
  181. #endif
  182. memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in));
  183. memset (&mask, 0, sizeof (struct sockaddr_in));
  184. masklen2ip (p->prefixlen, &mask.sin_addr);
  185. mask.sin_family = p->family;
  186. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  187. mask.sin_len = sizeof (struct sockaddr_in);
  188. #endif
  189. memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in));
  190. ret = if_ioctl (SIOCAIFADDR, (caddr_t) &addreq);
  191. if (ret < 0)
  192. return ret;
  193. return 0;
  194. }
  195. /* Set up interface's IP address, netmask (and broadcas? ). *BSD may
  196. has ifaliasreq structure. */
  197. int
  198. if_unset_prefix (struct interface *ifp, struct connected *ifc)
  199. {
  200. int ret;
  201. struct ifaliasreq addreq;
  202. struct sockaddr_in addr;
  203. struct sockaddr_in mask;
  204. struct prefix_ipv4 *p;
  205. p = (struct prefix_ipv4 *)ifc->address;
  206. memset (&addreq, 0, sizeof addreq);
  207. strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
  208. memset (&addr, 0, sizeof (struct sockaddr_in));
  209. addr.sin_addr = p->prefix;
  210. addr.sin_family = p->family;
  211. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  212. addr.sin_len = sizeof (struct sockaddr_in);
  213. #endif
  214. memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in));
  215. memset (&mask, 0, sizeof (struct sockaddr_in));
  216. masklen2ip (p->prefixlen, &mask.sin_addr);
  217. mask.sin_family = p->family;
  218. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  219. mask.sin_len = sizeof (struct sockaddr_in);
  220. #endif
  221. memcpy (&addreq.ifra_mask, &mask, sizeof (struct sockaddr_in));
  222. ret = if_ioctl (SIOCDIFADDR, (caddr_t) &addreq);
  223. if (ret < 0)
  224. return ret;
  225. return 0;
  226. }
  227. #else
  228. /* Set up interface's address, netmask (and broadcas? ). Linux or
  229. Solaris uses ifname:number semantics to set IP address aliases. */
  230. int
  231. if_set_prefix (struct interface *ifp, struct connected *ifc)
  232. {
  233. int ret;
  234. struct ifreq ifreq;
  235. struct sockaddr_in addr;
  236. struct sockaddr_in broad;
  237. struct sockaddr_in mask;
  238. struct prefix_ipv4 ifaddr;
  239. struct prefix_ipv4 *p;
  240. p = (struct prefix_ipv4 *) ifc->address;
  241. ifaddr = *p;
  242. ifreq_set_name (&ifreq, ifp);
  243. addr.sin_addr = p->prefix;
  244. addr.sin_family = p->family;
  245. memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
  246. ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq);
  247. if (ret < 0)
  248. return ret;
  249. /* We need mask for make broadcast addr. */
  250. masklen2ip (p->prefixlen, &mask.sin_addr);
  251. if (if_is_broadcast (ifp))
  252. {
  253. apply_mask_ipv4 (&ifaddr);
  254. addr.sin_addr = ifaddr.prefix;
  255. broad.sin_addr.s_addr = (addr.sin_addr.s_addr | ~mask.sin_addr.s_addr);
  256. broad.sin_family = p->family;
  257. memcpy (&ifreq.ifr_broadaddr, &broad, sizeof (struct sockaddr_in));
  258. ret = if_ioctl (SIOCSIFBRDADDR, (caddr_t) &ifreq);
  259. if (ret < 0)
  260. return ret;
  261. }
  262. mask.sin_family = p->family;
  263. #ifdef SUNOS_5
  264. memcpy (&mask, &ifreq.ifr_addr, sizeof (mask));
  265. #else
  266. memcpy (&ifreq.ifr_netmask, &mask, sizeof (struct sockaddr_in));
  267. #endif /* SUNOS5 */
  268. ret = if_ioctl (SIOCSIFNETMASK, (caddr_t) &ifreq);
  269. if (ret < 0)
  270. return ret;
  271. return 0;
  272. }
  273. /* Set up interface's address, netmask (and broadcas? ). Linux or
  274. Solaris uses ifname:number semantics to set IP address aliases. */
  275. int
  276. if_unset_prefix (struct interface *ifp, struct connected *ifc)
  277. {
  278. int ret;
  279. struct ifreq ifreq;
  280. struct sockaddr_in addr;
  281. struct prefix_ipv4 *p;
  282. p = (struct prefix_ipv4 *) ifc->address;
  283. ifreq_set_name (&ifreq, ifp);
  284. memset (&addr, 0, sizeof (struct sockaddr_in));
  285. addr.sin_family = p->family;
  286. memcpy (&ifreq.ifr_addr, &addr, sizeof (struct sockaddr_in));
  287. ret = if_ioctl (SIOCSIFADDR, (caddr_t) &ifreq);
  288. if (ret < 0)
  289. return ret;
  290. return 0;
  291. }
  292. #endif /* HAVE_STRUCT_IFALIASREQ */
  293. #endif /* HAVE_NETLINK */
  294. /* get interface flags */
  295. void
  296. if_get_flags (struct interface *ifp)
  297. {
  298. int ret;
  299. struct ifreq ifreq;
  300. #ifdef HAVE_BSD_LINK_DETECT
  301. struct ifmediareq ifmr;
  302. #endif /* HAVE_BSD_LINK_DETECT */
  303. ifreq_set_name (&ifreq, ifp);
  304. ret = if_ioctl (SIOCGIFFLAGS, (caddr_t) &ifreq);
  305. if (ret < 0)
  306. {
  307. zlog_err("if_ioctl(SIOCGIFFLAGS) failed: %s", safe_strerror(errno));
  308. return;
  309. }
  310. #ifdef HAVE_BSD_LINK_DETECT /* Detect BSD link-state at start-up */
  311. /* Per-default, IFF_RUNNING is held high, unless link-detect says
  312. * otherwise - we abuse IFF_RUNNING inside zebra as a link-state flag,
  313. * following practice on Linux and Solaris kernels
  314. */
  315. SET_FLAG(ifreq.ifr_flags, IFF_RUNNING);
  316. if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_LINKDETECTION))
  317. {
  318. (void) memset(&ifmr, 0, sizeof(ifmr));
  319. strncpy (ifmr.ifm_name, ifp->name, IFNAMSIZ);
  320. /* Seems not all interfaces implement this ioctl */
  321. if (if_ioctl(SIOCGIFMEDIA, (caddr_t) &ifmr) < 0)
  322. zlog_err("if_ioctl(SIOCGIFMEDIA) failed: %s", safe_strerror(errno));
  323. else if (ifmr.ifm_status & IFM_AVALID) /* Link state is valid */
  324. {
  325. if (ifmr.ifm_status & IFM_ACTIVE)
  326. SET_FLAG(ifreq.ifr_flags, IFF_RUNNING);
  327. else
  328. UNSET_FLAG(ifreq.ifr_flags, IFF_RUNNING);
  329. }
  330. }
  331. #endif /* HAVE_BSD_LINK_DETECT */
  332. if_flags_update (ifp, (ifreq.ifr_flags & 0x0000ffff));
  333. }
  334. /* Set interface flags */
  335. int
  336. if_set_flags (struct interface *ifp, uint64_t flags)
  337. {
  338. int ret;
  339. struct ifreq ifreq;
  340. memset (&ifreq, 0, sizeof(struct ifreq));
  341. ifreq_set_name (&ifreq, ifp);
  342. ifreq.ifr_flags = ifp->flags;
  343. ifreq.ifr_flags |= flags;
  344. ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
  345. if (ret < 0)
  346. {
  347. zlog_info ("can't set interface flags");
  348. return ret;
  349. }
  350. return 0;
  351. }
  352. /* Unset interface's flag. */
  353. int
  354. if_unset_flags (struct interface *ifp, uint64_t flags)
  355. {
  356. int ret;
  357. struct ifreq ifreq;
  358. memset (&ifreq, 0, sizeof(struct ifreq));
  359. ifreq_set_name (&ifreq, ifp);
  360. ifreq.ifr_flags = ifp->flags;
  361. ifreq.ifr_flags &= ~flags;
  362. ret = if_ioctl (SIOCSIFFLAGS, (caddr_t) &ifreq);
  363. if (ret < 0)
  364. {
  365. zlog_info ("can't unset interface flags");
  366. return ret;
  367. }
  368. return 0;
  369. }
  370. #ifdef HAVE_IPV6
  371. #ifdef LINUX_IPV6
  372. #ifndef _LINUX_IN6_H
  373. /* linux/include/net/ipv6.h */
  374. struct in6_ifreq
  375. {
  376. struct in6_addr ifr6_addr;
  377. u_int32_t ifr6_prefixlen;
  378. int ifr6_ifindex;
  379. };
  380. #endif /* _LINUX_IN6_H */
  381. /* Interface's address add/delete functions. */
  382. int
  383. if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
  384. {
  385. int ret;
  386. struct prefix_ipv6 *p;
  387. struct in6_ifreq ifreq;
  388. p = (struct prefix_ipv6 *) ifc->address;
  389. memset (&ifreq, 0, sizeof (struct in6_ifreq));
  390. memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
  391. ifreq.ifr6_ifindex = ifp->ifindex;
  392. ifreq.ifr6_prefixlen = p->prefixlen;
  393. ret = if_ioctl_ipv6 (SIOCSIFADDR, (caddr_t) &ifreq);
  394. return ret;
  395. }
  396. int
  397. if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
  398. {
  399. int ret;
  400. struct prefix_ipv6 *p;
  401. struct in6_ifreq ifreq;
  402. p = (struct prefix_ipv6 *) ifc->address;
  403. memset (&ifreq, 0, sizeof (struct in6_ifreq));
  404. memcpy (&ifreq.ifr6_addr, &p->prefix, sizeof (struct in6_addr));
  405. ifreq.ifr6_ifindex = ifp->ifindex;
  406. ifreq.ifr6_prefixlen = p->prefixlen;
  407. ret = if_ioctl_ipv6 (SIOCDIFADDR, (caddr_t) &ifreq);
  408. return ret;
  409. }
  410. #else /* LINUX_IPV6 */
  411. #ifdef HAVE_STRUCT_IN6_ALIASREQ
  412. #ifndef ND6_INFINITE_LIFETIME
  413. #define ND6_INFINITE_LIFETIME 0xffffffffL
  414. #endif /* ND6_INFINITE_LIFETIME */
  415. int
  416. if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
  417. {
  418. int ret;
  419. struct in6_aliasreq addreq;
  420. struct sockaddr_in6 addr;
  421. struct sockaddr_in6 mask;
  422. struct prefix_ipv6 *p;
  423. p = (struct prefix_ipv6 * ) ifc->address;
  424. memset (&addreq, 0, sizeof addreq);
  425. strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
  426. memset (&addr, 0, sizeof (struct sockaddr_in6));
  427. addr.sin6_addr = p->prefix;
  428. addr.sin6_family = p->family;
  429. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  430. addr.sin6_len = sizeof (struct sockaddr_in6);
  431. #endif
  432. memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
  433. memset (&mask, 0, sizeof (struct sockaddr_in6));
  434. masklen2ip6 (p->prefixlen, &mask.sin6_addr);
  435. mask.sin6_family = p->family;
  436. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  437. mask.sin6_len = sizeof (struct sockaddr_in6);
  438. #endif
  439. memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
  440. addreq.ifra_lifetime.ia6t_vltime = 0xffffffff;
  441. addreq.ifra_lifetime.ia6t_pltime = 0xffffffff;
  442. #ifdef HAVE_STRUCT_IF6_ALIASREQ_IFRA_LIFETIME
  443. addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
  444. addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
  445. #endif
  446. ret = if_ioctl_ipv6 (SIOCAIFADDR_IN6, (caddr_t) &addreq);
  447. if (ret < 0)
  448. return ret;
  449. return 0;
  450. }
  451. int
  452. if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
  453. {
  454. int ret;
  455. struct in6_aliasreq addreq;
  456. struct sockaddr_in6 addr;
  457. struct sockaddr_in6 mask;
  458. struct prefix_ipv6 *p;
  459. p = (struct prefix_ipv6 *) ifc->address;
  460. memset (&addreq, 0, sizeof addreq);
  461. strncpy ((char *)&addreq.ifra_name, ifp->name, sizeof addreq.ifra_name);
  462. memset (&addr, 0, sizeof (struct sockaddr_in6));
  463. addr.sin6_addr = p->prefix;
  464. addr.sin6_family = p->family;
  465. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  466. addr.sin6_len = sizeof (struct sockaddr_in6);
  467. #endif
  468. memcpy (&addreq.ifra_addr, &addr, sizeof (struct sockaddr_in6));
  469. memset (&mask, 0, sizeof (struct sockaddr_in6));
  470. masklen2ip6 (p->prefixlen, &mask.sin6_addr);
  471. mask.sin6_family = p->family;
  472. #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
  473. mask.sin6_len = sizeof (struct sockaddr_in6);
  474. #endif
  475. memcpy (&addreq.ifra_prefixmask, &mask, sizeof (struct sockaddr_in6));
  476. #ifdef HAVE_STRUCT_IF6_ALIASREQ_IFRA_LIFETIME
  477. addreq.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME;
  478. addreq.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME;
  479. #endif
  480. ret = if_ioctl_ipv6 (SIOCDIFADDR_IN6, (caddr_t) &addreq);
  481. if (ret < 0)
  482. return ret;
  483. return 0;
  484. }
  485. #else
  486. int
  487. if_prefix_add_ipv6 (struct interface *ifp, struct connected *ifc)
  488. {
  489. return 0;
  490. }
  491. int
  492. if_prefix_delete_ipv6 (struct interface *ifp, struct connected *ifc)
  493. {
  494. return 0;
  495. }
  496. #endif /* HAVE_STRUCT_IN6_ALIASREQ */
  497. #endif /* LINUX_IPV6 */
  498. #endif /* HAVE_IPV6 */