tun.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  1. /*
  2. * TUN - Universal TUN/TAP device driver.
  3. * Copyright (C) 1999-2002 Maxim Krasnyansky <maxk@qualcomm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  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. *
  15. * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
  16. */
  17. /*
  18. * Changes:
  19. *
  20. * Mike Kershaw <dragorn@kismetwireless.net> 2005/08/14
  21. * Add TUNSETLINK ioctl to set the link encapsulation
  22. *
  23. * Mark Smith <markzzzsmith@yahoo.com.au>
  24. * Use random_ether_addr() for tap MAC address.
  25. *
  26. * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
  27. * Fixes in packet dropping, queue length setting and queue wakeup.
  28. * Increased default tx queue length.
  29. * Added ethtool API.
  30. * Minor cleanups
  31. *
  32. * Daniel Podlejski <underley@underley.eu.org>
  33. * Modifications for 2.3.99-pre5 kernel.
  34. */
  35. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  36. #define DRV_NAME "tun"
  37. #define DRV_VERSION "1.6"
  38. #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
  39. #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
  40. #include <linux/module.h>
  41. #include <linux/errno.h>
  42. #include <linux/kernel.h>
  43. #include <linux/major.h>
  44. #include <linux/slab.h>
  45. #include <linux/poll.h>
  46. #include <linux/fcntl.h>
  47. #include <linux/init.h>
  48. #include <linux/skbuff.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/etherdevice.h>
  51. #include <linux/miscdevice.h>
  52. #include <linux/ethtool.h>
  53. #include <linux/rtnetlink.h>
  54. #include <linux/compat.h>
  55. #include <linux/if.h>
  56. #include <linux/if_arp.h>
  57. #include <linux/if_ether.h>
  58. #include <linux/if_tun.h>
  59. #include <linux/crc32.h>
  60. #include <linux/nsproxy.h>
  61. #include <linux/virtio_net.h>
  62. #include <linux/rcupdate.h>
  63. #include <net/net_namespace.h>
  64. #include <net/netns/generic.h>
  65. #include <net/rtnetlink.h>
  66. #include <net/sock.h>
  67. #include <asm/system.h>
  68. #include <asm/uaccess.h>
  69. /* Uncomment to enable debugging */
  70. /* #define TUN_DEBUG 1 */
  71. #ifdef TUN_DEBUG
  72. static int debug;
  73. #define tun_debug(level, tun, fmt, args...) \
  74. do { \
  75. if (tun->debug) \
  76. netdev_printk(level, tun->dev, fmt, ##args); \
  77. } while (0)
  78. #define DBG1(level, fmt, args...) \
  79. do { \
  80. if (debug == 2) \
  81. printk(level fmt, ##args); \
  82. } while (0)
  83. #else
  84. #define tun_debug(level, tun, fmt, args...) \
  85. do { \
  86. if (0) \
  87. netdev_printk(level, tun->dev, fmt, ##args); \
  88. } while (0)
  89. #define DBG1(level, fmt, args...) \
  90. do { \
  91. if (0) \
  92. printk(level fmt, ##args); \
  93. } while (0)
  94. #endif
  95. #define FLT_EXACT_COUNT 8
  96. struct tap_filter {
  97. unsigned int count; /* Number of addrs. Zero means disabled */
  98. u32 mask[2]; /* Mask of the hashed addrs */
  99. unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
  100. };
  101. struct tun_file {
  102. atomic_t count;
  103. struct tun_struct *tun;
  104. struct net *net;
  105. };
  106. struct tun_sock;
  107. struct tun_struct {
  108. struct tun_file *tfile;
  109. unsigned int flags;
  110. uid_t owner;
  111. gid_t group;
  112. struct net_device *dev;
  113. u32 set_features;
  114. #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
  115. NETIF_F_TSO6|NETIF_F_UFO)
  116. struct fasync_struct *fasync;
  117. struct tap_filter txflt;
  118. struct socket socket;
  119. struct socket_wq wq;
  120. int vnet_hdr_sz;
  121. #ifdef TUN_DEBUG
  122. int debug;
  123. #endif
  124. };
  125. struct tun_sock {
  126. struct sock sk;
  127. struct tun_struct *tun;
  128. };
  129. static inline struct tun_sock *tun_sk(struct sock *sk)
  130. {
  131. return container_of(sk, struct tun_sock, sk);
  132. }
  133. static int tun_attach(struct tun_struct *tun, struct file *file)
  134. {
  135. struct tun_file *tfile = file->private_data;
  136. int err;
  137. ASSERT_RTNL();
  138. netif_tx_lock_bh(tun->dev);
  139. err = -EINVAL;
  140. if (tfile->tun)
  141. goto out;
  142. err = -EBUSY;
  143. if (tun->tfile)
  144. goto out;
  145. err = 0;
  146. tfile->tun = tun;
  147. tun->tfile = tfile;
  148. tun->socket.file = file;
  149. netif_carrier_on(tun->dev);
  150. dev_hold(tun->dev);
  151. sock_hold(tun->socket.sk);
  152. atomic_inc(&tfile->count);
  153. out:
  154. netif_tx_unlock_bh(tun->dev);
  155. return err;
  156. }
  157. static void __tun_detach(struct tun_struct *tun)
  158. {
  159. /* Detach from net device */
  160. netif_tx_lock_bh(tun->dev);
  161. netif_carrier_off(tun->dev);
  162. tun->tfile = NULL;
  163. tun->socket.file = NULL;
  164. netif_tx_unlock_bh(tun->dev);
  165. /* Drop read queue */
  166. skb_queue_purge(&tun->socket.sk->sk_receive_queue);
  167. /* Drop the extra count on the net device */
  168. dev_put(tun->dev);
  169. }
  170. static void tun_detach(struct tun_struct *tun)
  171. {
  172. rtnl_lock();
  173. __tun_detach(tun);
  174. rtnl_unlock();
  175. }
  176. static struct tun_struct *__tun_get(struct tun_file *tfile)
  177. {
  178. struct tun_struct *tun = NULL;
  179. if (atomic_inc_not_zero(&tfile->count))
  180. tun = tfile->tun;
  181. return tun;
  182. }
  183. static struct tun_struct *tun_get(struct file *file)
  184. {
  185. return __tun_get(file->private_data);
  186. }
  187. static void tun_put(struct tun_struct *tun)
  188. {
  189. struct tun_file *tfile = tun->tfile;
  190. if (atomic_dec_and_test(&tfile->count))
  191. tun_detach(tfile->tun);
  192. }
  193. /* TAP filtering */
  194. static void addr_hash_set(u32 *mask, const u8 *addr)
  195. {
  196. int n = ether_crc(ETH_ALEN, addr) >> 26;
  197. mask[n >> 5] |= (1 << (n & 31));
  198. }
  199. static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
  200. {
  201. int n = ether_crc(ETH_ALEN, addr) >> 26;
  202. return mask[n >> 5] & (1 << (n & 31));
  203. }
  204. static int update_filter(struct tap_filter *filter, void __user *arg)
  205. {
  206. struct { u8 u[ETH_ALEN]; } *addr;
  207. struct tun_filter uf;
  208. int err, alen, n, nexact;
  209. if (copy_from_user(&uf, arg, sizeof(uf)))
  210. return -EFAULT;
  211. if (!uf.count) {
  212. /* Disabled */
  213. filter->count = 0;
  214. return 0;
  215. }
  216. alen = ETH_ALEN * uf.count;
  217. addr = kmalloc(alen, GFP_KERNEL);
  218. if (!addr)
  219. return -ENOMEM;
  220. if (copy_from_user(addr, arg + sizeof(uf), alen)) {
  221. err = -EFAULT;
  222. goto done;
  223. }
  224. /* The filter is updated without holding any locks. Which is
  225. * perfectly safe. We disable it first and in the worst
  226. * case we'll accept a few undesired packets. */
  227. filter->count = 0;
  228. wmb();
  229. /* Use first set of addresses as an exact filter */
  230. for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
  231. memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
  232. nexact = n;
  233. /* Remaining multicast addresses are hashed,
  234. * unicast will leave the filter disabled. */
  235. memset(filter->mask, 0, sizeof(filter->mask));
  236. for (; n < uf.count; n++) {
  237. if (!is_multicast_ether_addr(addr[n].u)) {
  238. err = 0; /* no filter */
  239. goto done;
  240. }
  241. addr_hash_set(filter->mask, addr[n].u);
  242. }
  243. /* For ALLMULTI just set the mask to all ones.
  244. * This overrides the mask populated above. */
  245. if ((uf.flags & TUN_FLT_ALLMULTI))
  246. memset(filter->mask, ~0, sizeof(filter->mask));
  247. /* Now enable the filter */
  248. wmb();
  249. filter->count = nexact;
  250. /* Return the number of exact filters */
  251. err = nexact;
  252. done:
  253. kfree(addr);
  254. return err;
  255. }
  256. /* Returns: 0 - drop, !=0 - accept */
  257. static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
  258. {
  259. /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
  260. * at this point. */
  261. struct ethhdr *eh = (struct ethhdr *) skb->data;
  262. int i;
  263. /* Exact match */
  264. for (i = 0; i < filter->count; i++)
  265. if (!compare_ether_addr(eh->h_dest, filter->addr[i]))
  266. return 1;
  267. /* Inexact match (multicast only) */
  268. if (is_multicast_ether_addr(eh->h_dest))
  269. return addr_hash_test(filter->mask, eh->h_dest);
  270. return 0;
  271. }
  272. /*
  273. * Checks whether the packet is accepted or not.
  274. * Returns: 0 - drop, !=0 - accept
  275. */
  276. static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
  277. {
  278. if (!filter->count)
  279. return 1;
  280. return run_filter(filter, skb);
  281. }
  282. /* Network device part of the driver */
  283. static const struct ethtool_ops tun_ethtool_ops;
  284. /* Net device detach from fd. */
  285. static void tun_net_uninit(struct net_device *dev)
  286. {
  287. struct tun_struct *tun = netdev_priv(dev);
  288. struct tun_file *tfile = tun->tfile;
  289. /* Inform the methods they need to stop using the dev.
  290. */
  291. if (tfile) {
  292. wake_up_all(&tun->wq.wait);
  293. if (atomic_dec_and_test(&tfile->count))
  294. __tun_detach(tun);
  295. }
  296. }
  297. static void tun_free_netdev(struct net_device *dev)
  298. {
  299. struct tun_struct *tun = netdev_priv(dev);
  300. sock_put(tun->socket.sk);
  301. }
  302. /* Net device open. */
  303. static int tun_net_open(struct net_device *dev)
  304. {
  305. netif_start_queue(dev);
  306. return 0;
  307. }
  308. /* Net device close. */
  309. static int tun_net_close(struct net_device *dev)
  310. {
  311. netif_stop_queue(dev);
  312. return 0;
  313. }
  314. /* Net device start xmit */
  315. static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
  316. {
  317. struct tun_struct *tun = netdev_priv(dev);
  318. tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
  319. /* Drop packet if interface is not attached */
  320. if (!tun->tfile)
  321. goto drop;
  322. /* Drop if the filter does not like it.
  323. * This is a noop if the filter is disabled.
  324. * Filter can be enabled only for the TAP devices. */
  325. if (!check_filter(&tun->txflt, skb))
  326. goto drop;
  327. if (tun->socket.sk->sk_filter &&
  328. sk_filter(tun->socket.sk, skb))
  329. goto drop;
  330. if (skb_queue_len(&tun->socket.sk->sk_receive_queue) >= dev->tx_queue_len) {
  331. if (!(tun->flags & TUN_ONE_QUEUE)) {
  332. /* Normal queueing mode. */
  333. /* Packet scheduler handles dropping of further packets. */
  334. netif_stop_queue(dev);
  335. /* We won't see all dropped packets individually, so overrun
  336. * error is more appropriate. */
  337. dev->stats.tx_fifo_errors++;
  338. } else {
  339. /* Single queue mode.
  340. * Driver handles dropping of all packets itself. */
  341. goto drop;
  342. }
  343. }
  344. /* Orphan the skb - required as we might hang on to it
  345. * for indefinite time. */
  346. skb_orphan(skb);
  347. /* Enqueue packet */
  348. skb_queue_tail(&tun->socket.sk->sk_receive_queue, skb);
  349. /* Notify and wake up reader process */
  350. if (tun->flags & TUN_FASYNC)
  351. kill_fasync(&tun->fasync, SIGIO, POLL_IN);
  352. wake_up_interruptible_poll(&tun->wq.wait, POLLIN |
  353. POLLRDNORM | POLLRDBAND);
  354. return NETDEV_TX_OK;
  355. drop:
  356. dev->stats.tx_dropped++;
  357. kfree_skb(skb);
  358. return NETDEV_TX_OK;
  359. }
  360. static void tun_net_mclist(struct net_device *dev)
  361. {
  362. /*
  363. * This callback is supposed to deal with mc filter in
  364. * _rx_ path and has nothing to do with the _tx_ path.
  365. * In rx path we always accept everything userspace gives us.
  366. */
  367. }
  368. #define MIN_MTU 68
  369. #define MAX_MTU 65535
  370. static int
  371. tun_net_change_mtu(struct net_device *dev, int new_mtu)
  372. {
  373. if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
  374. return -EINVAL;
  375. dev->mtu = new_mtu;
  376. return 0;
  377. }
  378. static u32 tun_net_fix_features(struct net_device *dev, u32 features)
  379. {
  380. struct tun_struct *tun = netdev_priv(dev);
  381. return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
  382. }
  383. static const struct net_device_ops tun_netdev_ops = {
  384. .ndo_uninit = tun_net_uninit,
  385. .ndo_open = tun_net_open,
  386. .ndo_stop = tun_net_close,
  387. .ndo_start_xmit = tun_net_xmit,
  388. .ndo_change_mtu = tun_net_change_mtu,
  389. .ndo_fix_features = tun_net_fix_features,
  390. };
  391. static const struct net_device_ops tap_netdev_ops = {
  392. .ndo_uninit = tun_net_uninit,
  393. .ndo_open = tun_net_open,
  394. .ndo_stop = tun_net_close,
  395. .ndo_start_xmit = tun_net_xmit,
  396. .ndo_change_mtu = tun_net_change_mtu,
  397. .ndo_fix_features = tun_net_fix_features,
  398. .ndo_set_multicast_list = tun_net_mclist,
  399. .ndo_set_mac_address = eth_mac_addr,
  400. .ndo_validate_addr = eth_validate_addr,
  401. };
  402. /* Initialize net device. */
  403. static void tun_net_init(struct net_device *dev)
  404. {
  405. struct tun_struct *tun = netdev_priv(dev);
  406. switch (tun->flags & TUN_TYPE_MASK) {
  407. case TUN_TUN_DEV:
  408. dev->netdev_ops = &tun_netdev_ops;
  409. /* Point-to-Point TUN Device */
  410. dev->hard_header_len = 0;
  411. dev->addr_len = 0;
  412. dev->mtu = 1500;
  413. /* Zero header length */
  414. dev->type = ARPHRD_NONE;
  415. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  416. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  417. break;
  418. case TUN_TAP_DEV:
  419. dev->netdev_ops = &tap_netdev_ops;
  420. /* Ethernet TAP Device */
  421. ether_setup(dev);
  422. random_ether_addr(dev->dev_addr);
  423. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  424. break;
  425. }
  426. }
  427. /* Character device part */
  428. /* Poll */
  429. static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
  430. {
  431. struct tun_file *tfile = file->private_data;
  432. struct tun_struct *tun = __tun_get(tfile);
  433. struct sock *sk;
  434. unsigned int mask = 0;
  435. if (!tun)
  436. return POLLERR;
  437. sk = tun->socket.sk;
  438. tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
  439. poll_wait(file, &tun->wq.wait, wait);
  440. if (!skb_queue_empty(&sk->sk_receive_queue))
  441. mask |= POLLIN | POLLRDNORM;
  442. if (sock_writeable(sk) ||
  443. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
  444. sock_writeable(sk)))
  445. mask |= POLLOUT | POLLWRNORM;
  446. if (tun->dev->reg_state != NETREG_REGISTERED)
  447. mask = POLLERR;
  448. tun_put(tun);
  449. return mask;
  450. }
  451. /* prepad is the amount to reserve at front. len is length after that.
  452. * linear is a hint as to how much to copy (usually headers). */
  453. static struct sk_buff *tun_alloc_skb(struct tun_struct *tun,
  454. size_t prepad, size_t len,
  455. size_t linear, int noblock)
  456. {
  457. struct sock *sk = tun->socket.sk;
  458. struct sk_buff *skb;
  459. int err;
  460. sock_update_classid(sk);
  461. /* Under a page? Don't bother with paged skb. */
  462. if (prepad + len < PAGE_SIZE || !linear)
  463. linear = len;
  464. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  465. &err);
  466. if (!skb)
  467. return ERR_PTR(err);
  468. skb_reserve(skb, prepad);
  469. skb_put(skb, linear);
  470. skb->data_len = len - linear;
  471. skb->len += len - linear;
  472. return skb;
  473. }
  474. /* Get packet from user space buffer */
  475. static ssize_t tun_get_user(struct tun_struct *tun,
  476. const struct iovec *iv, size_t count,
  477. int noblock)
  478. {
  479. struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
  480. struct sk_buff *skb;
  481. size_t len = count, align = NET_SKB_PAD;
  482. struct virtio_net_hdr gso = { 0 };
  483. int offset = 0;
  484. if (!(tun->flags & TUN_NO_PI)) {
  485. if ((len -= sizeof(pi)) > count)
  486. return -EINVAL;
  487. if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
  488. return -EFAULT;
  489. offset += sizeof(pi);
  490. }
  491. if (tun->flags & TUN_VNET_HDR) {
  492. if ((len -= tun->vnet_hdr_sz) > count)
  493. return -EINVAL;
  494. if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
  495. return -EFAULT;
  496. if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  497. gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
  498. gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
  499. if (gso.hdr_len > len)
  500. return -EINVAL;
  501. offset += tun->vnet_hdr_sz;
  502. }
  503. if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
  504. align += NET_IP_ALIGN;
  505. if (unlikely(len < ETH_HLEN ||
  506. (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
  507. return -EINVAL;
  508. }
  509. skb = tun_alloc_skb(tun, align, len, gso.hdr_len, noblock);
  510. if (IS_ERR(skb)) {
  511. if (PTR_ERR(skb) != -EAGAIN)
  512. tun->dev->stats.rx_dropped++;
  513. return PTR_ERR(skb);
  514. }
  515. if (skb_copy_datagram_from_iovec(skb, 0, iv, offset, len)) {
  516. tun->dev->stats.rx_dropped++;
  517. kfree_skb(skb);
  518. return -EFAULT;
  519. }
  520. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  521. if (!skb_partial_csum_set(skb, gso.csum_start,
  522. gso.csum_offset)) {
  523. tun->dev->stats.rx_frame_errors++;
  524. kfree_skb(skb);
  525. return -EINVAL;
  526. }
  527. }
  528. switch (tun->flags & TUN_TYPE_MASK) {
  529. case TUN_TUN_DEV:
  530. if (tun->flags & TUN_NO_PI) {
  531. switch (skb->data[0] & 0xf0) {
  532. case 0x40:
  533. pi.proto = htons(ETH_P_IP);
  534. break;
  535. case 0x60:
  536. pi.proto = htons(ETH_P_IPV6);
  537. break;
  538. default:
  539. tun->dev->stats.rx_dropped++;
  540. kfree_skb(skb);
  541. return -EINVAL;
  542. }
  543. }
  544. skb_reset_mac_header(skb);
  545. skb->protocol = pi.proto;
  546. skb->dev = tun->dev;
  547. break;
  548. case TUN_TAP_DEV:
  549. skb->protocol = eth_type_trans(skb, tun->dev);
  550. break;
  551. }
  552. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  553. pr_debug("GSO!\n");
  554. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  555. case VIRTIO_NET_HDR_GSO_TCPV4:
  556. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  557. break;
  558. case VIRTIO_NET_HDR_GSO_TCPV6:
  559. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  560. break;
  561. case VIRTIO_NET_HDR_GSO_UDP:
  562. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  563. break;
  564. default:
  565. tun->dev->stats.rx_frame_errors++;
  566. kfree_skb(skb);
  567. return -EINVAL;
  568. }
  569. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  570. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  571. skb_shinfo(skb)->gso_size = gso.gso_size;
  572. if (skb_shinfo(skb)->gso_size == 0) {
  573. tun->dev->stats.rx_frame_errors++;
  574. kfree_skb(skb);
  575. return -EINVAL;
  576. }
  577. /* Header must be checked, and gso_segs computed. */
  578. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  579. skb_shinfo(skb)->gso_segs = 0;
  580. }
  581. netif_rx_ni(skb);
  582. tun->dev->stats.rx_packets++;
  583. tun->dev->stats.rx_bytes += len;
  584. return count;
  585. }
  586. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  587. unsigned long count, loff_t pos)
  588. {
  589. struct file *file = iocb->ki_filp;
  590. struct tun_struct *tun = tun_get(file);
  591. ssize_t result;
  592. if (!tun)
  593. return -EBADFD;
  594. tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
  595. result = tun_get_user(tun, iv, iov_length(iv, count),
  596. file->f_flags & O_NONBLOCK);
  597. tun_put(tun);
  598. return result;
  599. }
  600. /* Put packet to the user space buffer */
  601. static ssize_t tun_put_user(struct tun_struct *tun,
  602. struct sk_buff *skb,
  603. const struct iovec *iv, int len)
  604. {
  605. struct tun_pi pi = { 0, skb->protocol };
  606. ssize_t total = 0;
  607. if (!(tun->flags & TUN_NO_PI)) {
  608. if ((len -= sizeof(pi)) < 0)
  609. return -EINVAL;
  610. if (len < skb->len) {
  611. /* Packet will be striped */
  612. pi.flags |= TUN_PKT_STRIP;
  613. }
  614. if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
  615. return -EFAULT;
  616. total += sizeof(pi);
  617. }
  618. if (tun->flags & TUN_VNET_HDR) {
  619. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  620. if ((len -= tun->vnet_hdr_sz) < 0)
  621. return -EINVAL;
  622. if (skb_is_gso(skb)) {
  623. struct skb_shared_info *sinfo = skb_shinfo(skb);
  624. /* This is a hint as to how much should be linear. */
  625. gso.hdr_len = skb_headlen(skb);
  626. gso.gso_size = sinfo->gso_size;
  627. if (sinfo->gso_type & SKB_GSO_TCPV4)
  628. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  629. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  630. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  631. else if (sinfo->gso_type & SKB_GSO_UDP)
  632. gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  633. else {
  634. pr_err("unexpected GSO type: "
  635. "0x%x, gso_size %d, hdr_len %d\n",
  636. sinfo->gso_type, gso.gso_size,
  637. gso.hdr_len);
  638. print_hex_dump(KERN_ERR, "tun: ",
  639. DUMP_PREFIX_NONE,
  640. 16, 1, skb->head,
  641. min((int)gso.hdr_len, 64), true);
  642. WARN_ON_ONCE(1);
  643. return -EINVAL;
  644. }
  645. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  646. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  647. } else
  648. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  649. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  650. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  651. gso.csum_start = skb_checksum_start_offset(skb);
  652. gso.csum_offset = skb->csum_offset;
  653. } /* else everything is zero */
  654. if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
  655. sizeof(gso))))
  656. return -EFAULT;
  657. total += tun->vnet_hdr_sz;
  658. }
  659. len = min_t(int, skb->len, len);
  660. skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
  661. total += skb->len;
  662. tun->dev->stats.tx_packets++;
  663. tun->dev->stats.tx_bytes += len;
  664. return total;
  665. }
  666. static ssize_t tun_do_read(struct tun_struct *tun,
  667. struct kiocb *iocb, const struct iovec *iv,
  668. ssize_t len, int noblock)
  669. {
  670. DECLARE_WAITQUEUE(wait, current);
  671. struct sk_buff *skb;
  672. ssize_t ret = 0;
  673. tun_debug(KERN_INFO, tun, "tun_chr_read\n");
  674. if (unlikely(!noblock))
  675. add_wait_queue(&tun->wq.wait, &wait);
  676. while (len) {
  677. current->state = TASK_INTERRUPTIBLE;
  678. /* Read frames from the queue */
  679. if (!(skb=skb_dequeue(&tun->socket.sk->sk_receive_queue))) {
  680. if (noblock) {
  681. ret = -EAGAIN;
  682. break;
  683. }
  684. if (signal_pending(current)) {
  685. ret = -ERESTARTSYS;
  686. break;
  687. }
  688. if (tun->dev->reg_state != NETREG_REGISTERED) {
  689. ret = -EIO;
  690. break;
  691. }
  692. /* Nothing to read, let's sleep */
  693. schedule();
  694. continue;
  695. }
  696. netif_wake_queue(tun->dev);
  697. ret = tun_put_user(tun, skb, iv, len);
  698. kfree_skb(skb);
  699. break;
  700. }
  701. current->state = TASK_RUNNING;
  702. if (unlikely(!noblock))
  703. remove_wait_queue(&tun->wq.wait, &wait);
  704. return ret;
  705. }
  706. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  707. unsigned long count, loff_t pos)
  708. {
  709. struct file *file = iocb->ki_filp;
  710. struct tun_file *tfile = file->private_data;
  711. struct tun_struct *tun = __tun_get(tfile);
  712. ssize_t len, ret;
  713. if (!tun)
  714. return -EBADFD;
  715. len = iov_length(iv, count);
  716. if (len < 0) {
  717. ret = -EINVAL;
  718. goto out;
  719. }
  720. ret = tun_do_read(tun, iocb, iv, len, file->f_flags & O_NONBLOCK);
  721. ret = min_t(ssize_t, ret, len);
  722. out:
  723. tun_put(tun);
  724. return ret;
  725. }
  726. static void tun_setup(struct net_device *dev)
  727. {
  728. struct tun_struct *tun = netdev_priv(dev);
  729. tun->owner = -1;
  730. tun->group = -1;
  731. dev->ethtool_ops = &tun_ethtool_ops;
  732. dev->destructor = tun_free_netdev;
  733. }
  734. /* Trivial set of netlink ops to allow deleting tun or tap
  735. * device with netlink.
  736. */
  737. static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
  738. {
  739. return -EINVAL;
  740. }
  741. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  742. .kind = DRV_NAME,
  743. .priv_size = sizeof(struct tun_struct),
  744. .setup = tun_setup,
  745. .validate = tun_validate,
  746. };
  747. static void tun_sock_write_space(struct sock *sk)
  748. {
  749. struct tun_struct *tun;
  750. wait_queue_head_t *wqueue;
  751. if (!sock_writeable(sk))
  752. return;
  753. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  754. return;
  755. wqueue = sk_sleep(sk);
  756. if (wqueue && waitqueue_active(wqueue))
  757. wake_up_interruptible_sync_poll(wqueue, POLLOUT |
  758. POLLWRNORM | POLLWRBAND);
  759. tun = tun_sk(sk)->tun;
  760. kill_fasync(&tun->fasync, SIGIO, POLL_OUT);
  761. }
  762. static void tun_sock_destruct(struct sock *sk)
  763. {
  764. free_netdev(tun_sk(sk)->tun->dev);
  765. }
  766. static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
  767. struct msghdr *m, size_t total_len)
  768. {
  769. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  770. return tun_get_user(tun, m->msg_iov, total_len,
  771. m->msg_flags & MSG_DONTWAIT);
  772. }
  773. static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
  774. struct msghdr *m, size_t total_len,
  775. int flags)
  776. {
  777. struct tun_struct *tun = container_of(sock, struct tun_struct, socket);
  778. int ret;
  779. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  780. return -EINVAL;
  781. ret = tun_do_read(tun, iocb, m->msg_iov, total_len,
  782. flags & MSG_DONTWAIT);
  783. if (ret > total_len) {
  784. m->msg_flags |= MSG_TRUNC;
  785. ret = flags & MSG_TRUNC ? ret : total_len;
  786. }
  787. return ret;
  788. }
  789. /* Ops structure to mimic raw sockets with tun */
  790. static const struct proto_ops tun_socket_ops = {
  791. .sendmsg = tun_sendmsg,
  792. .recvmsg = tun_recvmsg,
  793. };
  794. static struct proto tun_proto = {
  795. .name = "tun",
  796. .owner = THIS_MODULE,
  797. .obj_size = sizeof(struct tun_sock),
  798. };
  799. static int tun_flags(struct tun_struct *tun)
  800. {
  801. int flags = 0;
  802. if (tun->flags & TUN_TUN_DEV)
  803. flags |= IFF_TUN;
  804. else
  805. flags |= IFF_TAP;
  806. if (tun->flags & TUN_NO_PI)
  807. flags |= IFF_NO_PI;
  808. if (tun->flags & TUN_ONE_QUEUE)
  809. flags |= IFF_ONE_QUEUE;
  810. if (tun->flags & TUN_VNET_HDR)
  811. flags |= IFF_VNET_HDR;
  812. return flags;
  813. }
  814. static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
  815. char *buf)
  816. {
  817. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  818. return sprintf(buf, "0x%x\n", tun_flags(tun));
  819. }
  820. static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
  821. char *buf)
  822. {
  823. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  824. return sprintf(buf, "%d\n", tun->owner);
  825. }
  826. static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
  827. char *buf)
  828. {
  829. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  830. return sprintf(buf, "%d\n", tun->group);
  831. }
  832. static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
  833. static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
  834. static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
  835. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  836. {
  837. struct sock *sk;
  838. struct tun_struct *tun;
  839. struct net_device *dev;
  840. int err;
  841. dev = __dev_get_by_name(net, ifr->ifr_name);
  842. if (dev) {
  843. const struct cred *cred = current_cred();
  844. if (ifr->ifr_flags & IFF_TUN_EXCL)
  845. return -EBUSY;
  846. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  847. tun = netdev_priv(dev);
  848. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  849. tun = netdev_priv(dev);
  850. else
  851. return -EINVAL;
  852. if (((tun->owner != -1 && cred->euid != tun->owner) ||
  853. (tun->group != -1 && !in_egroup_p(tun->group))) &&
  854. !capable(CAP_NET_ADMIN))
  855. return -EPERM;
  856. err = security_tun_dev_attach(tun->socket.sk);
  857. if (err < 0)
  858. return err;
  859. err = tun_attach(tun, file);
  860. if (err < 0)
  861. return err;
  862. }
  863. else {
  864. char *name;
  865. unsigned long flags = 0;
  866. if (!capable(CAP_NET_ADMIN))
  867. return -EPERM;
  868. err = security_tun_dev_create();
  869. if (err < 0)
  870. return err;
  871. /* Set dev type */
  872. if (ifr->ifr_flags & IFF_TUN) {
  873. /* TUN device */
  874. flags |= TUN_TUN_DEV;
  875. name = "tun%d";
  876. } else if (ifr->ifr_flags & IFF_TAP) {
  877. /* TAP device */
  878. flags |= TUN_TAP_DEV;
  879. name = "tap%d";
  880. } else
  881. return -EINVAL;
  882. if (*ifr->ifr_name)
  883. name = ifr->ifr_name;
  884. dev = alloc_netdev(sizeof(struct tun_struct), name,
  885. tun_setup);
  886. if (!dev)
  887. return -ENOMEM;
  888. dev_net_set(dev, net);
  889. dev->rtnl_link_ops = &tun_link_ops;
  890. tun = netdev_priv(dev);
  891. tun->dev = dev;
  892. tun->flags = flags;
  893. tun->txflt.count = 0;
  894. tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  895. err = -ENOMEM;
  896. sk = sk_alloc(net, AF_UNSPEC, GFP_KERNEL, &tun_proto);
  897. if (!sk)
  898. goto err_free_dev;
  899. tun->socket.wq = &tun->wq;
  900. init_waitqueue_head(&tun->wq.wait);
  901. tun->socket.ops = &tun_socket_ops;
  902. sock_init_data(&tun->socket, sk);
  903. sk->sk_write_space = tun_sock_write_space;
  904. sk->sk_sndbuf = INT_MAX;
  905. tun_sk(sk)->tun = tun;
  906. security_tun_dev_post_create(sk);
  907. tun_net_init(dev);
  908. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
  909. TUN_USER_FEATURES;
  910. dev->features = dev->hw_features;
  911. err = register_netdevice(tun->dev);
  912. if (err < 0)
  913. goto err_free_sk;
  914. if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
  915. device_create_file(&tun->dev->dev, &dev_attr_owner) ||
  916. device_create_file(&tun->dev->dev, &dev_attr_group))
  917. pr_err("Failed to create tun sysfs files\n");
  918. sk->sk_destruct = tun_sock_destruct;
  919. err = tun_attach(tun, file);
  920. if (err < 0)
  921. goto failed;
  922. }
  923. tun_debug(KERN_INFO, tun, "tun_set_iff\n");
  924. if (ifr->ifr_flags & IFF_NO_PI)
  925. tun->flags |= TUN_NO_PI;
  926. else
  927. tun->flags &= ~TUN_NO_PI;
  928. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  929. tun->flags |= TUN_ONE_QUEUE;
  930. else
  931. tun->flags &= ~TUN_ONE_QUEUE;
  932. if (ifr->ifr_flags & IFF_VNET_HDR)
  933. tun->flags |= TUN_VNET_HDR;
  934. else
  935. tun->flags &= ~TUN_VNET_HDR;
  936. /* Make sure persistent devices do not get stuck in
  937. * xoff state.
  938. */
  939. if (netif_running(tun->dev))
  940. netif_wake_queue(tun->dev);
  941. strcpy(ifr->ifr_name, tun->dev->name);
  942. return 0;
  943. err_free_sk:
  944. sock_put(sk);
  945. err_free_dev:
  946. free_netdev(dev);
  947. failed:
  948. return err;
  949. }
  950. static int tun_get_iff(struct net *net, struct tun_struct *tun,
  951. struct ifreq *ifr)
  952. {
  953. tun_debug(KERN_INFO, tun, "tun_get_iff\n");
  954. strcpy(ifr->ifr_name, tun->dev->name);
  955. ifr->ifr_flags = tun_flags(tun);
  956. return 0;
  957. }
  958. /* This is like a cut-down ethtool ops, except done via tun fd so no
  959. * privs required. */
  960. static int set_offload(struct tun_struct *tun, unsigned long arg)
  961. {
  962. u32 features = 0;
  963. if (arg & TUN_F_CSUM) {
  964. features |= NETIF_F_HW_CSUM;
  965. arg &= ~TUN_F_CSUM;
  966. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  967. if (arg & TUN_F_TSO_ECN) {
  968. features |= NETIF_F_TSO_ECN;
  969. arg &= ~TUN_F_TSO_ECN;
  970. }
  971. if (arg & TUN_F_TSO4)
  972. features |= NETIF_F_TSO;
  973. if (arg & TUN_F_TSO6)
  974. features |= NETIF_F_TSO6;
  975. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  976. }
  977. if (arg & TUN_F_UFO) {
  978. features |= NETIF_F_UFO;
  979. arg &= ~TUN_F_UFO;
  980. }
  981. }
  982. /* This gives the user a way to test for new features in future by
  983. * trying to set them. */
  984. if (arg)
  985. return -EINVAL;
  986. tun->set_features = features;
  987. netdev_update_features(tun->dev);
  988. return 0;
  989. }
  990. static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  991. unsigned long arg, int ifreq_len)
  992. {
  993. struct tun_file *tfile = file->private_data;
  994. struct tun_struct *tun;
  995. void __user* argp = (void __user*)arg;
  996. struct sock_fprog fprog;
  997. struct ifreq ifr;
  998. int sndbuf;
  999. int vnet_hdr_sz;
  1000. int ret;
  1001. if (cmd == TUNSETIFF || _IOC_TYPE(cmd) == 0x89)
  1002. if (copy_from_user(&ifr, argp, ifreq_len))
  1003. return -EFAULT;
  1004. if (cmd == TUNGETFEATURES) {
  1005. /* Currently this just means: "what IFF flags are valid?".
  1006. * This is needed because we never checked for invalid flags on
  1007. * TUNSETIFF. */
  1008. return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
  1009. IFF_VNET_HDR,
  1010. (unsigned int __user*)argp);
  1011. }
  1012. rtnl_lock();
  1013. tun = __tun_get(tfile);
  1014. if (cmd == TUNSETIFF && !tun) {
  1015. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  1016. ret = tun_set_iff(tfile->net, file, &ifr);
  1017. if (ret)
  1018. goto unlock;
  1019. if (copy_to_user(argp, &ifr, ifreq_len))
  1020. ret = -EFAULT;
  1021. goto unlock;
  1022. }
  1023. ret = -EBADFD;
  1024. if (!tun)
  1025. goto unlock;
  1026. tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %d\n", cmd);
  1027. ret = 0;
  1028. switch (cmd) {
  1029. case TUNGETIFF:
  1030. ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
  1031. if (ret)
  1032. break;
  1033. if (copy_to_user(argp, &ifr, ifreq_len))
  1034. ret = -EFAULT;
  1035. break;
  1036. case TUNSETNOCSUM:
  1037. /* Disable/Enable checksum */
  1038. /* [unimplemented] */
  1039. tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
  1040. arg ? "disabled" : "enabled");
  1041. break;
  1042. case TUNSETPERSIST:
  1043. /* Disable/Enable persist mode */
  1044. if (arg)
  1045. tun->flags |= TUN_PERSIST;
  1046. else
  1047. tun->flags &= ~TUN_PERSIST;
  1048. tun_debug(KERN_INFO, tun, "persist %s\n",
  1049. arg ? "enabled" : "disabled");
  1050. break;
  1051. case TUNSETOWNER:
  1052. /* Set owner of the device */
  1053. tun->owner = (uid_t) arg;
  1054. tun_debug(KERN_INFO, tun, "owner set to %d\n", tun->owner);
  1055. break;
  1056. case TUNSETGROUP:
  1057. /* Set group of the device */
  1058. tun->group= (gid_t) arg;
  1059. tun_debug(KERN_INFO, tun, "group set to %d\n", tun->group);
  1060. break;
  1061. case TUNSETLINK:
  1062. /* Only allow setting the type when the interface is down */
  1063. if (tun->dev->flags & IFF_UP) {
  1064. tun_debug(KERN_INFO, tun,
  1065. "Linktype set failed because interface is up\n");
  1066. ret = -EBUSY;
  1067. } else {
  1068. tun->dev->type = (int) arg;
  1069. tun_debug(KERN_INFO, tun, "linktype set to %d\n",
  1070. tun->dev->type);
  1071. ret = 0;
  1072. }
  1073. break;
  1074. #ifdef TUN_DEBUG
  1075. case TUNSETDEBUG:
  1076. tun->debug = arg;
  1077. break;
  1078. #endif
  1079. case TUNSETOFFLOAD:
  1080. ret = set_offload(tun, arg);
  1081. break;
  1082. case TUNSETTXFILTER:
  1083. /* Can be set only for TAPs */
  1084. ret = -EINVAL;
  1085. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1086. break;
  1087. ret = update_filter(&tun->txflt, (void __user *)arg);
  1088. break;
  1089. case SIOCGIFHWADDR:
  1090. /* Get hw address */
  1091. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  1092. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  1093. if (copy_to_user(argp, &ifr, ifreq_len))
  1094. ret = -EFAULT;
  1095. break;
  1096. case SIOCSIFHWADDR:
  1097. /* Set hw address */
  1098. tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
  1099. ifr.ifr_hwaddr.sa_data);
  1100. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  1101. break;
  1102. case TUNGETSNDBUF:
  1103. sndbuf = tun->socket.sk->sk_sndbuf;
  1104. if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
  1105. ret = -EFAULT;
  1106. break;
  1107. case TUNSETSNDBUF:
  1108. if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
  1109. ret = -EFAULT;
  1110. break;
  1111. }
  1112. tun->socket.sk->sk_sndbuf = sndbuf;
  1113. break;
  1114. case TUNGETVNETHDRSZ:
  1115. vnet_hdr_sz = tun->vnet_hdr_sz;
  1116. if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
  1117. ret = -EFAULT;
  1118. break;
  1119. case TUNSETVNETHDRSZ:
  1120. if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
  1121. ret = -EFAULT;
  1122. break;
  1123. }
  1124. if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
  1125. ret = -EINVAL;
  1126. break;
  1127. }
  1128. tun->vnet_hdr_sz = vnet_hdr_sz;
  1129. break;
  1130. case TUNATTACHFILTER:
  1131. /* Can be set only for TAPs */
  1132. ret = -EINVAL;
  1133. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1134. break;
  1135. ret = -EFAULT;
  1136. if (copy_from_user(&fprog, argp, sizeof(fprog)))
  1137. break;
  1138. ret = sk_attach_filter(&fprog, tun->socket.sk);
  1139. break;
  1140. case TUNDETACHFILTER:
  1141. /* Can be set only for TAPs */
  1142. ret = -EINVAL;
  1143. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1144. break;
  1145. ret = sk_detach_filter(tun->socket.sk);
  1146. break;
  1147. default:
  1148. ret = -EINVAL;
  1149. break;
  1150. }
  1151. unlock:
  1152. rtnl_unlock();
  1153. if (tun)
  1154. tun_put(tun);
  1155. return ret;
  1156. }
  1157. static long tun_chr_ioctl(struct file *file,
  1158. unsigned int cmd, unsigned long arg)
  1159. {
  1160. return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
  1161. }
  1162. #ifdef CONFIG_COMPAT
  1163. static long tun_chr_compat_ioctl(struct file *file,
  1164. unsigned int cmd, unsigned long arg)
  1165. {
  1166. switch (cmd) {
  1167. case TUNSETIFF:
  1168. case TUNGETIFF:
  1169. case TUNSETTXFILTER:
  1170. case TUNGETSNDBUF:
  1171. case TUNSETSNDBUF:
  1172. case SIOCGIFHWADDR:
  1173. case SIOCSIFHWADDR:
  1174. arg = (unsigned long)compat_ptr(arg);
  1175. break;
  1176. default:
  1177. arg = (compat_ulong_t)arg;
  1178. break;
  1179. }
  1180. /*
  1181. * compat_ifreq is shorter than ifreq, so we must not access beyond
  1182. * the end of that structure. All fields that are used in this
  1183. * driver are compatible though, we don't need to convert the
  1184. * contents.
  1185. */
  1186. return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
  1187. }
  1188. #endif /* CONFIG_COMPAT */
  1189. static int tun_chr_fasync(int fd, struct file *file, int on)
  1190. {
  1191. struct tun_struct *tun = tun_get(file);
  1192. int ret;
  1193. if (!tun)
  1194. return -EBADFD;
  1195. tun_debug(KERN_INFO, tun, "tun_chr_fasync %d\n", on);
  1196. if ((ret = fasync_helper(fd, file, on, &tun->fasync)) < 0)
  1197. goto out;
  1198. if (on) {
  1199. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  1200. if (ret)
  1201. goto out;
  1202. tun->flags |= TUN_FASYNC;
  1203. } else
  1204. tun->flags &= ~TUN_FASYNC;
  1205. ret = 0;
  1206. out:
  1207. tun_put(tun);
  1208. return ret;
  1209. }
  1210. static int tun_chr_open(struct inode *inode, struct file * file)
  1211. {
  1212. struct tun_file *tfile;
  1213. DBG1(KERN_INFO, "tunX: tun_chr_open\n");
  1214. tfile = kmalloc(sizeof(*tfile), GFP_KERNEL);
  1215. if (!tfile)
  1216. return -ENOMEM;
  1217. atomic_set(&tfile->count, 0);
  1218. tfile->tun = NULL;
  1219. tfile->net = get_net(current->nsproxy->net_ns);
  1220. file->private_data = tfile;
  1221. return 0;
  1222. }
  1223. static int tun_chr_close(struct inode *inode, struct file *file)
  1224. {
  1225. struct tun_file *tfile = file->private_data;
  1226. struct tun_struct *tun;
  1227. tun = __tun_get(tfile);
  1228. if (tun) {
  1229. struct net_device *dev = tun->dev;
  1230. tun_debug(KERN_INFO, tun, "tun_chr_close\n");
  1231. __tun_detach(tun);
  1232. /* If desirable, unregister the netdevice. */
  1233. if (!(tun->flags & TUN_PERSIST)) {
  1234. rtnl_lock();
  1235. if (dev->reg_state == NETREG_REGISTERED)
  1236. unregister_netdevice(dev);
  1237. rtnl_unlock();
  1238. }
  1239. }
  1240. tun = tfile->tun;
  1241. if (tun)
  1242. sock_put(tun->socket.sk);
  1243. put_net(tfile->net);
  1244. kfree(tfile);
  1245. return 0;
  1246. }
  1247. static const struct file_operations tun_fops = {
  1248. .owner = THIS_MODULE,
  1249. .llseek = no_llseek,
  1250. .read = do_sync_read,
  1251. .aio_read = tun_chr_aio_read,
  1252. .write = do_sync_write,
  1253. .aio_write = tun_chr_aio_write,
  1254. .poll = tun_chr_poll,
  1255. .unlocked_ioctl = tun_chr_ioctl,
  1256. #ifdef CONFIG_COMPAT
  1257. .compat_ioctl = tun_chr_compat_ioctl,
  1258. #endif
  1259. .open = tun_chr_open,
  1260. .release = tun_chr_close,
  1261. .fasync = tun_chr_fasync
  1262. };
  1263. static struct miscdevice tun_miscdev = {
  1264. .minor = TUN_MINOR,
  1265. .name = "tun",
  1266. .nodename = "net/tun",
  1267. .fops = &tun_fops,
  1268. };
  1269. /* ethtool interface */
  1270. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1271. {
  1272. cmd->supported = 0;
  1273. cmd->advertising = 0;
  1274. ethtool_cmd_speed_set(cmd, SPEED_10);
  1275. cmd->duplex = DUPLEX_FULL;
  1276. cmd->port = PORT_TP;
  1277. cmd->phy_address = 0;
  1278. cmd->transceiver = XCVR_INTERNAL;
  1279. cmd->autoneg = AUTONEG_DISABLE;
  1280. cmd->maxtxpkt = 0;
  1281. cmd->maxrxpkt = 0;
  1282. return 0;
  1283. }
  1284. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1285. {
  1286. struct tun_struct *tun = netdev_priv(dev);
  1287. strcpy(info->driver, DRV_NAME);
  1288. strcpy(info->version, DRV_VERSION);
  1289. strcpy(info->fw_version, "N/A");
  1290. switch (tun->flags & TUN_TYPE_MASK) {
  1291. case TUN_TUN_DEV:
  1292. strcpy(info->bus_info, "tun");
  1293. break;
  1294. case TUN_TAP_DEV:
  1295. strcpy(info->bus_info, "tap");
  1296. break;
  1297. }
  1298. }
  1299. static u32 tun_get_msglevel(struct net_device *dev)
  1300. {
  1301. #ifdef TUN_DEBUG
  1302. struct tun_struct *tun = netdev_priv(dev);
  1303. return tun->debug;
  1304. #else
  1305. return -EOPNOTSUPP;
  1306. #endif
  1307. }
  1308. static void tun_set_msglevel(struct net_device *dev, u32 value)
  1309. {
  1310. #ifdef TUN_DEBUG
  1311. struct tun_struct *tun = netdev_priv(dev);
  1312. tun->debug = value;
  1313. #endif
  1314. }
  1315. static const struct ethtool_ops tun_ethtool_ops = {
  1316. .get_settings = tun_get_settings,
  1317. .get_drvinfo = tun_get_drvinfo,
  1318. .get_msglevel = tun_get_msglevel,
  1319. .set_msglevel = tun_set_msglevel,
  1320. .get_link = ethtool_op_get_link,
  1321. };
  1322. static int __init tun_init(void)
  1323. {
  1324. int ret = 0;
  1325. pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1326. pr_info("%s\n", DRV_COPYRIGHT);
  1327. ret = rtnl_link_register(&tun_link_ops);
  1328. if (ret) {
  1329. pr_err("Can't register link_ops\n");
  1330. goto err_linkops;
  1331. }
  1332. ret = misc_register(&tun_miscdev);
  1333. if (ret) {
  1334. pr_err("Can't register misc device %d\n", TUN_MINOR);
  1335. goto err_misc;
  1336. }
  1337. return 0;
  1338. err_misc:
  1339. rtnl_link_unregister(&tun_link_ops);
  1340. err_linkops:
  1341. return ret;
  1342. }
  1343. static void tun_cleanup(void)
  1344. {
  1345. misc_deregister(&tun_miscdev);
  1346. rtnl_link_unregister(&tun_link_ops);
  1347. }
  1348. /* Get an underlying socket object from tun file. Returns error unless file is
  1349. * attached to a device. The returned object works like a packet socket, it
  1350. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1351. * holding a reference to the file for as long as the socket is in use. */
  1352. struct socket *tun_get_socket(struct file *file)
  1353. {
  1354. struct tun_struct *tun;
  1355. if (file->f_op != &tun_fops)
  1356. return ERR_PTR(-EINVAL);
  1357. tun = tun_get(file);
  1358. if (!tun)
  1359. return ERR_PTR(-EBADFD);
  1360. tun_put(tun);
  1361. return &tun->socket;
  1362. }
  1363. EXPORT_SYMBOL_GPL(tun_get_socket);
  1364. module_init(tun_init);
  1365. module_exit(tun_cleanup);
  1366. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1367. MODULE_AUTHOR(DRV_COPYRIGHT);
  1368. MODULE_LICENSE("GPL");
  1369. MODULE_ALIAS_MISCDEV(TUN_MINOR);
  1370. MODULE_ALIAS("devname:net/tun");