tun.c 37 KB

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