device.c 10 KB

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