tun.c 55 KB

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