tun.c 53 KB

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