device.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. device.c -- Interaction with Solaris tun device
  3. Copyright (C) 2001-2005 Ivo Timmermans,
  4. 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
  5. 2001-2014 Guus Sliepen <guus@tinc-vpn.org>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License along
  15. with this program; if not, write to the Free Software Foundation, Inc.,
  16. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "../system.h"
  19. #include <sys/stropts.h>
  20. #include <sys/sockio.h>
  21. #include "../conf.h"
  22. #include "../device.h"
  23. #include "../logger.h"
  24. #include "../net.h"
  25. #include "../route.h"
  26. #include "../utils.h"
  27. #include "../xalloc.h"
  28. #ifndef TUNNEWPPA
  29. #warning Missing net/if_tun.h, using hardcoded value for TUNNEWPPA
  30. #define TUNNEWPPA (('T'<<16) | 0x0001)
  31. #endif
  32. #define DEFAULT_TUN_DEVICE "/dev/tun"
  33. #define DEFAULT_TAP_DEVICE "/dev/tap"
  34. static enum {
  35. DEVICE_TYPE_TUN,
  36. DEVICE_TYPE_TAP,
  37. } device_type = DEVICE_TYPE_TUN;
  38. int device_fd = -1;
  39. static int if_fd = -1;
  40. static int ip_fd = -1;
  41. char *device = NULL;
  42. char *iface = NULL;
  43. static char *device_info = NULL;
  44. uint64_t device_total_in = 0;
  45. uint64_t device_total_out = 0;
  46. static bool setup_device(void) {
  47. char *type;
  48. if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
  49. if(routing_mode == RMODE_ROUTER)
  50. device = xstrdup(DEFAULT_TUN_DEVICE);
  51. else
  52. device = xstrdup(DEFAULT_TAP_DEVICE);
  53. }
  54. if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
  55. if(!strcasecmp(type, "tun"))
  56. /* use default */;
  57. else if(!strcasecmp(type, "tap"))
  58. device_type = DEVICE_TYPE_TAP;
  59. else {
  60. logger(LOG_ERR, "Unknown device type %s!", type);
  61. return false;
  62. }
  63. } else {
  64. if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
  65. device_type = DEVICE_TYPE_TAP;
  66. }
  67. if(device_type == DEVICE_TYPE_TUN)
  68. device_info = "Solaris tun device";
  69. else
  70. device_info = "Solaris tap device";
  71. /* The following is black magic copied from OpenVPN. */
  72. if((ip_fd = open("/dev/ip", O_RDWR, 0)) < 0) {
  73. logger(LOG_ERR, "Could not open %s: %s\n", "/dev/ip", strerror(errno));
  74. return false;
  75. }
  76. if((device_fd = open(device, O_RDWR, 0)) < 0) {
  77. logger(LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
  78. return false;
  79. }
  80. /* Get unit number. */
  81. char *ptr = device;
  82. get_config_string(lookup_config(config_tree, "Interface"), &ptr);
  83. while(*ptr && !isdigit(*ptr))
  84. ptr++;
  85. int ppa = atoi(ptr);
  86. /* Assign a new PPA and get its unit number. */
  87. struct strioctl strioc_ppa = {
  88. .ic_cmd = TUNNEWPPA,
  89. .ic_len = sizeof ppa,
  90. .ic_dp = (char *)&ppa,
  91. };
  92. if(!*ptr) { /* no number given, try dynamic */
  93. bool found = false;
  94. while(!found && ppa < 64) {
  95. int new_ppa = ioctl(device_fd, I_STR, &strioc_ppa);
  96. if(new_ppa >= 0) {
  97. ppa = new_ppa;
  98. found = true;
  99. break;
  100. }
  101. ppa++;
  102. }
  103. if(!found) {
  104. logger(LOG_ERR, "Could not find free PPA for %s %s!", device_info, device);
  105. return false;
  106. }
  107. } else { /* try this particular one */
  108. if((ppa = ioctl(device_fd, I_STR, &strioc_ppa)) < 0) {
  109. logger(LOG_ERR, "Could not assign PPA %d for %s %s!", ppa, device_info, device);
  110. return false;
  111. }
  112. }
  113. if((if_fd = open(device, O_RDWR, 0)) < 0) {
  114. logger(LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
  115. return false;
  116. }
  117. if(ioctl(if_fd, I_PUSH, "ip") < 0) {
  118. logger(LOG_ERR, "Could not push IP module onto %s %s!", device_info, device);
  119. return false;
  120. }
  121. xasprintf(&iface, "%s%d", device_type == DEVICE_TYPE_TUN ? "tun" : "tap", ppa);
  122. {
  123. /* Remove muxes just in case they are left over from a crashed tincd */
  124. struct lifreq ifr = {};
  125. strncpy(ifr.lifr_name, iface, sizeof ifr.lifr_name);
  126. if(ioctl(ip_fd, SIOCGLIFMUXID, &ifr) >= 0) {
  127. int muxid = ifr.lifr_arp_muxid;
  128. ioctl(ip_fd, I_PUNLINK, muxid);
  129. muxid = ifr.lifr_ip_muxid;
  130. ioctl(ip_fd, I_PUNLINK, muxid);
  131. }
  132. }
  133. if(device_type == DEVICE_TYPE_TUN) {
  134. /* Assign ppa according to the unit number returned by tun device */
  135. if(ioctl(if_fd, IF_UNITSEL, (char *)&ppa) < 0) {
  136. logger(LOG_ERR, "Could not set PPA %d on %s %s!", ppa, device_info, device);
  137. return false;
  138. }
  139. }
  140. int arp_fd = -1;
  141. if(device_type == DEVICE_TYPE_TAP) {
  142. struct lifreq ifr = {};
  143. if(ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) {
  144. logger(LOG_ERR, "Could not set flags on %s %s!", device_info, device);
  145. return false;
  146. }
  147. strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
  148. ifr.lifr_ppa = ppa;
  149. /* Assign ppa according to the unit number returned by tun device */
  150. if(ioctl(if_fd, SIOCSLIFNAME, &ifr) < 0) {
  151. logger(LOG_ERR, "Could not set PPA %d on %s %s!", ppa, device_info, device);
  152. return false;
  153. }
  154. if(ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) {
  155. logger(LOG_ERR, "Could not set flags on %s %s!", device_info, device);
  156. return false;
  157. }
  158. /* Push arp module to if_fd */
  159. if(ioctl(if_fd, I_PUSH, "arp") < 0) {
  160. logger(LOG_ERR, "Could not push ARP module onto %s %s!", device_info, device);
  161. return false;
  162. }
  163. /* Pop any modules on the stream */
  164. while(true) {
  165. if(ioctl(ip_fd, I_POP, NULL) < 0)
  166. break;
  167. }
  168. /* Push arp module to ip_fd */
  169. if(ioctl(ip_fd, I_PUSH, "arp") < 0) {
  170. logger(LOG_ERR, "Could not push ARP module onto %s!", "/dev/ip");
  171. return false;
  172. }
  173. /* Open arp_fd */
  174. if((arp_fd = open(device, O_RDWR, 0)) < 0) {
  175. logger(LOG_ERR, "Could not open %s: %s\n", device, strerror(errno));
  176. return false;
  177. }
  178. /* Push arp module to arp_fd */
  179. if(ioctl(arp_fd, I_PUSH, "arp") < 0) {
  180. logger(LOG_ERR, "Could not push ARP module onto %s %s!", device_info, device);
  181. return false;
  182. }
  183. /* Set ifname to arp */
  184. struct strioctl strioc_if = {
  185. .ic_cmd = SIOCSLIFNAME,
  186. .ic_len = sizeof ifr,
  187. .ic_dp = (char *)&ifr,
  188. };
  189. if(ioctl(arp_fd, I_STR, &strioc_if) < 0) {
  190. logger(LOG_ERR, "Could not set ifname to %s %s", device_info, device);
  191. return false;
  192. }
  193. }
  194. int ip_muxid, arp_muxid;
  195. if((ip_muxid = ioctl(ip_fd, I_PLINK, if_fd)) < 0) {
  196. logger(LOG_ERR, "Could not link %s %s to IP", device_info, device);
  197. return false;
  198. }
  199. if(device_type == DEVICE_TYPE_TAP) {
  200. if((arp_muxid = ioctl(ip_fd, I_PLINK, arp_fd)) < 0) {
  201. logger(LOG_ERR, "Could not link %s %s to ARP", device_info, device);
  202. return false;
  203. }
  204. close(arp_fd);
  205. }
  206. struct lifreq ifr = {};
  207. strncpy(ifr.lifr_name, iface, sizeof(ifr.lifr_name));
  208. ifr.lifr_ip_muxid = ip_muxid;
  209. if(device_type == DEVICE_TYPE_TAP) {
  210. ifr.lifr_arp_muxid = arp_muxid;
  211. }
  212. if(ioctl(ip_fd, SIOCSLIFMUXID, &ifr) < 0) {
  213. if(device_type == DEVICE_TYPE_TAP) {
  214. ioctl(ip_fd, I_PUNLINK, arp_muxid);
  215. }
  216. ioctl(ip_fd, I_PUNLINK, ip_muxid);
  217. logger(LOG_ERR, "Could not set multiplexor id for %s %s", device_info, device);
  218. return false;
  219. }
  220. close(if_fd);
  221. #ifdef FD_CLOEXEC
  222. fcntl(device_fd, F_SETFD, FD_CLOEXEC);
  223. fcntl(ip_fd, F_SETFD, FD_CLOEXEC);
  224. #endif
  225. logger(LOG_INFO, "%s is a %s", device, device_info);
  226. return true;
  227. }
  228. static void close_device(void) {
  229. if(iface) {
  230. struct lifreq ifr = {};
  231. strncpy(ifr.lifr_name, iface, sizeof ifr.lifr_name);
  232. if(ioctl(ip_fd, SIOCGLIFMUXID, &ifr) >= 0) {
  233. int muxid = ifr.lifr_arp_muxid;
  234. ioctl(ip_fd, I_PUNLINK, muxid);
  235. muxid = ifr.lifr_ip_muxid;
  236. ioctl(ip_fd, I_PUNLINK, muxid);
  237. }
  238. }
  239. close(ip_fd);
  240. close(device_fd);
  241. free(device);
  242. free(iface);
  243. }
  244. static bool read_packet(vpn_packet_t *packet) {
  245. int inlen;
  246. switch(device_type) {
  247. case DEVICE_TYPE_TUN:
  248. if((inlen = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
  249. logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
  250. return false;
  251. }
  252. switch(packet->data[14] >> 4) {
  253. case 4:
  254. packet->data[12] = 0x08;
  255. packet->data[13] = 0x00;
  256. break;
  257. case 6:
  258. packet->data[12] = 0x86;
  259. packet->data[13] = 0xDD;
  260. break;
  261. default:
  262. ifdebug(TRAFFIC) logger(LOG_ERR, "Unknown IP version %d while reading packet from %s %s", packet->data[14] >> 4, device_info, device);
  263. return false;
  264. }
  265. memset(packet->data, 0, 12);
  266. packet->len = inlen + 14;
  267. break;
  268. case DEVICE_TYPE_TAP:
  269. if((inlen = read(device_fd, packet->data, MTU)) <= 0) {
  270. logger(LOG_ERR, "Error while reading from %s %s: %s", device_info, device, strerror(errno));
  271. return false;
  272. }
  273. packet->len = inlen + 14;
  274. break;
  275. default:
  276. abort();
  277. }
  278. device_total_in += packet->len;
  279. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len, device_info);
  280. return true;
  281. }
  282. static bool write_packet(vpn_packet_t *packet) {
  283. ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s", packet->len, device_info);
  284. switch(device_type) {
  285. case DEVICE_TYPE_TUN:
  286. if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
  287. logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
  288. return false;
  289. }
  290. break;
  291. case DEVICE_TYPE_TAP:
  292. if(write(device_fd, packet->data, packet->len) < 0) {
  293. logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
  294. return false;
  295. }
  296. break;
  297. default:
  298. abort();
  299. }
  300. device_total_out += packet->len;
  301. return true;
  302. }
  303. static void dump_device_stats(void) {
  304. logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
  305. logger(LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in);
  306. logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
  307. }
  308. const devops_t os_devops = {
  309. .setup = setup_device,
  310. .close = close_device,
  311. .read = read_packet,
  312. .write = write_packet,
  313. .dump_stats = dump_device_stats,
  314. };