tun.c 54 KB

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