port-net.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright (c) 2005 Reyk Floeter <reyk@openbsd.org>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "includes.h"
  17. #include <sys/types.h>
  18. #include <sys/ioctl.h>
  19. #include <netinet/in.h>
  20. #include <arpa/inet.h>
  21. #include <netinet/ip.h>
  22. #include <errno.h>
  23. #include <fcntl.h>
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include "openbsd-compat/sys-queue.h"
  29. #include "log.h"
  30. #include "misc.h"
  31. #include "sshbuf.h"
  32. #include "channels.h"
  33. #include "ssherr.h"
  34. /*
  35. * This file contains various portability code for network support,
  36. * including tun/tap forwarding and routing domains.
  37. */
  38. #if defined(SYS_RDOMAIN_LINUX) || defined(SSH_TUN_LINUX)
  39. #include <linux/if.h>
  40. #endif
  41. #if defined(SYS_RDOMAIN_LINUX)
  42. char *
  43. sys_get_rdomain(int fd)
  44. {
  45. char dev[IFNAMSIZ + 1];
  46. socklen_t len = sizeof(dev) - 1;
  47. if (getsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, dev, &len) == -1) {
  48. error("%s: cannot determine VRF for fd=%d : %s",
  49. __func__, fd, strerror(errno));
  50. return NULL;
  51. }
  52. dev[len] = '\0';
  53. return strdup(dev);
  54. }
  55. int
  56. sys_set_rdomain(int fd, const char *name)
  57. {
  58. if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
  59. name, strlen(name)) == -1) {
  60. error("%s: setsockopt(%d, SO_BINDTODEVICE, %s): %s",
  61. __func__, fd, name, strerror(errno));
  62. return -1;
  63. }
  64. return 0;
  65. }
  66. int
  67. sys_valid_rdomain(const char *name)
  68. {
  69. int fd;
  70. /*
  71. * This is a pretty crappy way to test. It would be better to
  72. * check whether "name" represents a VRF device, but apparently
  73. * that requires an rtnetlink transaction.
  74. */
  75. if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  76. return 0;
  77. if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
  78. name, strlen(name)) == -1) {
  79. close(fd);
  80. return 0;
  81. }
  82. close(fd);
  83. return 1;
  84. }
  85. #elif defined(SYS_RDOMAIN_XXX)
  86. /* XXX examples */
  87. char *
  88. sys_get_rdomain(int fd)
  89. {
  90. return NULL;
  91. }
  92. int
  93. sys_set_rdomain(int fd, const char *name)
  94. {
  95. return -1;
  96. }
  97. int
  98. valid_rdomain(const char *name)
  99. {
  100. return 0;
  101. }
  102. void
  103. sys_set_process_rdomain(const char *name)
  104. {
  105. fatal("%s: not supported", __func__);
  106. }
  107. #endif /* defined(SYS_RDOMAIN_XXX) */
  108. /*
  109. * This is the portable version of the SSH tunnel forwarding, it
  110. * uses some preprocessor definitions for various platform-specific
  111. * settings.
  112. *
  113. * SSH_TUN_LINUX Use the (newer) Linux tun/tap device
  114. * SSH_TUN_FREEBSD Use the FreeBSD tun/tap device
  115. * SSH_TUN_COMPAT_AF Translate the OpenBSD address family
  116. * SSH_TUN_PREPEND_AF Prepend/remove the address family
  117. */
  118. /*
  119. * System-specific tunnel open function
  120. */
  121. #if defined(SSH_TUN_LINUX)
  122. #include <linux/if_tun.h>
  123. #define TUN_CTRL_DEV "/dev/net/tun"
  124. int
  125. sys_tun_open(int tun, int mode, char **ifname)
  126. {
  127. struct ifreq ifr;
  128. int fd = -1;
  129. const char *name = NULL;
  130. if (ifname != NULL)
  131. *ifname = NULL;
  132. if ((fd = open(TUN_CTRL_DEV, O_RDWR)) == -1) {
  133. debug("%s: failed to open tunnel control device \"%s\": %s",
  134. __func__, TUN_CTRL_DEV, strerror(errno));
  135. return (-1);
  136. }
  137. bzero(&ifr, sizeof(ifr));
  138. if (mode == SSH_TUNMODE_ETHERNET) {
  139. ifr.ifr_flags = IFF_TAP;
  140. name = "tap%d";
  141. } else {
  142. ifr.ifr_flags = IFF_TUN;
  143. name = "tun%d";
  144. }
  145. ifr.ifr_flags |= IFF_NO_PI;
  146. if (tun != SSH_TUNID_ANY) {
  147. if (tun > SSH_TUNID_MAX) {
  148. debug("%s: invalid tunnel id %x: %s", __func__,
  149. tun, strerror(errno));
  150. goto failed;
  151. }
  152. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), name, tun);
  153. }
  154. if (ioctl(fd, TUNSETIFF, &ifr) == -1) {
  155. debug("%s: failed to configure tunnel (mode %d): %s", __func__,
  156. mode, strerror(errno));
  157. goto failed;
  158. }
  159. if (tun == SSH_TUNID_ANY)
  160. debug("%s: tunnel mode %d fd %d", __func__, mode, fd);
  161. else
  162. debug("%s: %s mode %d fd %d", __func__, ifr.ifr_name, mode, fd);
  163. if (ifname != NULL && (*ifname = strdup(ifr.ifr_name)) == NULL)
  164. goto failed;
  165. return (fd);
  166. failed:
  167. close(fd);
  168. return (-1);
  169. }
  170. #endif /* SSH_TUN_LINUX */
  171. #ifdef SSH_TUN_FREEBSD
  172. #include <sys/socket.h>
  173. #include <net/if.h>
  174. #ifdef HAVE_NET_IF_TUN_H
  175. #include <net/if_tun.h>
  176. #endif
  177. int
  178. sys_tun_open(int tun, int mode, char **ifname)
  179. {
  180. struct ifreq ifr;
  181. char name[100];
  182. int fd = -1, sock;
  183. const char *tunbase = "tun";
  184. #if defined(TUNSIFHEAD) && !defined(SSH_TUN_PREPEND_AF)
  185. int flag;
  186. #endif
  187. if (ifname != NULL)
  188. *ifname = NULL;
  189. if (mode == SSH_TUNMODE_ETHERNET) {
  190. #ifdef SSH_TUN_NO_L2
  191. debug("%s: no layer 2 tunnelling support", __func__);
  192. return (-1);
  193. #else
  194. tunbase = "tap";
  195. #endif
  196. }
  197. /* Open the tunnel device */
  198. if (tun <= SSH_TUNID_MAX) {
  199. snprintf(name, sizeof(name), "/dev/%s%d", tunbase, tun);
  200. fd = open(name, O_RDWR);
  201. } else if (tun == SSH_TUNID_ANY) {
  202. for (tun = 100; tun >= 0; tun--) {
  203. snprintf(name, sizeof(name), "/dev/%s%d",
  204. tunbase, tun);
  205. if ((fd = open(name, O_RDWR)) >= 0)
  206. break;
  207. }
  208. } else {
  209. debug("%s: invalid tunnel %u\n", __func__, tun);
  210. return (-1);
  211. }
  212. if (fd < 0) {
  213. debug("%s: %s open failed: %s", __func__, name,
  214. strerror(errno));
  215. return (-1);
  216. }
  217. /* Turn on tunnel headers */
  218. #if defined(TUNSIFHEAD) && !defined(SSH_TUN_PREPEND_AF)
  219. flag = 1;
  220. if (mode != SSH_TUNMODE_ETHERNET &&
  221. ioctl(fd, TUNSIFHEAD, &flag) == -1) {
  222. debug("%s: ioctl(%d, TUNSIFHEAD, 1): %s", __func__, fd,
  223. strerror(errno));
  224. close(fd);
  225. }
  226. #endif
  227. debug("%s: %s mode %d fd %d", __func__, name, mode, fd);
  228. /* Set the tunnel device operation mode */
  229. snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", tunbase, tun);
  230. if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1)
  231. goto failed;
  232. if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1)
  233. goto failed;
  234. if ((ifr.ifr_flags & IFF_UP) == 0) {
  235. ifr.ifr_flags |= IFF_UP;
  236. if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1)
  237. goto failed;
  238. }
  239. if (ifname != NULL && (*ifname = strdup(ifr.ifr_name)) == NULL)
  240. goto failed;
  241. close(sock);
  242. return (fd);
  243. failed:
  244. if (fd >= 0)
  245. close(fd);
  246. if (sock >= 0)
  247. close(sock);
  248. debug("%s: failed to set %s mode %d: %s", __func__, name,
  249. mode, strerror(errno));
  250. return (-1);
  251. }
  252. #endif /* SSH_TUN_FREEBSD */
  253. /*
  254. * System-specific channel filters
  255. */
  256. #if defined(SSH_TUN_FILTER)
  257. /*
  258. * The tunnel forwarding protocol prepends the address family of forwarded
  259. * IP packets using OpenBSD's numbers.
  260. */
  261. #define OPENBSD_AF_INET 2
  262. #define OPENBSD_AF_INET6 24
  263. int
  264. sys_tun_infilter(struct ssh *ssh, struct Channel *c, char *buf, int _len)
  265. {
  266. int r;
  267. size_t len;
  268. char *ptr = buf;
  269. #if defined(SSH_TUN_PREPEND_AF)
  270. char rbuf[CHAN_RBUF];
  271. struct ip iph;
  272. #endif
  273. #if defined(SSH_TUN_PREPEND_AF) || defined(SSH_TUN_COMPAT_AF)
  274. u_int32_t af;
  275. #endif
  276. /* XXX update channel input filter API to use unsigned length */
  277. if (_len < 0)
  278. return -1;
  279. len = _len;
  280. #if defined(SSH_TUN_PREPEND_AF)
  281. if (len <= sizeof(iph) || len > sizeof(rbuf) - 4)
  282. return -1;
  283. /* Determine address family from packet IP header. */
  284. memcpy(&iph, buf, sizeof(iph));
  285. af = iph.ip_v == 6 ? OPENBSD_AF_INET6 : OPENBSD_AF_INET;
  286. /* Prepend address family to packet using OpenBSD constants */
  287. memcpy(rbuf + 4, buf, len);
  288. len += 4;
  289. POKE_U32(rbuf, af);
  290. ptr = rbuf;
  291. #elif defined(SSH_TUN_COMPAT_AF)
  292. /* Convert existing address family header to OpenBSD value */
  293. if (len <= 4)
  294. return -1;
  295. af = PEEK_U32(buf);
  296. /* Put it back */
  297. POKE_U32(buf, af == AF_INET6 ? OPENBSD_AF_INET6 : OPENBSD_AF_INET);
  298. #endif
  299. if ((r = sshbuf_put_string(c->input, ptr, len)) != 0)
  300. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  301. return (0);
  302. }
  303. u_char *
  304. sys_tun_outfilter(struct ssh *ssh, struct Channel *c,
  305. u_char **data, size_t *dlen)
  306. {
  307. u_char *buf;
  308. u_int32_t af;
  309. int r;
  310. /* XXX new API is incompatible with this signature. */
  311. if ((r = sshbuf_get_string(c->output, data, dlen)) != 0)
  312. fatal("%s: buffer error: %s", __func__, ssh_err(r));
  313. if (*dlen < sizeof(af))
  314. return (NULL);
  315. buf = *data;
  316. #if defined(SSH_TUN_PREPEND_AF)
  317. /* skip address family */
  318. *dlen -= sizeof(af);
  319. buf = *data + sizeof(af);
  320. #elif defined(SSH_TUN_COMPAT_AF)
  321. /* translate address family */
  322. af = (PEEK_U32(buf) == OPENBSD_AF_INET6) ? AF_INET6 : AF_INET;
  323. POKE_U32(buf, af);
  324. #endif
  325. return (buf);
  326. }
  327. #endif /* SSH_TUN_FILTER */