tun.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034
  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 eth_random_addr() for tap MAC address.
  25. *
  26. * Harald Roelle <harald.roelle@ifi.lmu.de> 2004/04/20
  27. * Fixes in packet dropping, queue length setting and queue wakeup.
  28. * Increased default tx queue length.
  29. * Added ethtool API.
  30. * Minor cleanups
  31. *
  32. * Daniel Podlejski <underley@underley.eu.org>
  33. * Modifications for 2.3.99-pre5 kernel.
  34. */
  35. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  36. #define DRV_NAME "tun"
  37. #define DRV_VERSION "1.6"
  38. #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
  39. #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>"
  40. #include <linux/module.h>
  41. #include <linux/errno.h>
  42. #include <linux/kernel.h>
  43. #include <linux/major.h>
  44. #include <linux/slab.h>
  45. #include <linux/poll.h>
  46. #include <linux/fcntl.h>
  47. #include <linux/init.h>
  48. #include <linux/skbuff.h>
  49. #include <linux/netdevice.h>
  50. #include <linux/etherdevice.h>
  51. #include <linux/miscdevice.h>
  52. #include <linux/ethtool.h>
  53. #include <linux/rtnetlink.h>
  54. #include <linux/compat.h>
  55. #include <linux/if.h>
  56. #include <linux/if_arp.h>
  57. #include <linux/if_ether.h>
  58. #include <linux/if_tun.h>
  59. #include <linux/crc32.h>
  60. #include <linux/nsproxy.h>
  61. #include <linux/virtio_net.h>
  62. #include <linux/rcupdate.h>
  63. #include <net/net_namespace.h>
  64. #include <net/netns/generic.h>
  65. #include <net/rtnetlink.h>
  66. #include <net/sock.h>
  67. #include <asm/uaccess.h>
  68. /* Uncomment to enable debugging */
  69. /* #define TUN_DEBUG 1 */
  70. #ifdef TUN_DEBUG
  71. static int debug;
  72. #define tun_debug(level, tun, fmt, args...) \
  73. do { \
  74. if (tun->debug) \
  75. netdev_printk(level, tun->dev, fmt, ##args); \
  76. } while (0)
  77. #define DBG1(level, fmt, args...) \
  78. do { \
  79. if (debug == 2) \
  80. printk(level fmt, ##args); \
  81. } while (0)
  82. #else
  83. #define tun_debug(level, tun, fmt, args...) \
  84. do { \
  85. if (0) \
  86. netdev_printk(level, tun->dev, fmt, ##args); \
  87. } while (0)
  88. #define DBG1(level, fmt, args...) \
  89. do { \
  90. if (0) \
  91. printk(level fmt, ##args); \
  92. } while (0)
  93. #endif
  94. #define GOODCOPY_LEN 128
  95. #define FLT_EXACT_COUNT 8
  96. struct tap_filter {
  97. unsigned int count; /* Number of addrs. Zero means disabled */
  98. u32 mask[2]; /* Mask of the hashed addrs */
  99. unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
  100. };
  101. /* 1024 is probably a high enough limit: modern hypervisors seem to support on
  102. * the order of 100-200 CPUs so this leaves us some breathing space if we want
  103. * to match a queue per guest CPU.
  104. */
  105. #define MAX_TAP_QUEUES 1024
  106. /* A tun_file connects an open character device to a tuntap netdevice. It
  107. * also contains all socket related strctures (except sock_fprog and tap_filter)
  108. * to serve as one transmit queue for tuntap device. The sock_fprog and
  109. * tap_filter were kept in tun_struct since they were used for filtering for the
  110. * netdevice not for a specific queue (at least I didn't see the reqirement for
  111. * this).
  112. *
  113. * RCU usage:
  114. * The tun_file and tun_struct are loosely coupled, the pointer from on to the
  115. * other can only be read while rcu_read_lock or rtnl_lock is held.
  116. */
  117. struct tun_file {
  118. struct sock sk;
  119. struct socket socket;
  120. struct socket_wq wq;
  121. struct tun_struct __rcu *tun;
  122. struct net *net;
  123. struct fasync_struct *fasync;
  124. /* only used for fasnyc */
  125. unsigned int flags;
  126. u16 queue_index;
  127. };
  128. /* Since the socket were moved to tun_file, to preserve the behavior of persist
  129. * device, socket fileter, sndbuf and vnet header size were restore when the
  130. * file were attached to a persist device.
  131. */
  132. struct tun_struct {
  133. struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
  134. unsigned int numqueues;
  135. unsigned int flags;
  136. kuid_t owner;
  137. kgid_t group;
  138. struct net_device *dev;
  139. netdev_features_t set_features;
  140. #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
  141. NETIF_F_TSO6|NETIF_F_UFO)
  142. int vnet_hdr_sz;
  143. int sndbuf;
  144. struct tap_filter txflt;
  145. struct sock_fprog fprog;
  146. /* protected by rtnl lock */
  147. bool filter_attached;
  148. #ifdef TUN_DEBUG
  149. int debug;
  150. #endif
  151. };
  152. /* We try to identify a flow through its rxhash first. The reason that
  153. * we do not check rxq no. is becuase some cards(e.g 82599), chooses
  154. * the rxq based on the txq where the last packet of the flow comes. As
  155. * the userspace application move between processors, we may get a
  156. * different rxq no. here. If we could not get rxhash, then we would
  157. * hope the rxq no. may help here.
  158. */
  159. static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb)
  160. {
  161. struct tun_struct *tun = netdev_priv(dev);
  162. u32 txq = 0;
  163. u32 numqueues = 0;
  164. rcu_read_lock();
  165. numqueues = tun->numqueues;
  166. txq = skb_get_rxhash(skb);
  167. if (txq) {
  168. /* use multiply and shift instead of expensive divide */
  169. txq = ((u64)txq * numqueues) >> 32;
  170. } else if (likely(skb_rx_queue_recorded(skb))) {
  171. txq = skb_get_rx_queue(skb);
  172. while (unlikely(txq >= numqueues))
  173. txq -= numqueues;
  174. }
  175. rcu_read_unlock();
  176. return txq;
  177. }
  178. static inline bool tun_not_capable(struct tun_struct *tun)
  179. {
  180. const struct cred *cred = current_cred();
  181. return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
  182. (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
  183. !capable(CAP_NET_ADMIN);
  184. }
  185. static void tun_set_real_num_queues(struct tun_struct *tun)
  186. {
  187. netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
  188. netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
  189. }
  190. static void __tun_detach(struct tun_file *tfile, bool clean)
  191. {
  192. struct tun_file *ntfile;
  193. struct tun_struct *tun;
  194. struct net_device *dev;
  195. tun = rcu_dereference_protected(tfile->tun,
  196. lockdep_rtnl_is_held());
  197. if (tun) {
  198. u16 index = tfile->queue_index;
  199. BUG_ON(index >= tun->numqueues);
  200. dev = tun->dev;
  201. rcu_assign_pointer(tun->tfiles[index],
  202. tun->tfiles[tun->numqueues - 1]);
  203. rcu_assign_pointer(tfile->tun, NULL);
  204. ntfile = rcu_dereference_protected(tun->tfiles[index],
  205. lockdep_rtnl_is_held());
  206. ntfile->queue_index = index;
  207. --tun->numqueues;
  208. sock_put(&tfile->sk);
  209. synchronize_net();
  210. /* Drop read queue */
  211. skb_queue_purge(&tfile->sk.sk_receive_queue);
  212. tun_set_real_num_queues(tun);
  213. if (tun->numqueues == 0 && !(tun->flags & TUN_PERSIST))
  214. if (dev->reg_state == NETREG_REGISTERED)
  215. unregister_netdevice(dev);
  216. }
  217. if (clean) {
  218. BUG_ON(!test_bit(SOCK_EXTERNALLY_ALLOCATED,
  219. &tfile->socket.flags));
  220. sk_release_kernel(&tfile->sk);
  221. }
  222. }
  223. static void tun_detach(struct tun_file *tfile, bool clean)
  224. {
  225. rtnl_lock();
  226. __tun_detach(tfile, clean);
  227. rtnl_unlock();
  228. }
  229. static void tun_detach_all(struct net_device *dev)
  230. {
  231. struct tun_struct *tun = netdev_priv(dev);
  232. struct tun_file *tfile;
  233. int i, n = tun->numqueues;
  234. for (i = 0; i < n; i++) {
  235. tfile = rcu_dereference_protected(tun->tfiles[i],
  236. lockdep_rtnl_is_held());
  237. BUG_ON(!tfile);
  238. wake_up_all(&tfile->wq.wait);
  239. rcu_assign_pointer(tfile->tun, NULL);
  240. --tun->numqueues;
  241. }
  242. BUG_ON(tun->numqueues != 0);
  243. synchronize_net();
  244. for (i = 0; i < n; i++) {
  245. tfile = rcu_dereference_protected(tun->tfiles[i],
  246. lockdep_rtnl_is_held());
  247. /* Drop read queue */
  248. skb_queue_purge(&tfile->sk.sk_receive_queue);
  249. sock_put(&tfile->sk);
  250. }
  251. }
  252. static int tun_attach(struct tun_struct *tun, struct file *file)
  253. {
  254. struct tun_file *tfile = file->private_data;
  255. int err;
  256. err = -EINVAL;
  257. if (rcu_dereference_protected(tfile->tun, lockdep_rtnl_is_held()))
  258. goto out;
  259. err = -EBUSY;
  260. if (!(tun->flags & TUN_TAP_MQ) && tun->numqueues == 1)
  261. goto out;
  262. err = -E2BIG;
  263. if (tun->numqueues == MAX_TAP_QUEUES)
  264. goto out;
  265. err = 0;
  266. /* Re-attach the filter to presist device */
  267. if (tun->filter_attached == true) {
  268. err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
  269. if (!err)
  270. goto out;
  271. }
  272. tfile->queue_index = tun->numqueues;
  273. rcu_assign_pointer(tfile->tun, tun);
  274. rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
  275. sock_hold(&tfile->sk);
  276. tun->numqueues++;
  277. tun_set_real_num_queues(tun);
  278. if (tun->numqueues == 1)
  279. netif_carrier_on(tun->dev);
  280. /* device is allowed to go away first, so no need to hold extra
  281. * refcnt.
  282. */
  283. out:
  284. return err;
  285. }
  286. static struct tun_struct *__tun_get(struct tun_file *tfile)
  287. {
  288. struct tun_struct *tun;
  289. rcu_read_lock();
  290. tun = rcu_dereference(tfile->tun);
  291. if (tun)
  292. dev_hold(tun->dev);
  293. rcu_read_unlock();
  294. return tun;
  295. }
  296. static struct tun_struct *tun_get(struct file *file)
  297. {
  298. return __tun_get(file->private_data);
  299. }
  300. static void tun_put(struct tun_struct *tun)
  301. {
  302. dev_put(tun->dev);
  303. }
  304. /* TAP filtering */
  305. static void addr_hash_set(u32 *mask, const u8 *addr)
  306. {
  307. int n = ether_crc(ETH_ALEN, addr) >> 26;
  308. mask[n >> 5] |= (1 << (n & 31));
  309. }
  310. static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
  311. {
  312. int n = ether_crc(ETH_ALEN, addr) >> 26;
  313. return mask[n >> 5] & (1 << (n & 31));
  314. }
  315. static int update_filter(struct tap_filter *filter, void __user *arg)
  316. {
  317. struct { u8 u[ETH_ALEN]; } *addr;
  318. struct tun_filter uf;
  319. int err, alen, n, nexact;
  320. if (copy_from_user(&uf, arg, sizeof(uf)))
  321. return -EFAULT;
  322. if (!uf.count) {
  323. /* Disabled */
  324. filter->count = 0;
  325. return 0;
  326. }
  327. alen = ETH_ALEN * uf.count;
  328. addr = kmalloc(alen, GFP_KERNEL);
  329. if (!addr)
  330. return -ENOMEM;
  331. if (copy_from_user(addr, arg + sizeof(uf), alen)) {
  332. err = -EFAULT;
  333. goto done;
  334. }
  335. /* The filter is updated without holding any locks. Which is
  336. * perfectly safe. We disable it first and in the worst
  337. * case we'll accept a few undesired packets. */
  338. filter->count = 0;
  339. wmb();
  340. /* Use first set of addresses as an exact filter */
  341. for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
  342. memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
  343. nexact = n;
  344. /* Remaining multicast addresses are hashed,
  345. * unicast will leave the filter disabled. */
  346. memset(filter->mask, 0, sizeof(filter->mask));
  347. for (; n < uf.count; n++) {
  348. if (!is_multicast_ether_addr(addr[n].u)) {
  349. err = 0; /* no filter */
  350. goto done;
  351. }
  352. addr_hash_set(filter->mask, addr[n].u);
  353. }
  354. /* For ALLMULTI just set the mask to all ones.
  355. * This overrides the mask populated above. */
  356. if ((uf.flags & TUN_FLT_ALLMULTI))
  357. memset(filter->mask, ~0, sizeof(filter->mask));
  358. /* Now enable the filter */
  359. wmb();
  360. filter->count = nexact;
  361. /* Return the number of exact filters */
  362. err = nexact;
  363. done:
  364. kfree(addr);
  365. return err;
  366. }
  367. /* Returns: 0 - drop, !=0 - accept */
  368. static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
  369. {
  370. /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
  371. * at this point. */
  372. struct ethhdr *eh = (struct ethhdr *) skb->data;
  373. int i;
  374. /* Exact match */
  375. for (i = 0; i < filter->count; i++)
  376. if (ether_addr_equal(eh->h_dest, filter->addr[i]))
  377. return 1;
  378. /* Inexact match (multicast only) */
  379. if (is_multicast_ether_addr(eh->h_dest))
  380. return addr_hash_test(filter->mask, eh->h_dest);
  381. return 0;
  382. }
  383. /*
  384. * Checks whether the packet is accepted or not.
  385. * Returns: 0 - drop, !=0 - accept
  386. */
  387. static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
  388. {
  389. if (!filter->count)
  390. return 1;
  391. return run_filter(filter, skb);
  392. }
  393. /* Network device part of the driver */
  394. static const struct ethtool_ops tun_ethtool_ops;
  395. /* Net device detach from fd. */
  396. static void tun_net_uninit(struct net_device *dev)
  397. {
  398. tun_detach_all(dev);
  399. }
  400. /* Net device open. */
  401. static int tun_net_open(struct net_device *dev)
  402. {
  403. netif_tx_start_all_queues(dev);
  404. return 0;
  405. }
  406. /* Net device close. */
  407. static int tun_net_close(struct net_device *dev)
  408. {
  409. netif_tx_stop_all_queues(dev);
  410. return 0;
  411. }
  412. /* Net device start xmit */
  413. static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
  414. {
  415. struct tun_struct *tun = netdev_priv(dev);
  416. int txq = skb->queue_mapping;
  417. struct tun_file *tfile;
  418. rcu_read_lock();
  419. tfile = rcu_dereference(tun->tfiles[txq]);
  420. /* Drop packet if interface is not attached */
  421. if (txq >= tun->numqueues)
  422. goto drop;
  423. tun_debug(KERN_INFO, tun, "tun_net_xmit %d\n", skb->len);
  424. BUG_ON(!tfile);
  425. /* Drop if the filter does not like it.
  426. * This is a noop if the filter is disabled.
  427. * Filter can be enabled only for the TAP devices. */
  428. if (!check_filter(&tun->txflt, skb))
  429. goto drop;
  430. if (tfile->socket.sk->sk_filter &&
  431. sk_filter(tfile->socket.sk, skb))
  432. goto drop;
  433. /* Limit the number of packets queued by divining txq length with the
  434. * number of queues.
  435. */
  436. if (skb_queue_len(&tfile->socket.sk->sk_receive_queue)
  437. >= dev->tx_queue_len / tun->numqueues){
  438. if (!(tun->flags & TUN_ONE_QUEUE)) {
  439. /* Normal queueing mode. */
  440. /* Packet scheduler handles dropping of further packets. */
  441. netif_stop_subqueue(dev, txq);
  442. /* We won't see all dropped packets individually, so overrun
  443. * error is more appropriate. */
  444. dev->stats.tx_fifo_errors++;
  445. } else {
  446. /* Single queue mode.
  447. * Driver handles dropping of all packets itself. */
  448. goto drop;
  449. }
  450. }
  451. /* Orphan the skb - required as we might hang on to it
  452. * for indefinite time. */
  453. if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
  454. goto drop;
  455. skb_orphan(skb);
  456. /* Enqueue packet */
  457. skb_queue_tail(&tfile->socket.sk->sk_receive_queue, skb);
  458. /* Notify and wake up reader process */
  459. if (tfile->flags & TUN_FASYNC)
  460. kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
  461. wake_up_interruptible_poll(&tfile->wq.wait, POLLIN |
  462. POLLRDNORM | POLLRDBAND);
  463. rcu_read_unlock();
  464. return NETDEV_TX_OK;
  465. drop:
  466. dev->stats.tx_dropped++;
  467. kfree_skb(skb);
  468. rcu_read_unlock();
  469. return NETDEV_TX_OK;
  470. }
  471. static void tun_net_mclist(struct net_device *dev)
  472. {
  473. /*
  474. * This callback is supposed to deal with mc filter in
  475. * _rx_ path and has nothing to do with the _tx_ path.
  476. * In rx path we always accept everything userspace gives us.
  477. */
  478. }
  479. #define MIN_MTU 68
  480. #define MAX_MTU 65535
  481. static int
  482. tun_net_change_mtu(struct net_device *dev, int new_mtu)
  483. {
  484. if (new_mtu < MIN_MTU || new_mtu + dev->hard_header_len > MAX_MTU)
  485. return -EINVAL;
  486. dev->mtu = new_mtu;
  487. return 0;
  488. }
  489. static netdev_features_t tun_net_fix_features(struct net_device *dev,
  490. netdev_features_t features)
  491. {
  492. struct tun_struct *tun = netdev_priv(dev);
  493. return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
  494. }
  495. #ifdef CONFIG_NET_POLL_CONTROLLER
  496. static void tun_poll_controller(struct net_device *dev)
  497. {
  498. /*
  499. * Tun only receives frames when:
  500. * 1) the char device endpoint gets data from user space
  501. * 2) the tun socket gets a sendmsg call from user space
  502. * Since both of those are syncronous operations, we are guaranteed
  503. * never to have pending data when we poll for it
  504. * so theres nothing to do here but return.
  505. * We need this though so netpoll recognizes us as an interface that
  506. * supports polling, which enables bridge devices in virt setups to
  507. * still use netconsole
  508. */
  509. return;
  510. }
  511. #endif
  512. static const struct net_device_ops tun_netdev_ops = {
  513. .ndo_uninit = tun_net_uninit,
  514. .ndo_open = tun_net_open,
  515. .ndo_stop = tun_net_close,
  516. .ndo_start_xmit = tun_net_xmit,
  517. .ndo_change_mtu = tun_net_change_mtu,
  518. .ndo_fix_features = tun_net_fix_features,
  519. .ndo_select_queue = tun_select_queue,
  520. #ifdef CONFIG_NET_POLL_CONTROLLER
  521. .ndo_poll_controller = tun_poll_controller,
  522. #endif
  523. };
  524. static const struct net_device_ops tap_netdev_ops = {
  525. .ndo_uninit = tun_net_uninit,
  526. .ndo_open = tun_net_open,
  527. .ndo_stop = tun_net_close,
  528. .ndo_start_xmit = tun_net_xmit,
  529. .ndo_change_mtu = tun_net_change_mtu,
  530. .ndo_fix_features = tun_net_fix_features,
  531. .ndo_set_rx_mode = tun_net_mclist,
  532. .ndo_set_mac_address = eth_mac_addr,
  533. .ndo_validate_addr = eth_validate_addr,
  534. .ndo_select_queue = tun_select_queue,
  535. #ifdef CONFIG_NET_POLL_CONTROLLER
  536. .ndo_poll_controller = tun_poll_controller,
  537. #endif
  538. };
  539. /* Initialize net device. */
  540. static void tun_net_init(struct net_device *dev)
  541. {
  542. struct tun_struct *tun = netdev_priv(dev);
  543. switch (tun->flags & TUN_TYPE_MASK) {
  544. case TUN_TUN_DEV:
  545. dev->netdev_ops = &tun_netdev_ops;
  546. /* Point-to-Point TUN Device */
  547. dev->hard_header_len = 0;
  548. dev->addr_len = 0;
  549. dev->mtu = 1500;
  550. /* Zero header length */
  551. dev->type = ARPHRD_NONE;
  552. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  553. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  554. break;
  555. case TUN_TAP_DEV:
  556. dev->netdev_ops = &tap_netdev_ops;
  557. /* Ethernet TAP Device */
  558. ether_setup(dev);
  559. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  560. eth_hw_addr_random(dev);
  561. dev->tx_queue_len = TUN_READQ_SIZE; /* We prefer our own queue length */
  562. break;
  563. }
  564. }
  565. /* Character device part */
  566. /* Poll */
  567. static unsigned int tun_chr_poll(struct file *file, poll_table *wait)
  568. {
  569. struct tun_file *tfile = file->private_data;
  570. struct tun_struct *tun = __tun_get(tfile);
  571. struct sock *sk;
  572. unsigned int mask = 0;
  573. if (!tun)
  574. return POLLERR;
  575. sk = tfile->socket.sk;
  576. tun_debug(KERN_INFO, tun, "tun_chr_poll\n");
  577. poll_wait(file, &tfile->wq.wait, wait);
  578. if (!skb_queue_empty(&sk->sk_receive_queue))
  579. mask |= POLLIN | POLLRDNORM;
  580. if (sock_writeable(sk) ||
  581. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
  582. sock_writeable(sk)))
  583. mask |= POLLOUT | POLLWRNORM;
  584. if (tun->dev->reg_state != NETREG_REGISTERED)
  585. mask = POLLERR;
  586. tun_put(tun);
  587. return mask;
  588. }
  589. /* prepad is the amount to reserve at front. len is length after that.
  590. * linear is a hint as to how much to copy (usually headers). */
  591. static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
  592. size_t prepad, size_t len,
  593. size_t linear, int noblock)
  594. {
  595. struct sock *sk = tfile->socket.sk;
  596. struct sk_buff *skb;
  597. int err;
  598. /* Under a page? Don't bother with paged skb. */
  599. if (prepad + len < PAGE_SIZE || !linear)
  600. linear = len;
  601. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  602. &err);
  603. if (!skb)
  604. return ERR_PTR(err);
  605. skb_reserve(skb, prepad);
  606. skb_put(skb, linear);
  607. skb->data_len = len - linear;
  608. skb->len += len - linear;
  609. return skb;
  610. }
  611. /* set skb frags from iovec, this can move to core network code for reuse */
  612. static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
  613. int offset, size_t count)
  614. {
  615. int len = iov_length(from, count) - offset;
  616. int copy = skb_headlen(skb);
  617. int size, offset1 = 0;
  618. int i = 0;
  619. /* Skip over from offset */
  620. while (count && (offset >= from->iov_len)) {
  621. offset -= from->iov_len;
  622. ++from;
  623. --count;
  624. }
  625. /* copy up to skb headlen */
  626. while (count && (copy > 0)) {
  627. size = min_t(unsigned int, copy, from->iov_len - offset);
  628. if (copy_from_user(skb->data + offset1, from->iov_base + offset,
  629. size))
  630. return -EFAULT;
  631. if (copy > size) {
  632. ++from;
  633. --count;
  634. offset = 0;
  635. } else
  636. offset += size;
  637. copy -= size;
  638. offset1 += size;
  639. }
  640. if (len == offset1)
  641. return 0;
  642. while (count--) {
  643. struct page *page[MAX_SKB_FRAGS];
  644. int num_pages;
  645. unsigned long base;
  646. unsigned long truesize;
  647. len = from->iov_len - offset;
  648. if (!len) {
  649. offset = 0;
  650. ++from;
  651. continue;
  652. }
  653. base = (unsigned long)from->iov_base + offset;
  654. size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
  655. if (i + size > MAX_SKB_FRAGS)
  656. return -EMSGSIZE;
  657. num_pages = get_user_pages_fast(base, size, 0, &page[i]);
  658. if (num_pages != size) {
  659. for (i = 0; i < num_pages; i++)
  660. put_page(page[i]);
  661. return -EFAULT;
  662. }
  663. truesize = size * PAGE_SIZE;
  664. skb->data_len += len;
  665. skb->len += len;
  666. skb->truesize += truesize;
  667. atomic_add(truesize, &skb->sk->sk_wmem_alloc);
  668. while (len) {
  669. int off = base & ~PAGE_MASK;
  670. int size = min_t(int, len, PAGE_SIZE - off);
  671. __skb_fill_page_desc(skb, i, page[i], off, size);
  672. skb_shinfo(skb)->nr_frags++;
  673. /* increase sk_wmem_alloc */
  674. base += size;
  675. len -= size;
  676. i++;
  677. }
  678. offset = 0;
  679. ++from;
  680. }
  681. return 0;
  682. }
  683. /* Get packet from user space buffer */
  684. static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
  685. void *msg_control, const struct iovec *iv,
  686. size_t total_len, size_t count, int noblock)
  687. {
  688. struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
  689. struct sk_buff *skb;
  690. size_t len = total_len, align = NET_SKB_PAD;
  691. struct virtio_net_hdr gso = { 0 };
  692. int offset = 0;
  693. int copylen;
  694. bool zerocopy = false;
  695. int err;
  696. if (!(tun->flags & TUN_NO_PI)) {
  697. if ((len -= sizeof(pi)) > total_len)
  698. return -EINVAL;
  699. if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
  700. return -EFAULT;
  701. offset += sizeof(pi);
  702. }
  703. if (tun->flags & TUN_VNET_HDR) {
  704. if ((len -= tun->vnet_hdr_sz) > total_len)
  705. return -EINVAL;
  706. if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
  707. return -EFAULT;
  708. if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  709. gso.csum_start + gso.csum_offset + 2 > gso.hdr_len)
  710. gso.hdr_len = gso.csum_start + gso.csum_offset + 2;
  711. if (gso.hdr_len > len)
  712. return -EINVAL;
  713. offset += tun->vnet_hdr_sz;
  714. }
  715. if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
  716. align += NET_IP_ALIGN;
  717. if (unlikely(len < ETH_HLEN ||
  718. (gso.hdr_len && gso.hdr_len < ETH_HLEN)))
  719. return -EINVAL;
  720. }
  721. if (msg_control)
  722. zerocopy = true;
  723. if (zerocopy) {
  724. /* Userspace may produce vectors with count greater than
  725. * MAX_SKB_FRAGS, so we need to linearize parts of the skb
  726. * to let the rest of data to be fit in the frags.
  727. */
  728. if (count > MAX_SKB_FRAGS) {
  729. copylen = iov_length(iv, count - MAX_SKB_FRAGS);
  730. if (copylen < offset)
  731. copylen = 0;
  732. else
  733. copylen -= offset;
  734. } else
  735. copylen = 0;
  736. /* There are 256 bytes to be copied in skb, so there is enough
  737. * room for skb expand head in case it is used.
  738. * The rest of the buffer is mapped from userspace.
  739. */
  740. if (copylen < gso.hdr_len)
  741. copylen = gso.hdr_len;
  742. if (!copylen)
  743. copylen = GOODCOPY_LEN;
  744. } else
  745. copylen = len;
  746. skb = tun_alloc_skb(tfile, align, copylen, gso.hdr_len, noblock);
  747. if (IS_ERR(skb)) {
  748. if (PTR_ERR(skb) != -EAGAIN)
  749. tun->dev->stats.rx_dropped++;
  750. return PTR_ERR(skb);
  751. }
  752. if (zerocopy)
  753. err = zerocopy_sg_from_iovec(skb, iv, offset, count);
  754. else
  755. err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
  756. if (err) {
  757. tun->dev->stats.rx_dropped++;
  758. kfree_skb(skb);
  759. return -EFAULT;
  760. }
  761. if (gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  762. if (!skb_partial_csum_set(skb, gso.csum_start,
  763. gso.csum_offset)) {
  764. tun->dev->stats.rx_frame_errors++;
  765. kfree_skb(skb);
  766. return -EINVAL;
  767. }
  768. }
  769. switch (tun->flags & TUN_TYPE_MASK) {
  770. case TUN_TUN_DEV:
  771. if (tun->flags & TUN_NO_PI) {
  772. switch (skb->data[0] & 0xf0) {
  773. case 0x40:
  774. pi.proto = htons(ETH_P_IP);
  775. break;
  776. case 0x60:
  777. pi.proto = htons(ETH_P_IPV6);
  778. break;
  779. default:
  780. tun->dev->stats.rx_dropped++;
  781. kfree_skb(skb);
  782. return -EINVAL;
  783. }
  784. }
  785. skb_reset_mac_header(skb);
  786. skb->protocol = pi.proto;
  787. skb->dev = tun->dev;
  788. break;
  789. case TUN_TAP_DEV:
  790. skb->protocol = eth_type_trans(skb, tun->dev);
  791. break;
  792. }
  793. if (gso.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  794. pr_debug("GSO!\n");
  795. switch (gso.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  796. case VIRTIO_NET_HDR_GSO_TCPV4:
  797. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  798. break;
  799. case VIRTIO_NET_HDR_GSO_TCPV6:
  800. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  801. break;
  802. case VIRTIO_NET_HDR_GSO_UDP:
  803. skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
  804. break;
  805. default:
  806. tun->dev->stats.rx_frame_errors++;
  807. kfree_skb(skb);
  808. return -EINVAL;
  809. }
  810. if (gso.gso_type & VIRTIO_NET_HDR_GSO_ECN)
  811. skb_shinfo(skb)->gso_type |= SKB_GSO_TCP_ECN;
  812. skb_shinfo(skb)->gso_size = gso.gso_size;
  813. if (skb_shinfo(skb)->gso_size == 0) {
  814. tun->dev->stats.rx_frame_errors++;
  815. kfree_skb(skb);
  816. return -EINVAL;
  817. }
  818. /* Header must be checked, and gso_segs computed. */
  819. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  820. skb_shinfo(skb)->gso_segs = 0;
  821. }
  822. /* copy skb_ubuf_info for callback when skb has no error */
  823. if (zerocopy) {
  824. skb_shinfo(skb)->destructor_arg = msg_control;
  825. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  826. }
  827. netif_rx_ni(skb);
  828. tun->dev->stats.rx_packets++;
  829. tun->dev->stats.rx_bytes += len;
  830. return total_len;
  831. }
  832. static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
  833. unsigned long count, loff_t pos)
  834. {
  835. struct file *file = iocb->ki_filp;
  836. struct tun_struct *tun = tun_get(file);
  837. struct tun_file *tfile = file->private_data;
  838. ssize_t result;
  839. if (!tun)
  840. return -EBADFD;
  841. tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
  842. result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
  843. count, file->f_flags & O_NONBLOCK);
  844. tun_put(tun);
  845. return result;
  846. }
  847. /* Put packet to the user space buffer */
  848. static ssize_t tun_put_user(struct tun_struct *tun,
  849. struct tun_file *tfile,
  850. struct sk_buff *skb,
  851. const struct iovec *iv, int len)
  852. {
  853. struct tun_pi pi = { 0, skb->protocol };
  854. ssize_t total = 0;
  855. if (!(tun->flags & TUN_NO_PI)) {
  856. if ((len -= sizeof(pi)) < 0)
  857. return -EINVAL;
  858. if (len < skb->len) {
  859. /* Packet will be striped */
  860. pi.flags |= TUN_PKT_STRIP;
  861. }
  862. if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi)))
  863. return -EFAULT;
  864. total += sizeof(pi);
  865. }
  866. if (tun->flags & TUN_VNET_HDR) {
  867. struct virtio_net_hdr gso = { 0 }; /* no info leak */
  868. if ((len -= tun->vnet_hdr_sz) < 0)
  869. return -EINVAL;
  870. if (skb_is_gso(skb)) {
  871. struct skb_shared_info *sinfo = skb_shinfo(skb);
  872. /* This is a hint as to how much should be linear. */
  873. gso.hdr_len = skb_headlen(skb);
  874. gso.gso_size = sinfo->gso_size;
  875. if (sinfo->gso_type & SKB_GSO_TCPV4)
  876. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  877. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  878. gso.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  879. else if (sinfo->gso_type & SKB_GSO_UDP)
  880. gso.gso_type = VIRTIO_NET_HDR_GSO_UDP;
  881. else {
  882. pr_err("unexpected GSO type: "
  883. "0x%x, gso_size %d, hdr_len %d\n",
  884. sinfo->gso_type, gso.gso_size,
  885. gso.hdr_len);
  886. print_hex_dump(KERN_ERR, "tun: ",
  887. DUMP_PREFIX_NONE,
  888. 16, 1, skb->head,
  889. min((int)gso.hdr_len, 64), true);
  890. WARN_ON_ONCE(1);
  891. return -EINVAL;
  892. }
  893. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  894. gso.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  895. } else
  896. gso.gso_type = VIRTIO_NET_HDR_GSO_NONE;
  897. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  898. gso.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  899. gso.csum_start = skb_checksum_start_offset(skb);
  900. gso.csum_offset = skb->csum_offset;
  901. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  902. gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
  903. } /* else everything is zero */
  904. if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
  905. sizeof(gso))))
  906. return -EFAULT;
  907. total += tun->vnet_hdr_sz;
  908. }
  909. len = min_t(int, skb->len, len);
  910. skb_copy_datagram_const_iovec(skb, 0, iv, total, len);
  911. total += skb->len;
  912. tun->dev->stats.tx_packets++;
  913. tun->dev->stats.tx_bytes += len;
  914. return total;
  915. }
  916. static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
  917. struct kiocb *iocb, const struct iovec *iv,
  918. ssize_t len, int noblock)
  919. {
  920. DECLARE_WAITQUEUE(wait, current);
  921. struct sk_buff *skb;
  922. ssize_t ret = 0;
  923. tun_debug(KERN_INFO, tun, "tun_chr_read\n");
  924. if (unlikely(!noblock))
  925. add_wait_queue(&tfile->wq.wait, &wait);
  926. while (len) {
  927. current->state = TASK_INTERRUPTIBLE;
  928. /* Read frames from the queue */
  929. if (!(skb = skb_dequeue(&tfile->socket.sk->sk_receive_queue))) {
  930. if (noblock) {
  931. ret = -EAGAIN;
  932. break;
  933. }
  934. if (signal_pending(current)) {
  935. ret = -ERESTARTSYS;
  936. break;
  937. }
  938. if (tun->dev->reg_state != NETREG_REGISTERED) {
  939. ret = -EIO;
  940. break;
  941. }
  942. /* Nothing to read, let's sleep */
  943. schedule();
  944. continue;
  945. }
  946. netif_wake_subqueue(tun->dev, tfile->queue_index);
  947. ret = tun_put_user(tun, tfile, skb, iv, len);
  948. kfree_skb(skb);
  949. break;
  950. }
  951. current->state = TASK_RUNNING;
  952. if (unlikely(!noblock))
  953. remove_wait_queue(&tfile->wq.wait, &wait);
  954. return ret;
  955. }
  956. static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
  957. unsigned long count, loff_t pos)
  958. {
  959. struct file *file = iocb->ki_filp;
  960. struct tun_file *tfile = file->private_data;
  961. struct tun_struct *tun = __tun_get(tfile);
  962. ssize_t len, ret;
  963. if (!tun)
  964. return -EBADFD;
  965. len = iov_length(iv, count);
  966. if (len < 0) {
  967. ret = -EINVAL;
  968. goto out;
  969. }
  970. ret = tun_do_read(tun, tfile, iocb, iv, len,
  971. file->f_flags & O_NONBLOCK);
  972. ret = min_t(ssize_t, ret, len);
  973. out:
  974. tun_put(tun);
  975. return ret;
  976. }
  977. static void tun_setup(struct net_device *dev)
  978. {
  979. struct tun_struct *tun = netdev_priv(dev);
  980. tun->owner = INVALID_UID;
  981. tun->group = INVALID_GID;
  982. dev->ethtool_ops = &tun_ethtool_ops;
  983. dev->destructor = free_netdev;
  984. }
  985. /* Trivial set of netlink ops to allow deleting tun or tap
  986. * device with netlink.
  987. */
  988. static int tun_validate(struct nlattr *tb[], struct nlattr *data[])
  989. {
  990. return -EINVAL;
  991. }
  992. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  993. .kind = DRV_NAME,
  994. .priv_size = sizeof(struct tun_struct),
  995. .setup = tun_setup,
  996. .validate = tun_validate,
  997. };
  998. static void tun_sock_write_space(struct sock *sk)
  999. {
  1000. struct tun_file *tfile;
  1001. wait_queue_head_t *wqueue;
  1002. if (!sock_writeable(sk))
  1003. return;
  1004. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  1005. return;
  1006. wqueue = sk_sleep(sk);
  1007. if (wqueue && waitqueue_active(wqueue))
  1008. wake_up_interruptible_sync_poll(wqueue, POLLOUT |
  1009. POLLWRNORM | POLLWRBAND);
  1010. tfile = container_of(sk, struct tun_file, sk);
  1011. kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
  1012. }
  1013. static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
  1014. struct msghdr *m, size_t total_len)
  1015. {
  1016. int ret;
  1017. struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  1018. struct tun_struct *tun = __tun_get(tfile);
  1019. if (!tun)
  1020. return -EBADFD;
  1021. ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
  1022. m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
  1023. tun_put(tun);
  1024. return ret;
  1025. }
  1026. static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
  1027. struct msghdr *m, size_t total_len,
  1028. int flags)
  1029. {
  1030. struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  1031. struct tun_struct *tun = __tun_get(tfile);
  1032. int ret;
  1033. if (!tun)
  1034. return -EBADFD;
  1035. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  1036. return -EINVAL;
  1037. ret = tun_do_read(tun, tfile, iocb, m->msg_iov, total_len,
  1038. flags & MSG_DONTWAIT);
  1039. if (ret > total_len) {
  1040. m->msg_flags |= MSG_TRUNC;
  1041. ret = flags & MSG_TRUNC ? ret : total_len;
  1042. }
  1043. tun_put(tun);
  1044. return ret;
  1045. }
  1046. static int tun_release(struct socket *sock)
  1047. {
  1048. if (sock->sk)
  1049. sock_put(sock->sk);
  1050. return 0;
  1051. }
  1052. /* Ops structure to mimic raw sockets with tun */
  1053. static const struct proto_ops tun_socket_ops = {
  1054. .sendmsg = tun_sendmsg,
  1055. .recvmsg = tun_recvmsg,
  1056. .release = tun_release,
  1057. };
  1058. static struct proto tun_proto = {
  1059. .name = "tun",
  1060. .owner = THIS_MODULE,
  1061. .obj_size = sizeof(struct tun_file),
  1062. };
  1063. static int tun_flags(struct tun_struct *tun)
  1064. {
  1065. int flags = 0;
  1066. if (tun->flags & TUN_TUN_DEV)
  1067. flags |= IFF_TUN;
  1068. else
  1069. flags |= IFF_TAP;
  1070. if (tun->flags & TUN_NO_PI)
  1071. flags |= IFF_NO_PI;
  1072. if (tun->flags & TUN_ONE_QUEUE)
  1073. flags |= IFF_ONE_QUEUE;
  1074. if (tun->flags & TUN_VNET_HDR)
  1075. flags |= IFF_VNET_HDR;
  1076. if (tun->flags & TUN_TAP_MQ)
  1077. flags |= IFF_MULTI_QUEUE;
  1078. return flags;
  1079. }
  1080. static ssize_t tun_show_flags(struct device *dev, struct device_attribute *attr,
  1081. char *buf)
  1082. {
  1083. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  1084. return sprintf(buf, "0x%x\n", tun_flags(tun));
  1085. }
  1086. static ssize_t tun_show_owner(struct device *dev, struct device_attribute *attr,
  1087. char *buf)
  1088. {
  1089. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  1090. return uid_valid(tun->owner)?
  1091. sprintf(buf, "%u\n",
  1092. from_kuid_munged(current_user_ns(), tun->owner)):
  1093. sprintf(buf, "-1\n");
  1094. }
  1095. static ssize_t tun_show_group(struct device *dev, struct device_attribute *attr,
  1096. char *buf)
  1097. {
  1098. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  1099. return gid_valid(tun->group) ?
  1100. sprintf(buf, "%u\n",
  1101. from_kgid_munged(current_user_ns(), tun->group)):
  1102. sprintf(buf, "-1\n");
  1103. }
  1104. static DEVICE_ATTR(tun_flags, 0444, tun_show_flags, NULL);
  1105. static DEVICE_ATTR(owner, 0444, tun_show_owner, NULL);
  1106. static DEVICE_ATTR(group, 0444, tun_show_group, NULL);
  1107. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  1108. {
  1109. struct tun_struct *tun;
  1110. struct tun_file *tfile = file->private_data;
  1111. struct net_device *dev;
  1112. int err;
  1113. dev = __dev_get_by_name(net, ifr->ifr_name);
  1114. if (dev) {
  1115. if (ifr->ifr_flags & IFF_TUN_EXCL)
  1116. return -EBUSY;
  1117. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  1118. tun = netdev_priv(dev);
  1119. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  1120. tun = netdev_priv(dev);
  1121. else
  1122. return -EINVAL;
  1123. if (tun_not_capable(tun))
  1124. return -EPERM;
  1125. err = security_tun_dev_attach(tfile->socket.sk);
  1126. if (err < 0)
  1127. return err;
  1128. err = tun_attach(tun, file);
  1129. if (err < 0)
  1130. return err;
  1131. }
  1132. else {
  1133. char *name;
  1134. unsigned long flags = 0;
  1135. if (!capable(CAP_NET_ADMIN))
  1136. return -EPERM;
  1137. err = security_tun_dev_create();
  1138. if (err < 0)
  1139. return err;
  1140. /* Set dev type */
  1141. if (ifr->ifr_flags & IFF_TUN) {
  1142. /* TUN device */
  1143. flags |= TUN_TUN_DEV;
  1144. name = "tun%d";
  1145. } else if (ifr->ifr_flags & IFF_TAP) {
  1146. /* TAP device */
  1147. flags |= TUN_TAP_DEV;
  1148. name = "tap%d";
  1149. } else
  1150. return -EINVAL;
  1151. if (*ifr->ifr_name)
  1152. name = ifr->ifr_name;
  1153. dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
  1154. tun_setup,
  1155. MAX_TAP_QUEUES, MAX_TAP_QUEUES);
  1156. if (!dev)
  1157. return -ENOMEM;
  1158. dev_net_set(dev, net);
  1159. dev->rtnl_link_ops = &tun_link_ops;
  1160. tun = netdev_priv(dev);
  1161. tun->dev = dev;
  1162. tun->flags = flags;
  1163. tun->txflt.count = 0;
  1164. tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  1165. tun->filter_attached = false;
  1166. tun->sndbuf = tfile->socket.sk->sk_sndbuf;
  1167. security_tun_dev_post_create(&tfile->sk);
  1168. tun_net_init(dev);
  1169. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
  1170. TUN_USER_FEATURES;
  1171. dev->features = dev->hw_features;
  1172. err = register_netdevice(tun->dev);
  1173. if (err < 0)
  1174. goto err_free_dev;
  1175. if (device_create_file(&tun->dev->dev, &dev_attr_tun_flags) ||
  1176. device_create_file(&tun->dev->dev, &dev_attr_owner) ||
  1177. device_create_file(&tun->dev->dev, &dev_attr_group))
  1178. pr_err("Failed to create tun sysfs files\n");
  1179. err = tun_attach(tun, file);
  1180. if (err < 0)
  1181. goto err_free_dev;
  1182. }
  1183. tun_debug(KERN_INFO, tun, "tun_set_iff\n");
  1184. if (ifr->ifr_flags & IFF_NO_PI)
  1185. tun->flags |= TUN_NO_PI;
  1186. else
  1187. tun->flags &= ~TUN_NO_PI;
  1188. if (ifr->ifr_flags & IFF_ONE_QUEUE)
  1189. tun->flags |= TUN_ONE_QUEUE;
  1190. else
  1191. tun->flags &= ~TUN_ONE_QUEUE;
  1192. if (ifr->ifr_flags & IFF_VNET_HDR)
  1193. tun->flags |= TUN_VNET_HDR;
  1194. else
  1195. tun->flags &= ~TUN_VNET_HDR;
  1196. if (ifr->ifr_flags & IFF_MULTI_QUEUE)
  1197. tun->flags |= TUN_TAP_MQ;
  1198. else
  1199. tun->flags &= ~TUN_TAP_MQ;
  1200. /* Make sure persistent devices do not get stuck in
  1201. * xoff state.
  1202. */
  1203. if (netif_running(tun->dev))
  1204. netif_tx_wake_all_queues(tun->dev);
  1205. strcpy(ifr->ifr_name, tun->dev->name);
  1206. return 0;
  1207. err_free_dev:
  1208. free_netdev(dev);
  1209. return err;
  1210. }
  1211. static int tun_get_iff(struct net *net, struct tun_struct *tun,
  1212. struct ifreq *ifr)
  1213. {
  1214. tun_debug(KERN_INFO, tun, "tun_get_iff\n");
  1215. strcpy(ifr->ifr_name, tun->dev->name);
  1216. ifr->ifr_flags = tun_flags(tun);
  1217. return 0;
  1218. }
  1219. /* This is like a cut-down ethtool ops, except done via tun fd so no
  1220. * privs required. */
  1221. static int set_offload(struct tun_struct *tun, unsigned long arg)
  1222. {
  1223. netdev_features_t features = 0;
  1224. if (arg & TUN_F_CSUM) {
  1225. features |= NETIF_F_HW_CSUM;
  1226. arg &= ~TUN_F_CSUM;
  1227. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  1228. if (arg & TUN_F_TSO_ECN) {
  1229. features |= NETIF_F_TSO_ECN;
  1230. arg &= ~TUN_F_TSO_ECN;
  1231. }
  1232. if (arg & TUN_F_TSO4)
  1233. features |= NETIF_F_TSO;
  1234. if (arg & TUN_F_TSO6)
  1235. features |= NETIF_F_TSO6;
  1236. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  1237. }
  1238. if (arg & TUN_F_UFO) {
  1239. features |= NETIF_F_UFO;
  1240. arg &= ~TUN_F_UFO;
  1241. }
  1242. }
  1243. /* This gives the user a way to test for new features in future by
  1244. * trying to set them. */
  1245. if (arg)
  1246. return -EINVAL;
  1247. tun->set_features = features;
  1248. netdev_update_features(tun->dev);
  1249. return 0;
  1250. }
  1251. static void tun_detach_filter(struct tun_struct *tun, int n)
  1252. {
  1253. int i;
  1254. struct tun_file *tfile;
  1255. for (i = 0; i < n; i++) {
  1256. tfile = rcu_dereference_protected(tun->tfiles[i],
  1257. lockdep_rtnl_is_held());
  1258. sk_detach_filter(tfile->socket.sk);
  1259. }
  1260. tun->filter_attached = false;
  1261. }
  1262. static int tun_attach_filter(struct tun_struct *tun)
  1263. {
  1264. int i, ret = 0;
  1265. struct tun_file *tfile;
  1266. for (i = 0; i < tun->numqueues; i++) {
  1267. tfile = rcu_dereference_protected(tun->tfiles[i],
  1268. lockdep_rtnl_is_held());
  1269. ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
  1270. if (ret) {
  1271. tun_detach_filter(tun, i);
  1272. return ret;
  1273. }
  1274. }
  1275. tun->filter_attached = true;
  1276. return ret;
  1277. }
  1278. static void tun_set_sndbuf(struct tun_struct *tun)
  1279. {
  1280. struct tun_file *tfile;
  1281. int i;
  1282. for (i = 0; i < tun->numqueues; i++) {
  1283. tfile = rcu_dereference_protected(tun->tfiles[i],
  1284. lockdep_rtnl_is_held());
  1285. tfile->socket.sk->sk_sndbuf = tun->sndbuf;
  1286. }
  1287. }
  1288. static int tun_set_queue(struct file *file, struct ifreq *ifr)
  1289. {
  1290. struct tun_file *tfile = file->private_data;
  1291. struct tun_struct *tun;
  1292. struct net_device *dev;
  1293. int ret = 0;
  1294. rtnl_lock();
  1295. if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
  1296. dev = __dev_get_by_name(tfile->net, ifr->ifr_name);
  1297. if (!dev) {
  1298. ret = -EINVAL;
  1299. goto unlock;
  1300. }
  1301. tun = netdev_priv(dev);
  1302. if (dev->netdev_ops != &tap_netdev_ops &&
  1303. dev->netdev_ops != &tun_netdev_ops)
  1304. ret = -EINVAL;
  1305. else if (tun_not_capable(tun))
  1306. ret = -EPERM;
  1307. else
  1308. ret = tun_attach(tun, file);
  1309. } else if (ifr->ifr_flags & IFF_DETACH_QUEUE)
  1310. __tun_detach(tfile, false);
  1311. else
  1312. ret = -EINVAL;
  1313. unlock:
  1314. rtnl_unlock();
  1315. return ret;
  1316. }
  1317. static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  1318. unsigned long arg, int ifreq_len)
  1319. {
  1320. struct tun_file *tfile = file->private_data;
  1321. struct tun_struct *tun;
  1322. void __user* argp = (void __user*)arg;
  1323. struct ifreq ifr;
  1324. kuid_t owner;
  1325. kgid_t group;
  1326. int sndbuf;
  1327. int vnet_hdr_sz;
  1328. int ret;
  1329. if (cmd == TUNSETIFF || cmd == TUNSETQUEUE || _IOC_TYPE(cmd) == 0x89) {
  1330. if (copy_from_user(&ifr, argp, ifreq_len))
  1331. return -EFAULT;
  1332. } else {
  1333. memset(&ifr, 0, sizeof(ifr));
  1334. }
  1335. if (cmd == TUNGETFEATURES) {
  1336. /* Currently this just means: "what IFF flags are valid?".
  1337. * This is needed because we never checked for invalid flags on
  1338. * TUNSETIFF. */
  1339. return put_user(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
  1340. IFF_VNET_HDR | IFF_MULTI_QUEUE,
  1341. (unsigned int __user*)argp);
  1342. } else if (cmd == TUNSETQUEUE)
  1343. return tun_set_queue(file, &ifr);
  1344. ret = 0;
  1345. rtnl_lock();
  1346. tun = __tun_get(tfile);
  1347. if (cmd == TUNSETIFF && !tun) {
  1348. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  1349. ret = tun_set_iff(tfile->net, file, &ifr);
  1350. if (ret)
  1351. goto unlock;
  1352. if (copy_to_user(argp, &ifr, ifreq_len))
  1353. ret = -EFAULT;
  1354. goto unlock;
  1355. }
  1356. ret = -EBADFD;
  1357. if (!tun)
  1358. goto unlock;
  1359. tun_debug(KERN_INFO, tun, "tun_chr_ioctl cmd %u\n", cmd);
  1360. ret = 0;
  1361. switch (cmd) {
  1362. case TUNGETIFF:
  1363. ret = tun_get_iff(current->nsproxy->net_ns, tun, &ifr);
  1364. if (ret)
  1365. break;
  1366. if (copy_to_user(argp, &ifr, ifreq_len))
  1367. ret = -EFAULT;
  1368. break;
  1369. case TUNSETNOCSUM:
  1370. /* Disable/Enable checksum */
  1371. /* [unimplemented] */
  1372. tun_debug(KERN_INFO, tun, "ignored: set checksum %s\n",
  1373. arg ? "disabled" : "enabled");
  1374. break;
  1375. case TUNSETPERSIST:
  1376. /* Disable/Enable persist mode. Keep an extra reference to the
  1377. * module to prevent the module being unprobed.
  1378. */
  1379. if (arg) {
  1380. tun->flags |= TUN_PERSIST;
  1381. __module_get(THIS_MODULE);
  1382. } else {
  1383. tun->flags &= ~TUN_PERSIST;
  1384. module_put(THIS_MODULE);
  1385. }
  1386. tun_debug(KERN_INFO, tun, "persist %s\n",
  1387. arg ? "enabled" : "disabled");
  1388. break;
  1389. case TUNSETOWNER:
  1390. /* Set owner of the device */
  1391. owner = make_kuid(current_user_ns(), arg);
  1392. if (!uid_valid(owner)) {
  1393. ret = -EINVAL;
  1394. break;
  1395. }
  1396. tun->owner = owner;
  1397. tun_debug(KERN_INFO, tun, "owner set to %u\n",
  1398. from_kuid(&init_user_ns, tun->owner));
  1399. break;
  1400. case TUNSETGROUP:
  1401. /* Set group of the device */
  1402. group = make_kgid(current_user_ns(), arg);
  1403. if (!gid_valid(group)) {
  1404. ret = -EINVAL;
  1405. break;
  1406. }
  1407. tun->group = group;
  1408. tun_debug(KERN_INFO, tun, "group set to %u\n",
  1409. from_kgid(&init_user_ns, tun->group));
  1410. break;
  1411. case TUNSETLINK:
  1412. /* Only allow setting the type when the interface is down */
  1413. if (tun->dev->flags & IFF_UP) {
  1414. tun_debug(KERN_INFO, tun,
  1415. "Linktype set failed because interface is up\n");
  1416. ret = -EBUSY;
  1417. } else {
  1418. tun->dev->type = (int) arg;
  1419. tun_debug(KERN_INFO, tun, "linktype set to %d\n",
  1420. tun->dev->type);
  1421. ret = 0;
  1422. }
  1423. break;
  1424. #ifdef TUN_DEBUG
  1425. case TUNSETDEBUG:
  1426. tun->debug = arg;
  1427. break;
  1428. #endif
  1429. case TUNSETOFFLOAD:
  1430. ret = set_offload(tun, arg);
  1431. break;
  1432. case TUNSETTXFILTER:
  1433. /* Can be set only for TAPs */
  1434. ret = -EINVAL;
  1435. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1436. break;
  1437. ret = update_filter(&tun->txflt, (void __user *)arg);
  1438. break;
  1439. case SIOCGIFHWADDR:
  1440. /* Get hw address */
  1441. memcpy(ifr.ifr_hwaddr.sa_data, tun->dev->dev_addr, ETH_ALEN);
  1442. ifr.ifr_hwaddr.sa_family = tun->dev->type;
  1443. if (copy_to_user(argp, &ifr, ifreq_len))
  1444. ret = -EFAULT;
  1445. break;
  1446. case SIOCSIFHWADDR:
  1447. /* Set hw address */
  1448. tun_debug(KERN_DEBUG, tun, "set hw address: %pM\n",
  1449. ifr.ifr_hwaddr.sa_data);
  1450. ret = dev_set_mac_address(tun->dev, &ifr.ifr_hwaddr);
  1451. break;
  1452. case TUNGETSNDBUF:
  1453. sndbuf = tfile->socket.sk->sk_sndbuf;
  1454. if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
  1455. ret = -EFAULT;
  1456. break;
  1457. case TUNSETSNDBUF:
  1458. if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
  1459. ret = -EFAULT;
  1460. break;
  1461. }
  1462. tun->sndbuf = sndbuf;
  1463. tun_set_sndbuf(tun);
  1464. break;
  1465. case TUNGETVNETHDRSZ:
  1466. vnet_hdr_sz = tun->vnet_hdr_sz;
  1467. if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
  1468. ret = -EFAULT;
  1469. break;
  1470. case TUNSETVNETHDRSZ:
  1471. if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
  1472. ret = -EFAULT;
  1473. break;
  1474. }
  1475. if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
  1476. ret = -EINVAL;
  1477. break;
  1478. }
  1479. tun->vnet_hdr_sz = vnet_hdr_sz;
  1480. break;
  1481. case TUNATTACHFILTER:
  1482. /* Can be set only for TAPs */
  1483. ret = -EINVAL;
  1484. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1485. break;
  1486. ret = -EFAULT;
  1487. if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
  1488. break;
  1489. ret = tun_attach_filter(tun);
  1490. break;
  1491. case TUNDETACHFILTER:
  1492. /* Can be set only for TAPs */
  1493. ret = -EINVAL;
  1494. if ((tun->flags & TUN_TYPE_MASK) != TUN_TAP_DEV)
  1495. break;
  1496. ret = 0;
  1497. tun_detach_filter(tun, tun->numqueues);
  1498. break;
  1499. default:
  1500. ret = -EINVAL;
  1501. break;
  1502. }
  1503. unlock:
  1504. rtnl_unlock();
  1505. if (tun)
  1506. tun_put(tun);
  1507. return ret;
  1508. }
  1509. static long tun_chr_ioctl(struct file *file,
  1510. unsigned int cmd, unsigned long arg)
  1511. {
  1512. return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
  1513. }
  1514. #ifdef CONFIG_COMPAT
  1515. static long tun_chr_compat_ioctl(struct file *file,
  1516. unsigned int cmd, unsigned long arg)
  1517. {
  1518. switch (cmd) {
  1519. case TUNSETIFF:
  1520. case TUNGETIFF:
  1521. case TUNSETTXFILTER:
  1522. case TUNGETSNDBUF:
  1523. case TUNSETSNDBUF:
  1524. case SIOCGIFHWADDR:
  1525. case SIOCSIFHWADDR:
  1526. arg = (unsigned long)compat_ptr(arg);
  1527. break;
  1528. default:
  1529. arg = (compat_ulong_t)arg;
  1530. break;
  1531. }
  1532. /*
  1533. * compat_ifreq is shorter than ifreq, so we must not access beyond
  1534. * the end of that structure. All fields that are used in this
  1535. * driver are compatible though, we don't need to convert the
  1536. * contents.
  1537. */
  1538. return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
  1539. }
  1540. #endif /* CONFIG_COMPAT */
  1541. static int tun_chr_fasync(int fd, struct file *file, int on)
  1542. {
  1543. struct tun_file *tfile = file->private_data;
  1544. int ret;
  1545. if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
  1546. goto out;
  1547. if (on) {
  1548. ret = __f_setown(file, task_pid(current), PIDTYPE_PID, 0);
  1549. if (ret)
  1550. goto out;
  1551. tfile->flags |= TUN_FASYNC;
  1552. } else
  1553. tfile->flags &= ~TUN_FASYNC;
  1554. ret = 0;
  1555. out:
  1556. return ret;
  1557. }
  1558. static int tun_chr_open(struct inode *inode, struct file * file)
  1559. {
  1560. struct tun_file *tfile;
  1561. DBG1(KERN_INFO, "tunX: tun_chr_open\n");
  1562. tfile = (struct tun_file *)sk_alloc(&init_net, AF_UNSPEC, GFP_KERNEL,
  1563. &tun_proto);
  1564. if (!tfile)
  1565. return -ENOMEM;
  1566. rcu_assign_pointer(tfile->tun, NULL);
  1567. tfile->net = get_net(current->nsproxy->net_ns);
  1568. tfile->flags = 0;
  1569. rcu_assign_pointer(tfile->socket.wq, &tfile->wq);
  1570. init_waitqueue_head(&tfile->wq.wait);
  1571. tfile->socket.file = file;
  1572. tfile->socket.ops = &tun_socket_ops;
  1573. sock_init_data(&tfile->socket, &tfile->sk);
  1574. sk_change_net(&tfile->sk, tfile->net);
  1575. tfile->sk.sk_write_space = tun_sock_write_space;
  1576. tfile->sk.sk_sndbuf = INT_MAX;
  1577. file->private_data = tfile;
  1578. set_bit(SOCK_EXTERNALLY_ALLOCATED, &tfile->socket.flags);
  1579. return 0;
  1580. }
  1581. static int tun_chr_close(struct inode *inode, struct file *file)
  1582. {
  1583. struct tun_file *tfile = file->private_data;
  1584. struct net *net = tfile->net;
  1585. tun_detach(tfile, true);
  1586. put_net(net);
  1587. return 0;
  1588. }
  1589. static const struct file_operations tun_fops = {
  1590. .owner = THIS_MODULE,
  1591. .llseek = no_llseek,
  1592. .read = do_sync_read,
  1593. .aio_read = tun_chr_aio_read,
  1594. .write = do_sync_write,
  1595. .aio_write = tun_chr_aio_write,
  1596. .poll = tun_chr_poll,
  1597. .unlocked_ioctl = tun_chr_ioctl,
  1598. #ifdef CONFIG_COMPAT
  1599. .compat_ioctl = tun_chr_compat_ioctl,
  1600. #endif
  1601. .open = tun_chr_open,
  1602. .release = tun_chr_close,
  1603. .fasync = tun_chr_fasync
  1604. };
  1605. static struct miscdevice tun_miscdev = {
  1606. .minor = TUN_MINOR,
  1607. .name = "tun",
  1608. .nodename = "net/tun",
  1609. .fops = &tun_fops,
  1610. };
  1611. /* ethtool interface */
  1612. static int tun_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1613. {
  1614. cmd->supported = 0;
  1615. cmd->advertising = 0;
  1616. ethtool_cmd_speed_set(cmd, SPEED_10);
  1617. cmd->duplex = DUPLEX_FULL;
  1618. cmd->port = PORT_TP;
  1619. cmd->phy_address = 0;
  1620. cmd->transceiver = XCVR_INTERNAL;
  1621. cmd->autoneg = AUTONEG_DISABLE;
  1622. cmd->maxtxpkt = 0;
  1623. cmd->maxrxpkt = 0;
  1624. return 0;
  1625. }
  1626. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1627. {
  1628. struct tun_struct *tun = netdev_priv(dev);
  1629. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1630. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1631. switch (tun->flags & TUN_TYPE_MASK) {
  1632. case TUN_TUN_DEV:
  1633. strlcpy(info->bus_info, "tun", sizeof(info->bus_info));
  1634. break;
  1635. case TUN_TAP_DEV:
  1636. strlcpy(info->bus_info, "tap", sizeof(info->bus_info));
  1637. break;
  1638. }
  1639. }
  1640. static u32 tun_get_msglevel(struct net_device *dev)
  1641. {
  1642. #ifdef TUN_DEBUG
  1643. struct tun_struct *tun = netdev_priv(dev);
  1644. return tun->debug;
  1645. #else
  1646. return -EOPNOTSUPP;
  1647. #endif
  1648. }
  1649. static void tun_set_msglevel(struct net_device *dev, u32 value)
  1650. {
  1651. #ifdef TUN_DEBUG
  1652. struct tun_struct *tun = netdev_priv(dev);
  1653. tun->debug = value;
  1654. #endif
  1655. }
  1656. static const struct ethtool_ops tun_ethtool_ops = {
  1657. .get_settings = tun_get_settings,
  1658. .get_drvinfo = tun_get_drvinfo,
  1659. .get_msglevel = tun_get_msglevel,
  1660. .set_msglevel = tun_set_msglevel,
  1661. .get_link = ethtool_op_get_link,
  1662. };
  1663. static int __init tun_init(void)
  1664. {
  1665. int ret = 0;
  1666. pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  1667. pr_info("%s\n", DRV_COPYRIGHT);
  1668. ret = rtnl_link_register(&tun_link_ops);
  1669. if (ret) {
  1670. pr_err("Can't register link_ops\n");
  1671. goto err_linkops;
  1672. }
  1673. ret = misc_register(&tun_miscdev);
  1674. if (ret) {
  1675. pr_err("Can't register misc device %d\n", TUN_MINOR);
  1676. goto err_misc;
  1677. }
  1678. return 0;
  1679. err_misc:
  1680. rtnl_link_unregister(&tun_link_ops);
  1681. err_linkops:
  1682. return ret;
  1683. }
  1684. static void tun_cleanup(void)
  1685. {
  1686. misc_deregister(&tun_miscdev);
  1687. rtnl_link_unregister(&tun_link_ops);
  1688. }
  1689. /* Get an underlying socket object from tun file. Returns error unless file is
  1690. * attached to a device. The returned object works like a packet socket, it
  1691. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1692. * holding a reference to the file for as long as the socket is in use. */
  1693. struct socket *tun_get_socket(struct file *file)
  1694. {
  1695. struct tun_file *tfile;
  1696. if (file->f_op != &tun_fops)
  1697. return ERR_PTR(-EINVAL);
  1698. tfile = file->private_data;
  1699. if (!tfile)
  1700. return ERR_PTR(-EBADFD);
  1701. return &tfile->socket;
  1702. }
  1703. EXPORT_SYMBOL_GPL(tun_get_socket);
  1704. module_init(tun_init);
  1705. module_exit(tun_cleanup);
  1706. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  1707. MODULE_AUTHOR(DRV_COPYRIGHT);
  1708. MODULE_LICENSE("GPL");
  1709. MODULE_ALIAS_MISCDEV(TUN_MINOR);
  1710. MODULE_ALIAS("devname:net/tun");