tun.c 59 KB

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