tun.c 55 KB

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