tun.c 56 KB

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