tun.c 37 KB

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