tun.c 56 KB

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