tun.c 54 KB

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