tun.c 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * TUN - Universal TUN/TAP device driver.
  4. * Copyright (C) 1999-2002 Maxim Krasnyansky <[email protected]>
  5. *
  6. * $Id: tun.c,v 1.15 2002/03/01 02:44:24 maxk Exp $
  7. */
  8. /*
  9. * Changes:
  10. *
  11. * Mike Kershaw <[email protected]> 2005/08/14
  12. * Add TUNSETLINK ioctl to set the link encapsulation
  13. *
  14. * Mark Smith <[email protected]>
  15. * Use eth_random_addr() for tap MAC address.
  16. *
  17. * Harald Roelle <[email protected]> 2004/04/20
  18. * Fixes in packet dropping, queue length setting and queue wakeup.
  19. * Increased default tx queue length.
  20. * Added ethtool API.
  21. * Minor cleanups
  22. *
  23. * Daniel Podlejski <[email protected]>
  24. * Modifications for 2.3.99-pre5 kernel.
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #define DRV_NAME "tun"
  28. #define DRV_VERSION "1.6"
  29. #define DRV_DESCRIPTION "Universal TUN/TAP device driver"
  30. #define DRV_COPYRIGHT "(C) 1999-2004 Max Krasnyansky <[email protected]>"
  31. #include <linux/module.h>
  32. #include <linux/errno.h>
  33. #include <linux/kernel.h>
  34. #include <linux/sched/signal.h>
  35. #include <linux/major.h>
  36. #include <linux/slab.h>
  37. #include <linux/poll.h>
  38. #include <linux/fcntl.h>
  39. #include <linux/init.h>
  40. #include <linux/skbuff.h>
  41. #include <linux/netdevice.h>
  42. #include <linux/etherdevice.h>
  43. #include <linux/miscdevice.h>
  44. #include <linux/ethtool.h>
  45. #include <linux/rtnetlink.h>
  46. #include <linux/compat.h>
  47. #include <linux/if.h>
  48. #include <linux/if_arp.h>
  49. #include <linux/if_ether.h>
  50. #include <linux/if_tun.h>
  51. #include <linux/if_vlan.h>
  52. #include <linux/crc32.h>
  53. #include <linux/nsproxy.h>
  54. #include <linux/virtio_net.h>
  55. #include <linux/rcupdate.h>
  56. #include <net/net_namespace.h>
  57. #include <net/netns/generic.h>
  58. #include <net/rtnetlink.h>
  59. #include <net/sock.h>
  60. #include <net/xdp.h>
  61. #include <net/ip_tunnels.h>
  62. #include <linux/seq_file.h>
  63. #include <linux/uio.h>
  64. #include <linux/skb_array.h>
  65. #include <linux/bpf.h>
  66. #include <linux/bpf_trace.h>
  67. #include <linux/mutex.h>
  68. #include <linux/ieee802154.h>
  69. #include <linux/if_ltalk.h>
  70. #include <uapi/linux/if_fddi.h>
  71. #include <uapi/linux/if_hippi.h>
  72. #include <uapi/linux/if_fc.h>
  73. #include <net/ax25.h>
  74. #include <net/rose.h>
  75. #include <net/6lowpan.h>
  76. #include <linux/uaccess.h>
  77. #include <linux/proc_fs.h>
  78. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN {
  79. #ifdef CONFIG_KNOX_NCM
  80. #include <linux/types.h>
  81. #include <linux/udp.h>
  82. #include <linux/tcp.h>
  83. #include <linux/ip.h>
  84. #include <net/ip.h>
  85. #define META_MARK_BASE_LOWER 100
  86. #define META_MARK_BASE_UPPER 500
  87. #endif
  88. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN }
  89. static void tun_default_link_ksettings(struct net_device *dev,
  90. struct ethtool_link_ksettings *cmd);
  91. #define TUN_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
  92. /* TUN device flags */
  93. /* IFF_ATTACH_QUEUE is never stored in device flags,
  94. * overload it to mean fasync when stored there.
  95. */
  96. #define TUN_FASYNC IFF_ATTACH_QUEUE
  97. /* High bits in flags field are unused. */
  98. #define TUN_VNET_LE 0x80000000
  99. #define TUN_VNET_BE 0x40000000
  100. #define TUN_FEATURES (IFF_NO_PI | IFF_ONE_QUEUE | IFF_VNET_HDR | \
  101. IFF_MULTI_QUEUE | IFF_NAPI | IFF_NAPI_FRAGS)
  102. #define GOODCOPY_LEN 128
  103. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN {
  104. #ifdef CONFIG_KNOX_NCM
  105. struct knox_meta_param {
  106. uid_t uid;
  107. pid_t pid;
  108. };
  109. #define TUN_META_HDR_SZ sizeof(struct knox_meta_param)
  110. #define TUN_META_MARK_OFFSET offsetof(struct knox_meta_param, uid)
  111. #endif
  112. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN }
  113. #define FLT_EXACT_COUNT 8
  114. struct tap_filter {
  115. unsigned int count; /* Number of addrs. Zero means disabled */
  116. u32 mask[2]; /* Mask of the hashed addrs */
  117. unsigned char addr[FLT_EXACT_COUNT][ETH_ALEN];
  118. };
  119. /* MAX_TAP_QUEUES 256 is chosen to allow rx/tx queues to be equal
  120. * to max number of VCPUs in guest. */
  121. #define MAX_TAP_QUEUES 256
  122. #define MAX_TAP_FLOWS 4096
  123. #define TUN_FLOW_EXPIRE (3 * HZ)
  124. /* A tun_file connects an open character device to a tuntap netdevice. It
  125. * also contains all socket related structures (except sock_fprog and tap_filter)
  126. * to serve as one transmit queue for tuntap device. The sock_fprog and
  127. * tap_filter were kept in tun_struct since they were used for filtering for the
  128. * netdevice not for a specific queue (at least I didn't see the requirement for
  129. * this).
  130. *
  131. * RCU usage:
  132. * The tun_file and tun_struct are loosely coupled, the pointer from one to the
  133. * other can only be read while rcu_read_lock or rtnl_lock is held.
  134. */
  135. struct tun_file {
  136. struct sock sk;
  137. struct socket socket;
  138. struct tun_struct __rcu *tun;
  139. struct fasync_struct *fasync;
  140. /* only used for fasnyc */
  141. unsigned int flags;
  142. union {
  143. u16 queue_index;
  144. unsigned int ifindex;
  145. };
  146. struct napi_struct napi;
  147. bool napi_enabled;
  148. bool napi_frags_enabled;
  149. struct mutex napi_mutex; /* Protects access to the above napi */
  150. struct list_head next;
  151. struct tun_struct *detached;
  152. struct ptr_ring tx_ring;
  153. struct xdp_rxq_info xdp_rxq;
  154. };
  155. struct tun_page {
  156. struct page *page;
  157. int count;
  158. };
  159. struct tun_flow_entry {
  160. struct hlist_node hash_link;
  161. struct rcu_head rcu;
  162. struct tun_struct *tun;
  163. u32 rxhash;
  164. u32 rps_rxhash;
  165. int queue_index;
  166. unsigned long updated ____cacheline_aligned_in_smp;
  167. };
  168. #define TUN_NUM_FLOW_ENTRIES 1024
  169. #define TUN_MASK_FLOW_ENTRIES (TUN_NUM_FLOW_ENTRIES - 1)
  170. struct tun_prog {
  171. struct rcu_head rcu;
  172. struct bpf_prog *prog;
  173. };
  174. /* Since the socket were moved to tun_file, to preserve the behavior of persist
  175. * device, socket filter, sndbuf and vnet header size were restore when the
  176. * file were attached to a persist device.
  177. */
  178. struct tun_struct {
  179. struct tun_file __rcu *tfiles[MAX_TAP_QUEUES];
  180. unsigned int numqueues;
  181. unsigned int flags;
  182. kuid_t owner;
  183. kgid_t group;
  184. struct net_device *dev;
  185. netdev_features_t set_features;
  186. #define TUN_USER_FEATURES (NETIF_F_HW_CSUM|NETIF_F_TSO_ECN|NETIF_F_TSO| \
  187. NETIF_F_TSO6)
  188. int align;
  189. int vnet_hdr_sz;
  190. int sndbuf;
  191. struct tap_filter txflt;
  192. struct sock_fprog fprog;
  193. /* protected by rtnl lock */
  194. bool filter_attached;
  195. u32 msg_enable;
  196. spinlock_t lock;
  197. struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
  198. struct timer_list flow_gc_timer;
  199. unsigned long ageing_time;
  200. unsigned int numdisabled;
  201. struct list_head disabled;
  202. void *security;
  203. u32 flow_count;
  204. u32 rx_batched;
  205. atomic_long_t rx_frame_errors;
  206. struct bpf_prog __rcu *xdp_prog;
  207. struct tun_prog __rcu *steering_prog;
  208. struct tun_prog __rcu *filter_prog;
  209. struct ethtool_link_ksettings link_ksettings;
  210. /* init args */
  211. struct file *file;
  212. struct ifreq *ifr;
  213. };
  214. struct veth {
  215. __be16 h_vlan_proto;
  216. __be16 h_vlan_TCI;
  217. };
  218. static void tun_flow_init(struct tun_struct *tun);
  219. static void tun_flow_uninit(struct tun_struct *tun);
  220. static int tun_napi_receive(struct napi_struct *napi, int budget)
  221. {
  222. struct tun_file *tfile = container_of(napi, struct tun_file, napi);
  223. struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
  224. struct sk_buff_head process_queue;
  225. struct sk_buff *skb;
  226. int received = 0;
  227. __skb_queue_head_init(&process_queue);
  228. spin_lock(&queue->lock);
  229. skb_queue_splice_tail_init(queue, &process_queue);
  230. spin_unlock(&queue->lock);
  231. while (received < budget && (skb = __skb_dequeue(&process_queue))) {
  232. napi_gro_receive(napi, skb);
  233. ++received;
  234. }
  235. if (!skb_queue_empty(&process_queue)) {
  236. spin_lock(&queue->lock);
  237. skb_queue_splice(&process_queue, queue);
  238. spin_unlock(&queue->lock);
  239. }
  240. return received;
  241. }
  242. static int tun_napi_poll(struct napi_struct *napi, int budget)
  243. {
  244. unsigned int received;
  245. received = tun_napi_receive(napi, budget);
  246. if (received < budget)
  247. napi_complete_done(napi, received);
  248. return received;
  249. }
  250. static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile,
  251. bool napi_en, bool napi_frags)
  252. {
  253. tfile->napi_enabled = napi_en;
  254. tfile->napi_frags_enabled = napi_en && napi_frags;
  255. if (napi_en) {
  256. netif_napi_add_tx(tun->dev, &tfile->napi, tun_napi_poll);
  257. napi_enable(&tfile->napi);
  258. }
  259. }
  260. static void tun_napi_enable(struct tun_file *tfile)
  261. {
  262. if (tfile->napi_enabled)
  263. napi_enable(&tfile->napi);
  264. }
  265. static void tun_napi_disable(struct tun_file *tfile)
  266. {
  267. if (tfile->napi_enabled)
  268. napi_disable(&tfile->napi);
  269. }
  270. static void tun_napi_del(struct tun_file *tfile)
  271. {
  272. if (tfile->napi_enabled)
  273. netif_napi_del(&tfile->napi);
  274. }
  275. static bool tun_napi_frags_enabled(const struct tun_file *tfile)
  276. {
  277. return tfile->napi_frags_enabled;
  278. }
  279. #ifdef CONFIG_TUN_VNET_CROSS_LE
  280. static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
  281. {
  282. return tun->flags & TUN_VNET_BE ? false :
  283. virtio_legacy_is_little_endian();
  284. }
  285. static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
  286. {
  287. int be = !!(tun->flags & TUN_VNET_BE);
  288. if (put_user(be, argp))
  289. return -EFAULT;
  290. return 0;
  291. }
  292. static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
  293. {
  294. int be;
  295. if (get_user(be, argp))
  296. return -EFAULT;
  297. if (be)
  298. tun->flags |= TUN_VNET_BE;
  299. else
  300. tun->flags &= ~TUN_VNET_BE;
  301. return 0;
  302. }
  303. #else
  304. static inline bool tun_legacy_is_little_endian(struct tun_struct *tun)
  305. {
  306. return virtio_legacy_is_little_endian();
  307. }
  308. static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)
  309. {
  310. return -EINVAL;
  311. }
  312. static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)
  313. {
  314. return -EINVAL;
  315. }
  316. #endif /* CONFIG_TUN_VNET_CROSS_LE */
  317. static inline bool tun_is_little_endian(struct tun_struct *tun)
  318. {
  319. return tun->flags & TUN_VNET_LE ||
  320. tun_legacy_is_little_endian(tun);
  321. }
  322. static inline u16 tun16_to_cpu(struct tun_struct *tun, __virtio16 val)
  323. {
  324. return __virtio16_to_cpu(tun_is_little_endian(tun), val);
  325. }
  326. static inline __virtio16 cpu_to_tun16(struct tun_struct *tun, u16 val)
  327. {
  328. return __cpu_to_virtio16(tun_is_little_endian(tun), val);
  329. }
  330. static inline u32 tun_hashfn(u32 rxhash)
  331. {
  332. return rxhash & TUN_MASK_FLOW_ENTRIES;
  333. }
  334. static struct tun_flow_entry *tun_flow_find(struct hlist_head *head, u32 rxhash)
  335. {
  336. struct tun_flow_entry *e;
  337. hlist_for_each_entry_rcu(e, head, hash_link) {
  338. if (e->rxhash == rxhash)
  339. return e;
  340. }
  341. return NULL;
  342. }
  343. static struct tun_flow_entry *tun_flow_create(struct tun_struct *tun,
  344. struct hlist_head *head,
  345. u32 rxhash, u16 queue_index)
  346. {
  347. struct tun_flow_entry *e = kmalloc(sizeof(*e), GFP_ATOMIC);
  348. if (e) {
  349. netif_info(tun, tx_queued, tun->dev,
  350. "create flow: hash %u index %u\n",
  351. rxhash, queue_index);
  352. e->updated = jiffies;
  353. e->rxhash = rxhash;
  354. e->rps_rxhash = 0;
  355. e->queue_index = queue_index;
  356. e->tun = tun;
  357. hlist_add_head_rcu(&e->hash_link, head);
  358. ++tun->flow_count;
  359. }
  360. return e;
  361. }
  362. static void tun_flow_delete(struct tun_struct *tun, struct tun_flow_entry *e)
  363. {
  364. netif_info(tun, tx_queued, tun->dev, "delete flow: hash %u index %u\n",
  365. e->rxhash, e->queue_index);
  366. hlist_del_rcu(&e->hash_link);
  367. kfree_rcu(e, rcu);
  368. --tun->flow_count;
  369. }
  370. static void tun_flow_flush(struct tun_struct *tun)
  371. {
  372. int i;
  373. spin_lock_bh(&tun->lock);
  374. for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
  375. struct tun_flow_entry *e;
  376. struct hlist_node *n;
  377. hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link)
  378. tun_flow_delete(tun, e);
  379. }
  380. spin_unlock_bh(&tun->lock);
  381. }
  382. static void tun_flow_delete_by_queue(struct tun_struct *tun, u16 queue_index)
  383. {
  384. int i;
  385. spin_lock_bh(&tun->lock);
  386. for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
  387. struct tun_flow_entry *e;
  388. struct hlist_node *n;
  389. hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
  390. if (e->queue_index == queue_index)
  391. tun_flow_delete(tun, e);
  392. }
  393. }
  394. spin_unlock_bh(&tun->lock);
  395. }
  396. static void tun_flow_cleanup(struct timer_list *t)
  397. {
  398. struct tun_struct *tun = from_timer(tun, t, flow_gc_timer);
  399. unsigned long delay = tun->ageing_time;
  400. unsigned long next_timer = jiffies + delay;
  401. unsigned long count = 0;
  402. int i;
  403. spin_lock(&tun->lock);
  404. for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++) {
  405. struct tun_flow_entry *e;
  406. struct hlist_node *n;
  407. hlist_for_each_entry_safe(e, n, &tun->flows[i], hash_link) {
  408. unsigned long this_timer;
  409. this_timer = e->updated + delay;
  410. if (time_before_eq(this_timer, jiffies)) {
  411. tun_flow_delete(tun, e);
  412. continue;
  413. }
  414. count++;
  415. if (time_before(this_timer, next_timer))
  416. next_timer = this_timer;
  417. }
  418. }
  419. if (count)
  420. mod_timer(&tun->flow_gc_timer, round_jiffies_up(next_timer));
  421. spin_unlock(&tun->lock);
  422. }
  423. static void tun_flow_update(struct tun_struct *tun, u32 rxhash,
  424. struct tun_file *tfile)
  425. {
  426. struct hlist_head *head;
  427. struct tun_flow_entry *e;
  428. unsigned long delay = tun->ageing_time;
  429. u16 queue_index = tfile->queue_index;
  430. head = &tun->flows[tun_hashfn(rxhash)];
  431. rcu_read_lock();
  432. e = tun_flow_find(head, rxhash);
  433. if (likely(e)) {
  434. /* TODO: keep queueing to old queue until it's empty? */
  435. if (READ_ONCE(e->queue_index) != queue_index)
  436. WRITE_ONCE(e->queue_index, queue_index);
  437. if (e->updated != jiffies)
  438. e->updated = jiffies;
  439. sock_rps_record_flow_hash(e->rps_rxhash);
  440. } else {
  441. spin_lock_bh(&tun->lock);
  442. if (!tun_flow_find(head, rxhash) &&
  443. tun->flow_count < MAX_TAP_FLOWS)
  444. tun_flow_create(tun, head, rxhash, queue_index);
  445. if (!timer_pending(&tun->flow_gc_timer))
  446. mod_timer(&tun->flow_gc_timer,
  447. round_jiffies_up(jiffies + delay));
  448. spin_unlock_bh(&tun->lock);
  449. }
  450. rcu_read_unlock();
  451. }
  452. /* Save the hash received in the stack receive path and update the
  453. * flow_hash table accordingly.
  454. */
  455. static inline void tun_flow_save_rps_rxhash(struct tun_flow_entry *e, u32 hash)
  456. {
  457. if (unlikely(e->rps_rxhash != hash))
  458. e->rps_rxhash = hash;
  459. }
  460. /* We try to identify a flow through its rxhash. The reason that
  461. * we do not check rxq no. is because some cards(e.g 82599), chooses
  462. * the rxq based on the txq where the last packet of the flow comes. As
  463. * the userspace application move between processors, we may get a
  464. * different rxq no. here.
  465. */
  466. static u16 tun_automq_select_queue(struct tun_struct *tun, struct sk_buff *skb)
  467. {
  468. struct tun_flow_entry *e;
  469. u32 txq = 0;
  470. u32 numqueues = 0;
  471. numqueues = READ_ONCE(tun->numqueues);
  472. txq = __skb_get_hash_symmetric(skb);
  473. e = tun_flow_find(&tun->flows[tun_hashfn(txq)], txq);
  474. if (e) {
  475. tun_flow_save_rps_rxhash(e, txq);
  476. txq = e->queue_index;
  477. } else {
  478. /* use multiply and shift instead of expensive divide */
  479. txq = ((u64)txq * numqueues) >> 32;
  480. }
  481. return txq;
  482. }
  483. static u16 tun_ebpf_select_queue(struct tun_struct *tun, struct sk_buff *skb)
  484. {
  485. struct tun_prog *prog;
  486. u32 numqueues;
  487. u16 ret = 0;
  488. numqueues = READ_ONCE(tun->numqueues);
  489. if (!numqueues)
  490. return 0;
  491. prog = rcu_dereference(tun->steering_prog);
  492. if (prog)
  493. ret = bpf_prog_run_clear_cb(prog->prog, skb);
  494. return ret % numqueues;
  495. }
  496. static u16 tun_select_queue(struct net_device *dev, struct sk_buff *skb,
  497. struct net_device *sb_dev)
  498. {
  499. struct tun_struct *tun = netdev_priv(dev);
  500. u16 ret;
  501. rcu_read_lock();
  502. if (rcu_dereference(tun->steering_prog))
  503. ret = tun_ebpf_select_queue(tun, skb);
  504. else
  505. ret = tun_automq_select_queue(tun, skb);
  506. rcu_read_unlock();
  507. return ret;
  508. }
  509. static inline bool tun_not_capable(struct tun_struct *tun)
  510. {
  511. const struct cred *cred = current_cred();
  512. struct net *net = dev_net(tun->dev);
  513. return ((uid_valid(tun->owner) && !uid_eq(cred->euid, tun->owner)) ||
  514. (gid_valid(tun->group) && !in_egroup_p(tun->group))) &&
  515. !ns_capable(net->user_ns, CAP_NET_ADMIN);
  516. }
  517. static void tun_set_real_num_queues(struct tun_struct *tun)
  518. {
  519. netif_set_real_num_tx_queues(tun->dev, tun->numqueues);
  520. netif_set_real_num_rx_queues(tun->dev, tun->numqueues);
  521. }
  522. static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile)
  523. {
  524. tfile->detached = tun;
  525. list_add_tail(&tfile->next, &tun->disabled);
  526. ++tun->numdisabled;
  527. }
  528. static struct tun_struct *tun_enable_queue(struct tun_file *tfile)
  529. {
  530. struct tun_struct *tun = tfile->detached;
  531. tfile->detached = NULL;
  532. list_del_init(&tfile->next);
  533. --tun->numdisabled;
  534. return tun;
  535. }
  536. void tun_ptr_free(void *ptr)
  537. {
  538. if (!ptr)
  539. return;
  540. if (tun_is_xdp_frame(ptr)) {
  541. struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
  542. xdp_return_frame(xdpf);
  543. } else {
  544. __skb_array_destroy_skb(ptr);
  545. }
  546. }
  547. EXPORT_SYMBOL_GPL(tun_ptr_free);
  548. static void tun_queue_purge(struct tun_file *tfile)
  549. {
  550. void *ptr;
  551. while ((ptr = ptr_ring_consume(&tfile->tx_ring)) != NULL)
  552. tun_ptr_free(ptr);
  553. skb_queue_purge(&tfile->sk.sk_write_queue);
  554. skb_queue_purge(&tfile->sk.sk_error_queue);
  555. }
  556. static void __tun_detach(struct tun_file *tfile, bool clean)
  557. {
  558. struct tun_file *ntfile;
  559. struct tun_struct *tun;
  560. tun = rtnl_dereference(tfile->tun);
  561. if (tun && clean) {
  562. if (!tfile->detached)
  563. tun_napi_disable(tfile);
  564. tun_napi_del(tfile);
  565. }
  566. if (tun && !tfile->detached) {
  567. u16 index = tfile->queue_index;
  568. BUG_ON(index >= tun->numqueues);
  569. rcu_assign_pointer(tun->tfiles[index],
  570. tun->tfiles[tun->numqueues - 1]);
  571. ntfile = rtnl_dereference(tun->tfiles[index]);
  572. ntfile->queue_index = index;
  573. rcu_assign_pointer(tun->tfiles[tun->numqueues - 1],
  574. NULL);
  575. --tun->numqueues;
  576. if (clean) {
  577. RCU_INIT_POINTER(tfile->tun, NULL);
  578. sock_put(&tfile->sk);
  579. } else {
  580. tun_disable_queue(tun, tfile);
  581. tun_napi_disable(tfile);
  582. }
  583. synchronize_net();
  584. tun_flow_delete_by_queue(tun, tun->numqueues + 1);
  585. /* Drop read queue */
  586. tun_queue_purge(tfile);
  587. tun_set_real_num_queues(tun);
  588. } else if (tfile->detached && clean) {
  589. tun = tun_enable_queue(tfile);
  590. sock_put(&tfile->sk);
  591. }
  592. if (clean) {
  593. if (tun && tun->numqueues == 0 && tun->numdisabled == 0) {
  594. netif_carrier_off(tun->dev);
  595. if (!(tun->flags & IFF_PERSIST) &&
  596. tun->dev->reg_state == NETREG_REGISTERED)
  597. unregister_netdevice(tun->dev);
  598. }
  599. if (tun)
  600. xdp_rxq_info_unreg(&tfile->xdp_rxq);
  601. ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free);
  602. }
  603. }
  604. static void tun_detach(struct tun_file *tfile, bool clean)
  605. {
  606. struct tun_struct *tun;
  607. struct net_device *dev;
  608. rtnl_lock();
  609. tun = rtnl_dereference(tfile->tun);
  610. dev = tun ? tun->dev : NULL;
  611. __tun_detach(tfile, clean);
  612. if (dev)
  613. netdev_state_change(dev);
  614. rtnl_unlock();
  615. if (clean)
  616. sock_put(&tfile->sk);
  617. }
  618. static void tun_detach_all(struct net_device *dev)
  619. {
  620. struct tun_struct *tun = netdev_priv(dev);
  621. struct tun_file *tfile, *tmp;
  622. int i, n = tun->numqueues;
  623. for (i = 0; i < n; i++) {
  624. tfile = rtnl_dereference(tun->tfiles[i]);
  625. BUG_ON(!tfile);
  626. tun_napi_disable(tfile);
  627. tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
  628. tfile->socket.sk->sk_data_ready(tfile->socket.sk);
  629. RCU_INIT_POINTER(tfile->tun, NULL);
  630. --tun->numqueues;
  631. }
  632. list_for_each_entry(tfile, &tun->disabled, next) {
  633. tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN;
  634. tfile->socket.sk->sk_data_ready(tfile->socket.sk);
  635. RCU_INIT_POINTER(tfile->tun, NULL);
  636. }
  637. BUG_ON(tun->numqueues != 0);
  638. synchronize_net();
  639. for (i = 0; i < n; i++) {
  640. tfile = rtnl_dereference(tun->tfiles[i]);
  641. tun_napi_del(tfile);
  642. /* Drop read queue */
  643. tun_queue_purge(tfile);
  644. xdp_rxq_info_unreg(&tfile->xdp_rxq);
  645. sock_put(&tfile->sk);
  646. }
  647. list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
  648. tun_napi_del(tfile);
  649. tun_enable_queue(tfile);
  650. tun_queue_purge(tfile);
  651. xdp_rxq_info_unreg(&tfile->xdp_rxq);
  652. sock_put(&tfile->sk);
  653. }
  654. BUG_ON(tun->numdisabled != 0);
  655. if (tun->flags & IFF_PERSIST)
  656. module_put(THIS_MODULE);
  657. }
  658. static int tun_attach(struct tun_struct *tun, struct file *file,
  659. bool skip_filter, bool napi, bool napi_frags,
  660. bool publish_tun)
  661. {
  662. struct tun_file *tfile = file->private_data;
  663. struct net_device *dev = tun->dev;
  664. int err;
  665. err = security_tun_dev_attach(tfile->socket.sk, tun->security);
  666. if (err < 0)
  667. goto out;
  668. err = -EINVAL;
  669. if (rtnl_dereference(tfile->tun) && !tfile->detached)
  670. goto out;
  671. err = -EBUSY;
  672. if (!(tun->flags & IFF_MULTI_QUEUE) && tun->numqueues == 1)
  673. goto out;
  674. err = -E2BIG;
  675. if (!tfile->detached &&
  676. tun->numqueues + tun->numdisabled == MAX_TAP_QUEUES)
  677. goto out;
  678. err = 0;
  679. /* Re-attach the filter to persist device */
  680. if (!skip_filter && (tun->filter_attached == true)) {
  681. lock_sock(tfile->socket.sk);
  682. err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
  683. release_sock(tfile->socket.sk);
  684. if (!err)
  685. goto out;
  686. }
  687. if (!tfile->detached &&
  688. ptr_ring_resize(&tfile->tx_ring, dev->tx_queue_len,
  689. GFP_KERNEL, tun_ptr_free)) {
  690. err = -ENOMEM;
  691. goto out;
  692. }
  693. tfile->queue_index = tun->numqueues;
  694. tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
  695. if (tfile->detached) {
  696. /* Re-attach detached tfile, updating XDP queue_index */
  697. WARN_ON(!xdp_rxq_info_is_reg(&tfile->xdp_rxq));
  698. if (tfile->xdp_rxq.queue_index != tfile->queue_index)
  699. tfile->xdp_rxq.queue_index = tfile->queue_index;
  700. } else {
  701. /* Setup XDP RX-queue info, for new tfile getting attached */
  702. err = xdp_rxq_info_reg(&tfile->xdp_rxq,
  703. tun->dev, tfile->queue_index, 0);
  704. if (err < 0)
  705. goto out;
  706. err = xdp_rxq_info_reg_mem_model(&tfile->xdp_rxq,
  707. MEM_TYPE_PAGE_SHARED, NULL);
  708. if (err < 0) {
  709. xdp_rxq_info_unreg(&tfile->xdp_rxq);
  710. goto out;
  711. }
  712. err = 0;
  713. }
  714. if (tfile->detached) {
  715. tun_enable_queue(tfile);
  716. tun_napi_enable(tfile);
  717. } else {
  718. sock_hold(&tfile->sk);
  719. tun_napi_init(tun, tfile, napi, napi_frags);
  720. }
  721. if (rtnl_dereference(tun->xdp_prog))
  722. sock_set_flag(&tfile->sk, SOCK_XDP);
  723. /* device is allowed to go away first, so no need to hold extra
  724. * refcnt.
  725. */
  726. /* Publish tfile->tun and tun->tfiles only after we've fully
  727. * initialized tfile; otherwise we risk using half-initialized
  728. * object.
  729. */
  730. if (publish_tun)
  731. rcu_assign_pointer(tfile->tun, tun);
  732. rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
  733. tun->numqueues++;
  734. tun_set_real_num_queues(tun);
  735. out:
  736. return err;
  737. }
  738. static struct tun_struct *tun_get(struct tun_file *tfile)
  739. {
  740. struct tun_struct *tun;
  741. rcu_read_lock();
  742. tun = rcu_dereference(tfile->tun);
  743. if (tun)
  744. dev_hold(tun->dev);
  745. rcu_read_unlock();
  746. return tun;
  747. }
  748. static void tun_put(struct tun_struct *tun)
  749. {
  750. dev_put(tun->dev);
  751. }
  752. /* TAP filtering */
  753. static void addr_hash_set(u32 *mask, const u8 *addr)
  754. {
  755. int n = ether_crc(ETH_ALEN, addr) >> 26;
  756. mask[n >> 5] |= (1 << (n & 31));
  757. }
  758. static unsigned int addr_hash_test(const u32 *mask, const u8 *addr)
  759. {
  760. int n = ether_crc(ETH_ALEN, addr) >> 26;
  761. return mask[n >> 5] & (1 << (n & 31));
  762. }
  763. static int update_filter(struct tap_filter *filter, void __user *arg)
  764. {
  765. struct { u8 u[ETH_ALEN]; } *addr;
  766. struct tun_filter uf;
  767. int err, alen, n, nexact;
  768. if (copy_from_user(&uf, arg, sizeof(uf)))
  769. return -EFAULT;
  770. if (!uf.count) {
  771. /* Disabled */
  772. filter->count = 0;
  773. return 0;
  774. }
  775. alen = ETH_ALEN * uf.count;
  776. addr = memdup_user(arg + sizeof(uf), alen);
  777. if (IS_ERR(addr))
  778. return PTR_ERR(addr);
  779. /* The filter is updated without holding any locks. Which is
  780. * perfectly safe. We disable it first and in the worst
  781. * case we'll accept a few undesired packets. */
  782. filter->count = 0;
  783. wmb();
  784. /* Use first set of addresses as an exact filter */
  785. for (n = 0; n < uf.count && n < FLT_EXACT_COUNT; n++)
  786. memcpy(filter->addr[n], addr[n].u, ETH_ALEN);
  787. nexact = n;
  788. /* Remaining multicast addresses are hashed,
  789. * unicast will leave the filter disabled. */
  790. memset(filter->mask, 0, sizeof(filter->mask));
  791. for (; n < uf.count; n++) {
  792. if (!is_multicast_ether_addr(addr[n].u)) {
  793. err = 0; /* no filter */
  794. goto free_addr;
  795. }
  796. addr_hash_set(filter->mask, addr[n].u);
  797. }
  798. /* For ALLMULTI just set the mask to all ones.
  799. * This overrides the mask populated above. */
  800. if ((uf.flags & TUN_FLT_ALLMULTI))
  801. memset(filter->mask, ~0, sizeof(filter->mask));
  802. /* Now enable the filter */
  803. wmb();
  804. filter->count = nexact;
  805. /* Return the number of exact filters */
  806. err = nexact;
  807. free_addr:
  808. kfree(addr);
  809. return err;
  810. }
  811. /* Returns: 0 - drop, !=0 - accept */
  812. static int run_filter(struct tap_filter *filter, const struct sk_buff *skb)
  813. {
  814. /* Cannot use eth_hdr(skb) here because skb_mac_hdr() is incorrect
  815. * at this point. */
  816. struct ethhdr *eh = (struct ethhdr *) skb->data;
  817. int i;
  818. /* Exact match */
  819. for (i = 0; i < filter->count; i++)
  820. if (ether_addr_equal(eh->h_dest, filter->addr[i]))
  821. return 1;
  822. /* Inexact match (multicast only) */
  823. if (is_multicast_ether_addr(eh->h_dest))
  824. return addr_hash_test(filter->mask, eh->h_dest);
  825. return 0;
  826. }
  827. /*
  828. * Checks whether the packet is accepted or not.
  829. * Returns: 0 - drop, !=0 - accept
  830. */
  831. static int check_filter(struct tap_filter *filter, const struct sk_buff *skb)
  832. {
  833. if (!filter->count)
  834. return 1;
  835. return run_filter(filter, skb);
  836. }
  837. /* Network device part of the driver */
  838. static const struct ethtool_ops tun_ethtool_ops;
  839. static int tun_net_init(struct net_device *dev)
  840. {
  841. struct tun_struct *tun = netdev_priv(dev);
  842. struct ifreq *ifr = tun->ifr;
  843. int err;
  844. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  845. if (!dev->tstats)
  846. return -ENOMEM;
  847. spin_lock_init(&tun->lock);
  848. err = security_tun_dev_alloc_security(&tun->security);
  849. if (err < 0) {
  850. free_percpu(dev->tstats);
  851. return err;
  852. }
  853. tun_flow_init(tun);
  854. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
  855. TUN_USER_FEATURES | NETIF_F_HW_VLAN_CTAG_TX |
  856. NETIF_F_HW_VLAN_STAG_TX;
  857. dev->features = dev->hw_features | NETIF_F_LLTX;
  858. dev->vlan_features = dev->features &
  859. ~(NETIF_F_HW_VLAN_CTAG_TX |
  860. NETIF_F_HW_VLAN_STAG_TX);
  861. tun->flags = (tun->flags & ~TUN_FEATURES) |
  862. (ifr->ifr_flags & TUN_FEATURES);
  863. INIT_LIST_HEAD(&tun->disabled);
  864. err = tun_attach(tun, tun->file, false, ifr->ifr_flags & IFF_NAPI,
  865. ifr->ifr_flags & IFF_NAPI_FRAGS, false);
  866. if (err < 0) {
  867. tun_flow_uninit(tun);
  868. security_tun_dev_free_security(tun->security);
  869. free_percpu(dev->tstats);
  870. return err;
  871. }
  872. return 0;
  873. }
  874. /* Net device detach from fd. */
  875. static void tun_net_uninit(struct net_device *dev)
  876. {
  877. tun_detach_all(dev);
  878. }
  879. /* Net device open. */
  880. static int tun_net_open(struct net_device *dev)
  881. {
  882. netif_tx_start_all_queues(dev);
  883. return 0;
  884. }
  885. /* Net device close. */
  886. static int tun_net_close(struct net_device *dev)
  887. {
  888. netif_tx_stop_all_queues(dev);
  889. return 0;
  890. }
  891. /* Net device start xmit */
  892. static void tun_automq_xmit(struct tun_struct *tun, struct sk_buff *skb)
  893. {
  894. #ifdef CONFIG_RPS
  895. if (tun->numqueues == 1 && static_branch_unlikely(&rps_needed)) {
  896. /* Select queue was not called for the skbuff, so we extract the
  897. * RPS hash and save it into the flow_table here.
  898. */
  899. struct tun_flow_entry *e;
  900. __u32 rxhash;
  901. rxhash = __skb_get_hash_symmetric(skb);
  902. e = tun_flow_find(&tun->flows[tun_hashfn(rxhash)], rxhash);
  903. if (e)
  904. tun_flow_save_rps_rxhash(e, rxhash);
  905. }
  906. #endif
  907. }
  908. static unsigned int run_ebpf_filter(struct tun_struct *tun,
  909. struct sk_buff *skb,
  910. int len)
  911. {
  912. struct tun_prog *prog = rcu_dereference(tun->filter_prog);
  913. if (prog)
  914. len = bpf_prog_run_clear_cb(prog->prog, skb);
  915. return len;
  916. }
  917. /* Net device start xmit */
  918. static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
  919. {
  920. struct tun_struct *tun = netdev_priv(dev);
  921. enum skb_drop_reason drop_reason;
  922. int txq = skb->queue_mapping;
  923. struct netdev_queue *queue;
  924. struct tun_file *tfile;
  925. int len = skb->len;
  926. rcu_read_lock();
  927. tfile = rcu_dereference(tun->tfiles[txq]);
  928. /* Drop packet if interface is not attached */
  929. if (!tfile) {
  930. drop_reason = SKB_DROP_REASON_DEV_READY;
  931. goto drop;
  932. }
  933. if (!rcu_dereference(tun->steering_prog))
  934. tun_automq_xmit(tun, skb);
  935. netif_info(tun, tx_queued, tun->dev, "%s %d\n", __func__, skb->len);
  936. /* Drop if the filter does not like it.
  937. * This is a noop if the filter is disabled.
  938. * Filter can be enabled only for the TAP devices. */
  939. if (!check_filter(&tun->txflt, skb)) {
  940. drop_reason = SKB_DROP_REASON_TAP_TXFILTER;
  941. goto drop;
  942. }
  943. if (tfile->socket.sk->sk_filter &&
  944. sk_filter(tfile->socket.sk, skb)) {
  945. drop_reason = SKB_DROP_REASON_SOCKET_FILTER;
  946. goto drop;
  947. }
  948. len = run_ebpf_filter(tun, skb, len);
  949. if (len == 0) {
  950. drop_reason = SKB_DROP_REASON_TAP_FILTER;
  951. goto drop;
  952. }
  953. if (pskb_trim(skb, len)) {
  954. drop_reason = SKB_DROP_REASON_NOMEM;
  955. goto drop;
  956. }
  957. if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) {
  958. drop_reason = SKB_DROP_REASON_SKB_UCOPY_FAULT;
  959. goto drop;
  960. }
  961. skb_tx_timestamp(skb);
  962. /* Orphan the skb - required as we might hang on to it
  963. * for indefinite time.
  964. */
  965. skb_orphan(skb);
  966. nf_reset_ct(skb);
  967. if (ptr_ring_produce(&tfile->tx_ring, skb)) {
  968. drop_reason = SKB_DROP_REASON_FULL_RING;
  969. goto drop;
  970. }
  971. /* NETIF_F_LLTX requires to do our own update of trans_start */
  972. queue = netdev_get_tx_queue(dev, txq);
  973. txq_trans_cond_update(queue);
  974. /* Notify and wake up reader process */
  975. if (tfile->flags & TUN_FASYNC)
  976. kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
  977. tfile->socket.sk->sk_data_ready(tfile->socket.sk);
  978. rcu_read_unlock();
  979. return NETDEV_TX_OK;
  980. drop:
  981. dev_core_stats_tx_dropped_inc(dev);
  982. skb_tx_error(skb);
  983. kfree_skb_reason(skb, drop_reason);
  984. rcu_read_unlock();
  985. return NET_XMIT_DROP;
  986. }
  987. static void tun_net_mclist(struct net_device *dev)
  988. {
  989. /*
  990. * This callback is supposed to deal with mc filter in
  991. * _rx_ path and has nothing to do with the _tx_ path.
  992. * In rx path we always accept everything userspace gives us.
  993. */
  994. }
  995. static netdev_features_t tun_net_fix_features(struct net_device *dev,
  996. netdev_features_t features)
  997. {
  998. struct tun_struct *tun = netdev_priv(dev);
  999. return (features & tun->set_features) | (features & ~TUN_USER_FEATURES);
  1000. }
  1001. static void tun_set_headroom(struct net_device *dev, int new_hr)
  1002. {
  1003. struct tun_struct *tun = netdev_priv(dev);
  1004. if (new_hr < NET_SKB_PAD)
  1005. new_hr = NET_SKB_PAD;
  1006. tun->align = new_hr;
  1007. }
  1008. static void
  1009. tun_net_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  1010. {
  1011. struct tun_struct *tun = netdev_priv(dev);
  1012. dev_get_tstats64(dev, stats);
  1013. stats->rx_frame_errors +=
  1014. (unsigned long)atomic_long_read(&tun->rx_frame_errors);
  1015. }
  1016. static int tun_xdp_set(struct net_device *dev, struct bpf_prog *prog,
  1017. struct netlink_ext_ack *extack)
  1018. {
  1019. struct tun_struct *tun = netdev_priv(dev);
  1020. struct tun_file *tfile;
  1021. struct bpf_prog *old_prog;
  1022. int i;
  1023. old_prog = rtnl_dereference(tun->xdp_prog);
  1024. rcu_assign_pointer(tun->xdp_prog, prog);
  1025. if (old_prog)
  1026. bpf_prog_put(old_prog);
  1027. for (i = 0; i < tun->numqueues; i++) {
  1028. tfile = rtnl_dereference(tun->tfiles[i]);
  1029. if (prog)
  1030. sock_set_flag(&tfile->sk, SOCK_XDP);
  1031. else
  1032. sock_reset_flag(&tfile->sk, SOCK_XDP);
  1033. }
  1034. list_for_each_entry(tfile, &tun->disabled, next) {
  1035. if (prog)
  1036. sock_set_flag(&tfile->sk, SOCK_XDP);
  1037. else
  1038. sock_reset_flag(&tfile->sk, SOCK_XDP);
  1039. }
  1040. return 0;
  1041. }
  1042. static int tun_xdp(struct net_device *dev, struct netdev_bpf *xdp)
  1043. {
  1044. switch (xdp->command) {
  1045. case XDP_SETUP_PROG:
  1046. return tun_xdp_set(dev, xdp->prog, xdp->extack);
  1047. default:
  1048. return -EINVAL;
  1049. }
  1050. }
  1051. static int tun_net_change_carrier(struct net_device *dev, bool new_carrier)
  1052. {
  1053. if (new_carrier) {
  1054. struct tun_struct *tun = netdev_priv(dev);
  1055. if (!tun->numqueues)
  1056. return -EPERM;
  1057. netif_carrier_on(dev);
  1058. } else {
  1059. netif_carrier_off(dev);
  1060. }
  1061. return 0;
  1062. }
  1063. static const struct net_device_ops tun_netdev_ops = {
  1064. .ndo_init = tun_net_init,
  1065. .ndo_uninit = tun_net_uninit,
  1066. .ndo_open = tun_net_open,
  1067. .ndo_stop = tun_net_close,
  1068. .ndo_start_xmit = tun_net_xmit,
  1069. .ndo_fix_features = tun_net_fix_features,
  1070. .ndo_select_queue = tun_select_queue,
  1071. .ndo_set_rx_headroom = tun_set_headroom,
  1072. .ndo_get_stats64 = tun_net_get_stats64,
  1073. .ndo_change_carrier = tun_net_change_carrier,
  1074. };
  1075. static void __tun_xdp_flush_tfile(struct tun_file *tfile)
  1076. {
  1077. /* Notify and wake up reader process */
  1078. if (tfile->flags & TUN_FASYNC)
  1079. kill_fasync(&tfile->fasync, SIGIO, POLL_IN);
  1080. tfile->socket.sk->sk_data_ready(tfile->socket.sk);
  1081. }
  1082. static int tun_xdp_xmit(struct net_device *dev, int n,
  1083. struct xdp_frame **frames, u32 flags)
  1084. {
  1085. struct tun_struct *tun = netdev_priv(dev);
  1086. struct tun_file *tfile;
  1087. u32 numqueues;
  1088. int nxmit = 0;
  1089. int i;
  1090. if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
  1091. return -EINVAL;
  1092. rcu_read_lock();
  1093. resample:
  1094. numqueues = READ_ONCE(tun->numqueues);
  1095. if (!numqueues) {
  1096. rcu_read_unlock();
  1097. return -ENXIO; /* Caller will free/return all frames */
  1098. }
  1099. tfile = rcu_dereference(tun->tfiles[smp_processor_id() %
  1100. numqueues]);
  1101. if (unlikely(!tfile))
  1102. goto resample;
  1103. spin_lock(&tfile->tx_ring.producer_lock);
  1104. for (i = 0; i < n; i++) {
  1105. struct xdp_frame *xdp = frames[i];
  1106. /* Encode the XDP flag into lowest bit for consumer to differ
  1107. * XDP buffer from sk_buff.
  1108. */
  1109. void *frame = tun_xdp_to_ptr(xdp);
  1110. if (__ptr_ring_produce(&tfile->tx_ring, frame)) {
  1111. dev_core_stats_tx_dropped_inc(dev);
  1112. break;
  1113. }
  1114. nxmit++;
  1115. }
  1116. spin_unlock(&tfile->tx_ring.producer_lock);
  1117. if (flags & XDP_XMIT_FLUSH)
  1118. __tun_xdp_flush_tfile(tfile);
  1119. rcu_read_unlock();
  1120. return nxmit;
  1121. }
  1122. static int tun_xdp_tx(struct net_device *dev, struct xdp_buff *xdp)
  1123. {
  1124. struct xdp_frame *frame = xdp_convert_buff_to_frame(xdp);
  1125. int nxmit;
  1126. if (unlikely(!frame))
  1127. return -EOVERFLOW;
  1128. nxmit = tun_xdp_xmit(dev, 1, &frame, XDP_XMIT_FLUSH);
  1129. if (!nxmit)
  1130. xdp_return_frame_rx_napi(frame);
  1131. return nxmit;
  1132. }
  1133. static const struct net_device_ops tap_netdev_ops = {
  1134. .ndo_init = tun_net_init,
  1135. .ndo_uninit = tun_net_uninit,
  1136. .ndo_open = tun_net_open,
  1137. .ndo_stop = tun_net_close,
  1138. .ndo_start_xmit = tun_net_xmit,
  1139. .ndo_fix_features = tun_net_fix_features,
  1140. .ndo_set_rx_mode = tun_net_mclist,
  1141. .ndo_set_mac_address = eth_mac_addr,
  1142. .ndo_validate_addr = eth_validate_addr,
  1143. .ndo_select_queue = tun_select_queue,
  1144. .ndo_features_check = passthru_features_check,
  1145. .ndo_set_rx_headroom = tun_set_headroom,
  1146. .ndo_get_stats64 = dev_get_tstats64,
  1147. .ndo_bpf = tun_xdp,
  1148. .ndo_xdp_xmit = tun_xdp_xmit,
  1149. .ndo_change_carrier = tun_net_change_carrier,
  1150. };
  1151. static void tun_flow_init(struct tun_struct *tun)
  1152. {
  1153. int i;
  1154. for (i = 0; i < TUN_NUM_FLOW_ENTRIES; i++)
  1155. INIT_HLIST_HEAD(&tun->flows[i]);
  1156. tun->ageing_time = TUN_FLOW_EXPIRE;
  1157. timer_setup(&tun->flow_gc_timer, tun_flow_cleanup, 0);
  1158. mod_timer(&tun->flow_gc_timer,
  1159. round_jiffies_up(jiffies + tun->ageing_time));
  1160. }
  1161. static void tun_flow_uninit(struct tun_struct *tun)
  1162. {
  1163. del_timer_sync(&tun->flow_gc_timer);
  1164. tun_flow_flush(tun);
  1165. }
  1166. #define MIN_MTU 68
  1167. #define MAX_MTU 65535
  1168. /* Initialize net device. */
  1169. static void tun_net_initialize(struct net_device *dev)
  1170. {
  1171. struct tun_struct *tun = netdev_priv(dev);
  1172. switch (tun->flags & TUN_TYPE_MASK) {
  1173. case IFF_TUN:
  1174. dev->netdev_ops = &tun_netdev_ops;
  1175. dev->header_ops = &ip_tunnel_header_ops;
  1176. /* Point-to-Point TUN Device */
  1177. dev->hard_header_len = 0;
  1178. dev->addr_len = 0;
  1179. dev->mtu = 1500;
  1180. /* Zero header length */
  1181. dev->type = ARPHRD_NONE;
  1182. dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  1183. break;
  1184. case IFF_TAP:
  1185. dev->netdev_ops = &tap_netdev_ops;
  1186. /* Ethernet TAP Device */
  1187. ether_setup(dev);
  1188. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1189. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1190. eth_hw_addr_random(dev);
  1191. break;
  1192. }
  1193. dev->min_mtu = MIN_MTU;
  1194. dev->max_mtu = MAX_MTU - dev->hard_header_len;
  1195. }
  1196. static bool tun_sock_writeable(struct tun_struct *tun, struct tun_file *tfile)
  1197. {
  1198. struct sock *sk = tfile->socket.sk;
  1199. return (tun->dev->flags & IFF_UP) && sock_writeable(sk);
  1200. }
  1201. /* Character device part */
  1202. /* Poll */
  1203. static __poll_t tun_chr_poll(struct file *file, poll_table *wait)
  1204. {
  1205. struct tun_file *tfile = file->private_data;
  1206. struct tun_struct *tun = tun_get(tfile);
  1207. struct sock *sk;
  1208. __poll_t mask = 0;
  1209. if (!tun)
  1210. return EPOLLERR;
  1211. sk = tfile->socket.sk;
  1212. poll_wait(file, sk_sleep(sk), wait);
  1213. if (!ptr_ring_empty(&tfile->tx_ring))
  1214. mask |= EPOLLIN | EPOLLRDNORM;
  1215. /* Make sure SOCKWQ_ASYNC_NOSPACE is set if not writable to
  1216. * guarantee EPOLLOUT to be raised by either here or
  1217. * tun_sock_write_space(). Then process could get notification
  1218. * after it writes to a down device and meets -EIO.
  1219. */
  1220. if (tun_sock_writeable(tun, tfile) ||
  1221. (!test_and_set_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags) &&
  1222. tun_sock_writeable(tun, tfile)))
  1223. mask |= EPOLLOUT | EPOLLWRNORM;
  1224. if (tun->dev->reg_state != NETREG_REGISTERED)
  1225. mask = EPOLLERR;
  1226. tun_put(tun);
  1227. return mask;
  1228. }
  1229. static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile,
  1230. size_t len,
  1231. const struct iov_iter *it)
  1232. {
  1233. struct sk_buff *skb;
  1234. size_t linear;
  1235. int err;
  1236. int i;
  1237. if (it->nr_segs > MAX_SKB_FRAGS + 1 ||
  1238. len > (ETH_MAX_MTU - NET_SKB_PAD - NET_IP_ALIGN))
  1239. return ERR_PTR(-EMSGSIZE);
  1240. local_bh_disable();
  1241. skb = napi_get_frags(&tfile->napi);
  1242. local_bh_enable();
  1243. if (!skb)
  1244. return ERR_PTR(-ENOMEM);
  1245. linear = iov_iter_single_seg_count(it);
  1246. err = __skb_grow(skb, linear);
  1247. if (err)
  1248. goto free;
  1249. skb->len = len;
  1250. skb->data_len = len - linear;
  1251. skb->truesize += skb->data_len;
  1252. for (i = 1; i < it->nr_segs; i++) {
  1253. size_t fragsz = it->iov[i].iov_len;
  1254. struct page *page;
  1255. void *frag;
  1256. if (fragsz == 0 || fragsz > PAGE_SIZE) {
  1257. err = -EINVAL;
  1258. goto free;
  1259. }
  1260. frag = netdev_alloc_frag(fragsz);
  1261. if (!frag) {
  1262. err = -ENOMEM;
  1263. goto free;
  1264. }
  1265. page = virt_to_head_page(frag);
  1266. skb_fill_page_desc(skb, i - 1, page,
  1267. frag - page_address(page), fragsz);
  1268. }
  1269. return skb;
  1270. free:
  1271. /* frees skb and all frags allocated with napi_alloc_frag() */
  1272. napi_free_frags(&tfile->napi);
  1273. return ERR_PTR(err);
  1274. }
  1275. /* prepad is the amount to reserve at front. len is length after that.
  1276. * linear is a hint as to how much to copy (usually headers). */
  1277. static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
  1278. size_t prepad, size_t len,
  1279. size_t linear, int noblock)
  1280. {
  1281. struct sock *sk = tfile->socket.sk;
  1282. struct sk_buff *skb;
  1283. int err;
  1284. /* Under a page? Don't bother with paged skb. */
  1285. if (prepad + len < PAGE_SIZE || !linear)
  1286. linear = len;
  1287. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  1288. &err, 0);
  1289. if (!skb)
  1290. return ERR_PTR(err);
  1291. skb_reserve(skb, prepad);
  1292. skb_put(skb, linear);
  1293. skb->data_len = len - linear;
  1294. skb->len += len - linear;
  1295. return skb;
  1296. }
  1297. static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile,
  1298. struct sk_buff *skb, int more)
  1299. {
  1300. struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
  1301. struct sk_buff_head process_queue;
  1302. u32 rx_batched = tun->rx_batched;
  1303. bool rcv = false;
  1304. if (!rx_batched || (!more && skb_queue_empty(queue))) {
  1305. local_bh_disable();
  1306. skb_record_rx_queue(skb, tfile->queue_index);
  1307. netif_receive_skb(skb);
  1308. local_bh_enable();
  1309. return;
  1310. }
  1311. spin_lock(&queue->lock);
  1312. if (!more || skb_queue_len(queue) == rx_batched) {
  1313. __skb_queue_head_init(&process_queue);
  1314. skb_queue_splice_tail_init(queue, &process_queue);
  1315. rcv = true;
  1316. } else {
  1317. __skb_queue_tail(queue, skb);
  1318. }
  1319. spin_unlock(&queue->lock);
  1320. if (rcv) {
  1321. struct sk_buff *nskb;
  1322. local_bh_disable();
  1323. while ((nskb = __skb_dequeue(&process_queue))) {
  1324. skb_record_rx_queue(nskb, tfile->queue_index);
  1325. netif_receive_skb(nskb);
  1326. }
  1327. skb_record_rx_queue(skb, tfile->queue_index);
  1328. netif_receive_skb(skb);
  1329. local_bh_enable();
  1330. }
  1331. }
  1332. static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile,
  1333. int len, int noblock, bool zerocopy)
  1334. {
  1335. if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
  1336. return false;
  1337. if (tfile->socket.sk->sk_sndbuf != INT_MAX)
  1338. return false;
  1339. if (!noblock)
  1340. return false;
  1341. if (zerocopy)
  1342. return false;
  1343. if (SKB_DATA_ALIGN(len + TUN_RX_PAD + XDP_PACKET_HEADROOM) +
  1344. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE)
  1345. return false;
  1346. return true;
  1347. }
  1348. static struct sk_buff *__tun_build_skb(struct tun_file *tfile,
  1349. struct page_frag *alloc_frag, char *buf,
  1350. int buflen, int len, int pad)
  1351. {
  1352. struct sk_buff *skb = build_skb(buf, buflen);
  1353. if (!skb)
  1354. return ERR_PTR(-ENOMEM);
  1355. skb_reserve(skb, pad);
  1356. skb_put(skb, len);
  1357. skb_set_owner_w(skb, tfile->socket.sk);
  1358. get_page(alloc_frag->page);
  1359. alloc_frag->offset += buflen;
  1360. return skb;
  1361. }
  1362. static int tun_xdp_act(struct tun_struct *tun, struct bpf_prog *xdp_prog,
  1363. struct xdp_buff *xdp, u32 act)
  1364. {
  1365. int err;
  1366. switch (act) {
  1367. case XDP_REDIRECT:
  1368. err = xdp_do_redirect(tun->dev, xdp, xdp_prog);
  1369. if (err)
  1370. return err;
  1371. break;
  1372. case XDP_TX:
  1373. err = tun_xdp_tx(tun->dev, xdp);
  1374. if (err < 0)
  1375. return err;
  1376. break;
  1377. case XDP_PASS:
  1378. break;
  1379. default:
  1380. bpf_warn_invalid_xdp_action(tun->dev, xdp_prog, act);
  1381. fallthrough;
  1382. case XDP_ABORTED:
  1383. trace_xdp_exception(tun->dev, xdp_prog, act);
  1384. fallthrough;
  1385. case XDP_DROP:
  1386. dev_core_stats_rx_dropped_inc(tun->dev);
  1387. break;
  1388. }
  1389. return act;
  1390. }
  1391. static struct sk_buff *tun_build_skb(struct tun_struct *tun,
  1392. struct tun_file *tfile,
  1393. struct iov_iter *from,
  1394. struct virtio_net_hdr *hdr,
  1395. int len, int *skb_xdp)
  1396. {
  1397. struct page_frag *alloc_frag = &current->task_frag;
  1398. struct bpf_prog *xdp_prog;
  1399. int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  1400. char *buf;
  1401. size_t copied;
  1402. int pad = TUN_RX_PAD;
  1403. int err = 0;
  1404. rcu_read_lock();
  1405. xdp_prog = rcu_dereference(tun->xdp_prog);
  1406. if (xdp_prog)
  1407. pad += XDP_PACKET_HEADROOM;
  1408. buflen += SKB_DATA_ALIGN(len + pad);
  1409. rcu_read_unlock();
  1410. alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES);
  1411. if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL)))
  1412. return ERR_PTR(-ENOMEM);
  1413. buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
  1414. copied = copy_page_from_iter(alloc_frag->page,
  1415. alloc_frag->offset + pad,
  1416. len, from);
  1417. if (copied != len)
  1418. return ERR_PTR(-EFAULT);
  1419. /* There's a small window that XDP may be set after the check
  1420. * of xdp_prog above, this should be rare and for simplicity
  1421. * we do XDP on skb in case the headroom is not enough.
  1422. */
  1423. if (hdr->gso_type || !xdp_prog) {
  1424. *skb_xdp = 1;
  1425. return __tun_build_skb(tfile, alloc_frag, buf, buflen, len,
  1426. pad);
  1427. }
  1428. *skb_xdp = 0;
  1429. local_bh_disable();
  1430. rcu_read_lock();
  1431. xdp_prog = rcu_dereference(tun->xdp_prog);
  1432. if (xdp_prog) {
  1433. struct xdp_buff xdp;
  1434. u32 act;
  1435. xdp_init_buff(&xdp, buflen, &tfile->xdp_rxq);
  1436. xdp_prepare_buff(&xdp, buf, pad, len, false);
  1437. act = bpf_prog_run_xdp(xdp_prog, &xdp);
  1438. if (act == XDP_REDIRECT || act == XDP_TX) {
  1439. get_page(alloc_frag->page);
  1440. alloc_frag->offset += buflen;
  1441. }
  1442. err = tun_xdp_act(tun, xdp_prog, &xdp, act);
  1443. if (err < 0) {
  1444. if (act == XDP_REDIRECT || act == XDP_TX)
  1445. put_page(alloc_frag->page);
  1446. goto out;
  1447. }
  1448. if (err == XDP_REDIRECT)
  1449. xdp_do_flush();
  1450. if (err != XDP_PASS)
  1451. goto out;
  1452. pad = xdp.data - xdp.data_hard_start;
  1453. len = xdp.data_end - xdp.data;
  1454. }
  1455. rcu_read_unlock();
  1456. local_bh_enable();
  1457. return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, pad);
  1458. out:
  1459. rcu_read_unlock();
  1460. local_bh_enable();
  1461. return NULL;
  1462. }
  1463. /* Get packet from user space buffer */
  1464. static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
  1465. void *msg_control, struct iov_iter *from,
  1466. int noblock, bool more)
  1467. {
  1468. struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
  1469. struct sk_buff *skb;
  1470. size_t total_len = iov_iter_count(from);
  1471. size_t len = total_len, align = tun->align, linear;
  1472. struct virtio_net_hdr gso = { 0 };
  1473. int good_linear;
  1474. int copylen;
  1475. bool zerocopy = false;
  1476. int err;
  1477. u32 rxhash = 0;
  1478. int skb_xdp = 1;
  1479. bool frags = tun_napi_frags_enabled(tfile);
  1480. enum skb_drop_reason drop_reason = SKB_DROP_REASON_NOT_SPECIFIED;
  1481. if (!(tun->flags & IFF_NO_PI)) {
  1482. if (len < sizeof(pi))
  1483. return -EINVAL;
  1484. len -= sizeof(pi);
  1485. if (!copy_from_iter_full(&pi, sizeof(pi), from))
  1486. return -EFAULT;
  1487. }
  1488. if (tun->flags & IFF_VNET_HDR) {
  1489. int vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
  1490. if (len < vnet_hdr_sz)
  1491. return -EINVAL;
  1492. len -= vnet_hdr_sz;
  1493. if (!copy_from_iter_full(&gso, sizeof(gso), from))
  1494. return -EFAULT;
  1495. if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  1496. tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len))
  1497. gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2);
  1498. if (tun16_to_cpu(tun, gso.hdr_len) > len)
  1499. return -EINVAL;
  1500. iov_iter_advance(from, vnet_hdr_sz - sizeof(gso));
  1501. }
  1502. if ((tun->flags & TUN_TYPE_MASK) == IFF_TAP) {
  1503. align += NET_IP_ALIGN;
  1504. if (unlikely(len < ETH_HLEN ||
  1505. (gso.hdr_len && tun16_to_cpu(tun, gso.hdr_len) < ETH_HLEN)))
  1506. return -EINVAL;
  1507. }
  1508. good_linear = SKB_MAX_HEAD(align);
  1509. if (msg_control) {
  1510. struct iov_iter i = *from;
  1511. /* There are 256 bytes to be copied in skb, so there is
  1512. * enough room for skb expand head in case it is used.
  1513. * The rest of the buffer is mapped from userspace.
  1514. */
  1515. copylen = gso.hdr_len ? tun16_to_cpu(tun, gso.hdr_len) : GOODCOPY_LEN;
  1516. if (copylen > good_linear)
  1517. copylen = good_linear;
  1518. linear = copylen;
  1519. iov_iter_advance(&i, copylen);
  1520. if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
  1521. zerocopy = true;
  1522. }
  1523. if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) {
  1524. /* For the packet that is not easy to be processed
  1525. * (e.g gso or jumbo packet), we will do it at after
  1526. * skb was created with generic XDP routine.
  1527. */
  1528. skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp);
  1529. err = PTR_ERR_OR_ZERO(skb);
  1530. if (err)
  1531. goto drop;
  1532. if (!skb)
  1533. return total_len;
  1534. } else {
  1535. if (!zerocopy) {
  1536. copylen = len;
  1537. if (tun16_to_cpu(tun, gso.hdr_len) > good_linear)
  1538. linear = good_linear;
  1539. else
  1540. linear = tun16_to_cpu(tun, gso.hdr_len);
  1541. }
  1542. if (frags) {
  1543. mutex_lock(&tfile->napi_mutex);
  1544. skb = tun_napi_alloc_frags(tfile, copylen, from);
  1545. /* tun_napi_alloc_frags() enforces a layout for the skb.
  1546. * If zerocopy is enabled, then this layout will be
  1547. * overwritten by zerocopy_sg_from_iter().
  1548. */
  1549. zerocopy = false;
  1550. } else {
  1551. skb = tun_alloc_skb(tfile, align, copylen, linear,
  1552. noblock);
  1553. }
  1554. err = PTR_ERR_OR_ZERO(skb);
  1555. if (err)
  1556. goto drop;
  1557. if (zerocopy)
  1558. err = zerocopy_sg_from_iter(skb, from);
  1559. else
  1560. err = skb_copy_datagram_from_iter(skb, 0, from, len);
  1561. if (err) {
  1562. err = -EFAULT;
  1563. drop_reason = SKB_DROP_REASON_SKB_UCOPY_FAULT;
  1564. goto drop;
  1565. }
  1566. }
  1567. if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) {
  1568. atomic_long_inc(&tun->rx_frame_errors);
  1569. err = -EINVAL;
  1570. goto free_skb;
  1571. }
  1572. switch (tun->flags & TUN_TYPE_MASK) {
  1573. case IFF_TUN:
  1574. if (tun->flags & IFF_NO_PI) {
  1575. u8 ip_version = skb->len ? (skb->data[0] >> 4) : 0;
  1576. switch (ip_version) {
  1577. case 4:
  1578. pi.proto = htons(ETH_P_IP);
  1579. break;
  1580. case 6:
  1581. pi.proto = htons(ETH_P_IPV6);
  1582. break;
  1583. default:
  1584. err = -EINVAL;
  1585. goto drop;
  1586. }
  1587. }
  1588. skb_reset_mac_header(skb);
  1589. skb->protocol = pi.proto;
  1590. skb->dev = tun->dev;
  1591. break;
  1592. case IFF_TAP:
  1593. if (frags && !pskb_may_pull(skb, ETH_HLEN)) {
  1594. err = -ENOMEM;
  1595. drop_reason = SKB_DROP_REASON_HDR_TRUNC;
  1596. goto drop;
  1597. }
  1598. skb->protocol = eth_type_trans(skb, tun->dev);
  1599. break;
  1600. }
  1601. /* copy skb_ubuf_info for callback when skb has no error */
  1602. if (zerocopy) {
  1603. skb_zcopy_init(skb, msg_control);
  1604. } else if (msg_control) {
  1605. struct ubuf_info *uarg = msg_control;
  1606. uarg->callback(NULL, uarg, false);
  1607. }
  1608. skb_reset_network_header(skb);
  1609. skb_probe_transport_header(skb);
  1610. skb_record_rx_queue(skb, tfile->queue_index);
  1611. if (skb_xdp) {
  1612. struct bpf_prog *xdp_prog;
  1613. int ret;
  1614. local_bh_disable();
  1615. rcu_read_lock();
  1616. xdp_prog = rcu_dereference(tun->xdp_prog);
  1617. if (xdp_prog) {
  1618. ret = do_xdp_generic(xdp_prog, skb);
  1619. if (ret != XDP_PASS) {
  1620. rcu_read_unlock();
  1621. local_bh_enable();
  1622. goto unlock_frags;
  1623. }
  1624. }
  1625. rcu_read_unlock();
  1626. local_bh_enable();
  1627. }
  1628. /* Compute the costly rx hash only if needed for flow updates.
  1629. * We may get a very small possibility of OOO during switching, not
  1630. * worth to optimize.
  1631. */
  1632. if (!rcu_access_pointer(tun->steering_prog) && tun->numqueues > 1 &&
  1633. !tfile->detached)
  1634. rxhash = __skb_get_hash_symmetric(skb);
  1635. rcu_read_lock();
  1636. if (unlikely(!(tun->dev->flags & IFF_UP))) {
  1637. err = -EIO;
  1638. rcu_read_unlock();
  1639. drop_reason = SKB_DROP_REASON_DEV_READY;
  1640. goto drop;
  1641. }
  1642. if (frags) {
  1643. u32 headlen;
  1644. /* Exercise flow dissector code path. */
  1645. skb_push(skb, ETH_HLEN);
  1646. headlen = eth_get_headlen(tun->dev, skb->data,
  1647. skb_headlen(skb));
  1648. if (unlikely(headlen > skb_headlen(skb))) {
  1649. WARN_ON_ONCE(1);
  1650. err = -ENOMEM;
  1651. dev_core_stats_rx_dropped_inc(tun->dev);
  1652. napi_busy:
  1653. napi_free_frags(&tfile->napi);
  1654. rcu_read_unlock();
  1655. mutex_unlock(&tfile->napi_mutex);
  1656. return err;
  1657. }
  1658. if (likely(napi_schedule_prep(&tfile->napi))) {
  1659. local_bh_disable();
  1660. napi_gro_frags(&tfile->napi);
  1661. napi_complete(&tfile->napi);
  1662. local_bh_enable();
  1663. } else {
  1664. err = -EBUSY;
  1665. goto napi_busy;
  1666. }
  1667. mutex_unlock(&tfile->napi_mutex);
  1668. } else if (tfile->napi_enabled) {
  1669. struct sk_buff_head *queue = &tfile->sk.sk_write_queue;
  1670. int queue_len;
  1671. spin_lock_bh(&queue->lock);
  1672. if (unlikely(tfile->detached)) {
  1673. spin_unlock_bh(&queue->lock);
  1674. rcu_read_unlock();
  1675. err = -EBUSY;
  1676. goto free_skb;
  1677. }
  1678. __skb_queue_tail(queue, skb);
  1679. queue_len = skb_queue_len(queue);
  1680. spin_unlock(&queue->lock);
  1681. if (!more || queue_len > NAPI_POLL_WEIGHT)
  1682. napi_schedule(&tfile->napi);
  1683. local_bh_enable();
  1684. } else if (!IS_ENABLED(CONFIG_4KSTACKS)) {
  1685. tun_rx_batched(tun, tfile, skb, more);
  1686. } else {
  1687. netif_rx(skb);
  1688. }
  1689. rcu_read_unlock();
  1690. preempt_disable();
  1691. dev_sw_netstats_rx_add(tun->dev, len);
  1692. preempt_enable();
  1693. if (rxhash)
  1694. tun_flow_update(tun, rxhash, tfile);
  1695. return total_len;
  1696. drop:
  1697. if (err != -EAGAIN)
  1698. dev_core_stats_rx_dropped_inc(tun->dev);
  1699. free_skb:
  1700. if (!IS_ERR_OR_NULL(skb))
  1701. kfree_skb_reason(skb, drop_reason);
  1702. unlock_frags:
  1703. if (frags) {
  1704. tfile->napi.skb = NULL;
  1705. mutex_unlock(&tfile->napi_mutex);
  1706. }
  1707. return err ?: total_len;
  1708. }
  1709. static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
  1710. {
  1711. struct file *file = iocb->ki_filp;
  1712. struct tun_file *tfile = file->private_data;
  1713. struct tun_struct *tun = tun_get(tfile);
  1714. ssize_t result;
  1715. int noblock = 0;
  1716. if (!tun)
  1717. return -EBADFD;
  1718. if ((file->f_flags & O_NONBLOCK) || (iocb->ki_flags & IOCB_NOWAIT))
  1719. noblock = 1;
  1720. result = tun_get_user(tun, tfile, NULL, from, noblock, false);
  1721. tun_put(tun);
  1722. return result;
  1723. }
  1724. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN {
  1725. #ifdef CONFIG_KNOX_NCM
  1726. static int get_meta_param_values(struct sk_buff *skb,
  1727. struct knox_meta_param *metalocal) {
  1728. struct skb_shared_info *knox_shinfo = NULL;
  1729. if (skb != NULL)
  1730. knox_shinfo = skb_shinfo(skb);
  1731. else {
  1732. #ifdef TUN_DEBUG
  1733. pr_err("KNOX: NULL SKB in knoxvpn_process_uidpid");
  1734. #endif
  1735. return 1;
  1736. }
  1737. if (knox_shinfo == NULL) {
  1738. #ifdef TUN_DEBUG
  1739. pr_err("KNOX: knox_shinfo value is null");
  1740. #endif
  1741. return 1;
  1742. }
  1743. if (knox_shinfo->android_oem_data1[2] >= META_MARK_BASE_LOWER && knox_shinfo->android_oem_data1[2] <= META_MARK_BASE_UPPER) {
  1744. metalocal->uid = (uid_t)knox_shinfo->android_oem_data1[0];
  1745. metalocal->pid = (pid_t)knox_shinfo->android_oem_data1[1];
  1746. }
  1747. if (knox_shinfo != NULL) {
  1748. knox_shinfo->android_oem_data1[0] = knox_shinfo->android_oem_data1[1] = 0;
  1749. knox_shinfo->android_oem_data1[2] = 0;
  1750. }
  1751. return 0;
  1752. }
  1753. #endif
  1754. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN }
  1755. static ssize_t tun_put_user_xdp(struct tun_struct *tun,
  1756. struct tun_file *tfile,
  1757. struct xdp_frame *xdp_frame,
  1758. struct iov_iter *iter)
  1759. {
  1760. int vnet_hdr_sz = 0;
  1761. size_t size = xdp_frame->len;
  1762. size_t ret;
  1763. if (tun->flags & IFF_VNET_HDR) {
  1764. struct virtio_net_hdr gso = { 0 };
  1765. vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
  1766. if (unlikely(iov_iter_count(iter) < vnet_hdr_sz))
  1767. return -EINVAL;
  1768. if (unlikely(copy_to_iter(&gso, sizeof(gso), iter) !=
  1769. sizeof(gso)))
  1770. return -EFAULT;
  1771. iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
  1772. }
  1773. ret = copy_to_iter(xdp_frame->data, size, iter) + vnet_hdr_sz;
  1774. preempt_disable();
  1775. dev_sw_netstats_tx_add(tun->dev, 1, ret);
  1776. preempt_enable();
  1777. return ret;
  1778. }
  1779. /* Put packet to the user space buffer */
  1780. static ssize_t tun_put_user(struct tun_struct *tun,
  1781. struct tun_file *tfile,
  1782. struct sk_buff *skb,
  1783. struct iov_iter *iter)
  1784. {
  1785. struct tun_pi pi = { 0, skb->protocol };
  1786. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN {
  1787. #ifdef CONFIG_KNOX_NCM
  1788. struct knox_meta_param metalocal = { 0, 0 };
  1789. int meta_param_get_status = 0;
  1790. #endif
  1791. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN }
  1792. ssize_t total;
  1793. int vlan_offset = 0;
  1794. int vlan_hlen = 0;
  1795. int vnet_hdr_sz = 0;
  1796. if (skb_vlan_tag_present(skb))
  1797. vlan_hlen = VLAN_HLEN;
  1798. if (tun->flags & IFF_VNET_HDR)
  1799. vnet_hdr_sz = READ_ONCE(tun->vnet_hdr_sz);
  1800. total = skb->len + vlan_hlen + vnet_hdr_sz;
  1801. if (!(tun->flags & IFF_NO_PI)) {
  1802. if (iov_iter_count(iter) < sizeof(pi))
  1803. return -EINVAL;
  1804. total += sizeof(pi);
  1805. if (iov_iter_count(iter) < total) {
  1806. /* Packet will be striped */
  1807. pi.flags |= TUN_PKT_STRIP;
  1808. }
  1809. if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
  1810. return -EFAULT;
  1811. }
  1812. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN {
  1813. #ifdef CONFIG_KNOX_NCM
  1814. meta_param_get_status = get_meta_param_values(skb, &metalocal);
  1815. if(meta_param_get_status == 1) {
  1816. #ifdef TUN_DEBUG
  1817. pr_err("KNOX: Error obtaining meta param values");
  1818. #endif
  1819. } else {
  1820. if (tun->flags & TUN_META_HDR) {
  1821. #ifdef TUN_DEBUG
  1822. pr_err("KNOX: Appending uid: %d and pid: %d", metalocal.uid,
  1823. metalocal.pid);
  1824. #endif
  1825. if (iov_iter_count(iter) < sizeof(struct knox_meta_param)) {
  1826. return -EINVAL;
  1827. }
  1828. total += sizeof(struct knox_meta_param);
  1829. if (copy_to_iter(&metalocal, sizeof(struct knox_meta_param), iter) != sizeof(struct knox_meta_param)) {
  1830. return -EFAULT;
  1831. }
  1832. }
  1833. }
  1834. #endif
  1835. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN }
  1836. if (vnet_hdr_sz) {
  1837. struct virtio_net_hdr gso;
  1838. if (iov_iter_count(iter) < vnet_hdr_sz)
  1839. return -EINVAL;
  1840. if (virtio_net_hdr_from_skb(skb, &gso,
  1841. tun_is_little_endian(tun), true,
  1842. vlan_hlen)) {
  1843. struct skb_shared_info *sinfo = skb_shinfo(skb);
  1844. pr_err("unexpected GSO type: "
  1845. "0x%x, gso_size %d, hdr_len %d\n",
  1846. sinfo->gso_type, tun16_to_cpu(tun, gso.gso_size),
  1847. tun16_to_cpu(tun, gso.hdr_len));
  1848. print_hex_dump(KERN_ERR, "tun: ",
  1849. DUMP_PREFIX_NONE,
  1850. 16, 1, skb->head,
  1851. min((int)tun16_to_cpu(tun, gso.hdr_len), 64), true);
  1852. WARN_ON_ONCE(1);
  1853. return -EINVAL;
  1854. }
  1855. if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
  1856. return -EFAULT;
  1857. iov_iter_advance(iter, vnet_hdr_sz - sizeof(gso));
  1858. }
  1859. if (vlan_hlen) {
  1860. int ret;
  1861. struct veth veth;
  1862. veth.h_vlan_proto = skb->vlan_proto;
  1863. veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
  1864. vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
  1865. ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
  1866. if (ret || !iov_iter_count(iter))
  1867. goto done;
  1868. ret = copy_to_iter(&veth, sizeof(veth), iter);
  1869. if (ret != sizeof(veth) || !iov_iter_count(iter))
  1870. goto done;
  1871. }
  1872. skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
  1873. done:
  1874. /* caller is in process context, */
  1875. preempt_disable();
  1876. dev_sw_netstats_tx_add(tun->dev, 1, skb->len + vlan_hlen);
  1877. preempt_enable();
  1878. return total;
  1879. }
  1880. static void *tun_ring_recv(struct tun_file *tfile, int noblock, int *err)
  1881. {
  1882. DECLARE_WAITQUEUE(wait, current);
  1883. void *ptr = NULL;
  1884. int error = 0;
  1885. ptr = ptr_ring_consume(&tfile->tx_ring);
  1886. if (ptr)
  1887. goto out;
  1888. if (noblock) {
  1889. error = -EAGAIN;
  1890. goto out;
  1891. }
  1892. add_wait_queue(&tfile->socket.wq.wait, &wait);
  1893. while (1) {
  1894. set_current_state(TASK_INTERRUPTIBLE);
  1895. ptr = ptr_ring_consume(&tfile->tx_ring);
  1896. if (ptr)
  1897. break;
  1898. if (signal_pending(current)) {
  1899. error = -ERESTARTSYS;
  1900. break;
  1901. }
  1902. if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) {
  1903. error = -EFAULT;
  1904. break;
  1905. }
  1906. schedule();
  1907. }
  1908. __set_current_state(TASK_RUNNING);
  1909. remove_wait_queue(&tfile->socket.wq.wait, &wait);
  1910. out:
  1911. *err = error;
  1912. return ptr;
  1913. }
  1914. static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
  1915. struct iov_iter *to,
  1916. int noblock, void *ptr)
  1917. {
  1918. ssize_t ret;
  1919. int err;
  1920. if (!iov_iter_count(to)) {
  1921. tun_ptr_free(ptr);
  1922. return 0;
  1923. }
  1924. if (!ptr) {
  1925. /* Read frames from ring */
  1926. ptr = tun_ring_recv(tfile, noblock, &err);
  1927. if (!ptr)
  1928. return err;
  1929. }
  1930. if (tun_is_xdp_frame(ptr)) {
  1931. struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
  1932. ret = tun_put_user_xdp(tun, tfile, xdpf, to);
  1933. xdp_return_frame(xdpf);
  1934. } else {
  1935. struct sk_buff *skb = ptr;
  1936. ret = tun_put_user(tun, tfile, skb, to);
  1937. if (unlikely(ret < 0))
  1938. kfree_skb(skb);
  1939. else
  1940. consume_skb(skb);
  1941. }
  1942. return ret;
  1943. }
  1944. static ssize_t tun_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1945. {
  1946. struct file *file = iocb->ki_filp;
  1947. struct tun_file *tfile = file->private_data;
  1948. struct tun_struct *tun = tun_get(tfile);
  1949. ssize_t len = iov_iter_count(to), ret;
  1950. int noblock = 0;
  1951. if (!tun)
  1952. return -EBADFD;
  1953. if ((file->f_flags & O_NONBLOCK) || (iocb->ki_flags & IOCB_NOWAIT))
  1954. noblock = 1;
  1955. ret = tun_do_read(tun, tfile, to, noblock, NULL);
  1956. ret = min_t(ssize_t, ret, len);
  1957. if (ret > 0)
  1958. iocb->ki_pos = ret;
  1959. tun_put(tun);
  1960. return ret;
  1961. }
  1962. static void tun_prog_free(struct rcu_head *rcu)
  1963. {
  1964. struct tun_prog *prog = container_of(rcu, struct tun_prog, rcu);
  1965. bpf_prog_destroy(prog->prog);
  1966. kfree(prog);
  1967. }
  1968. static int __tun_set_ebpf(struct tun_struct *tun,
  1969. struct tun_prog __rcu **prog_p,
  1970. struct bpf_prog *prog)
  1971. {
  1972. struct tun_prog *old, *new = NULL;
  1973. if (prog) {
  1974. new = kmalloc(sizeof(*new), GFP_KERNEL);
  1975. if (!new)
  1976. return -ENOMEM;
  1977. new->prog = prog;
  1978. }
  1979. spin_lock_bh(&tun->lock);
  1980. old = rcu_dereference_protected(*prog_p,
  1981. lockdep_is_held(&tun->lock));
  1982. rcu_assign_pointer(*prog_p, new);
  1983. spin_unlock_bh(&tun->lock);
  1984. if (old)
  1985. call_rcu(&old->rcu, tun_prog_free);
  1986. return 0;
  1987. }
  1988. static void tun_free_netdev(struct net_device *dev)
  1989. {
  1990. struct tun_struct *tun = netdev_priv(dev);
  1991. BUG_ON(!(list_empty(&tun->disabled)));
  1992. free_percpu(dev->tstats);
  1993. tun_flow_uninit(tun);
  1994. security_tun_dev_free_security(tun->security);
  1995. __tun_set_ebpf(tun, &tun->steering_prog, NULL);
  1996. __tun_set_ebpf(tun, &tun->filter_prog, NULL);
  1997. }
  1998. static void tun_setup(struct net_device *dev)
  1999. {
  2000. struct tun_struct *tun = netdev_priv(dev);
  2001. tun->owner = INVALID_UID;
  2002. tun->group = INVALID_GID;
  2003. tun_default_link_ksettings(dev, &tun->link_ksettings);
  2004. dev->ethtool_ops = &tun_ethtool_ops;
  2005. dev->needs_free_netdev = true;
  2006. dev->priv_destructor = tun_free_netdev;
  2007. /* We prefer our own queue length */
  2008. dev->tx_queue_len = TUN_READQ_SIZE;
  2009. }
  2010. /* Trivial set of netlink ops to allow deleting tun or tap
  2011. * device with netlink.
  2012. */
  2013. static int tun_validate(struct nlattr *tb[], struct nlattr *data[],
  2014. struct netlink_ext_ack *extack)
  2015. {
  2016. NL_SET_ERR_MSG(extack,
  2017. "tun/tap creation via rtnetlink is not supported.");
  2018. return -EOPNOTSUPP;
  2019. }
  2020. static size_t tun_get_size(const struct net_device *dev)
  2021. {
  2022. BUILD_BUG_ON(sizeof(u32) != sizeof(uid_t));
  2023. BUILD_BUG_ON(sizeof(u32) != sizeof(gid_t));
  2024. return nla_total_size(sizeof(uid_t)) + /* OWNER */
  2025. nla_total_size(sizeof(gid_t)) + /* GROUP */
  2026. nla_total_size(sizeof(u8)) + /* TYPE */
  2027. nla_total_size(sizeof(u8)) + /* PI */
  2028. nla_total_size(sizeof(u8)) + /* VNET_HDR */
  2029. nla_total_size(sizeof(u8)) + /* PERSIST */
  2030. nla_total_size(sizeof(u8)) + /* MULTI_QUEUE */
  2031. nla_total_size(sizeof(u32)) + /* NUM_QUEUES */
  2032. nla_total_size(sizeof(u32)) + /* NUM_DISABLED_QUEUES */
  2033. 0;
  2034. }
  2035. static int tun_fill_info(struct sk_buff *skb, const struct net_device *dev)
  2036. {
  2037. struct tun_struct *tun = netdev_priv(dev);
  2038. if (nla_put_u8(skb, IFLA_TUN_TYPE, tun->flags & TUN_TYPE_MASK))
  2039. goto nla_put_failure;
  2040. if (uid_valid(tun->owner) &&
  2041. nla_put_u32(skb, IFLA_TUN_OWNER,
  2042. from_kuid_munged(current_user_ns(), tun->owner)))
  2043. goto nla_put_failure;
  2044. if (gid_valid(tun->group) &&
  2045. nla_put_u32(skb, IFLA_TUN_GROUP,
  2046. from_kgid_munged(current_user_ns(), tun->group)))
  2047. goto nla_put_failure;
  2048. if (nla_put_u8(skb, IFLA_TUN_PI, !(tun->flags & IFF_NO_PI)))
  2049. goto nla_put_failure;
  2050. if (nla_put_u8(skb, IFLA_TUN_VNET_HDR, !!(tun->flags & IFF_VNET_HDR)))
  2051. goto nla_put_failure;
  2052. if (nla_put_u8(skb, IFLA_TUN_PERSIST, !!(tun->flags & IFF_PERSIST)))
  2053. goto nla_put_failure;
  2054. if (nla_put_u8(skb, IFLA_TUN_MULTI_QUEUE,
  2055. !!(tun->flags & IFF_MULTI_QUEUE)))
  2056. goto nla_put_failure;
  2057. if (tun->flags & IFF_MULTI_QUEUE) {
  2058. if (nla_put_u32(skb, IFLA_TUN_NUM_QUEUES, tun->numqueues))
  2059. goto nla_put_failure;
  2060. if (nla_put_u32(skb, IFLA_TUN_NUM_DISABLED_QUEUES,
  2061. tun->numdisabled))
  2062. goto nla_put_failure;
  2063. }
  2064. return 0;
  2065. nla_put_failure:
  2066. return -EMSGSIZE;
  2067. }
  2068. static struct rtnl_link_ops tun_link_ops __read_mostly = {
  2069. .kind = DRV_NAME,
  2070. .priv_size = sizeof(struct tun_struct),
  2071. .setup = tun_setup,
  2072. .validate = tun_validate,
  2073. .get_size = tun_get_size,
  2074. .fill_info = tun_fill_info,
  2075. };
  2076. static void tun_sock_write_space(struct sock *sk)
  2077. {
  2078. struct tun_file *tfile;
  2079. wait_queue_head_t *wqueue;
  2080. if (!sock_writeable(sk))
  2081. return;
  2082. if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &sk->sk_socket->flags))
  2083. return;
  2084. wqueue = sk_sleep(sk);
  2085. if (wqueue && waitqueue_active(wqueue))
  2086. wake_up_interruptible_sync_poll(wqueue, EPOLLOUT |
  2087. EPOLLWRNORM | EPOLLWRBAND);
  2088. tfile = container_of(sk, struct tun_file, sk);
  2089. kill_fasync(&tfile->fasync, SIGIO, POLL_OUT);
  2090. }
  2091. static void tun_put_page(struct tun_page *tpage)
  2092. {
  2093. if (tpage->page)
  2094. __page_frag_cache_drain(tpage->page, tpage->count);
  2095. }
  2096. static int tun_xdp_one(struct tun_struct *tun,
  2097. struct tun_file *tfile,
  2098. struct xdp_buff *xdp, int *flush,
  2099. struct tun_page *tpage)
  2100. {
  2101. unsigned int datasize = xdp->data_end - xdp->data;
  2102. struct tun_xdp_hdr *hdr = xdp->data_hard_start;
  2103. struct virtio_net_hdr *gso = &hdr->gso;
  2104. struct bpf_prog *xdp_prog;
  2105. struct sk_buff *skb = NULL;
  2106. struct sk_buff_head *queue;
  2107. u32 rxhash = 0, act;
  2108. int buflen = hdr->buflen;
  2109. int ret = 0;
  2110. bool skb_xdp = false;
  2111. struct page *page;
  2112. xdp_prog = rcu_dereference(tun->xdp_prog);
  2113. if (xdp_prog) {
  2114. if (gso->gso_type) {
  2115. skb_xdp = true;
  2116. goto build;
  2117. }
  2118. xdp_init_buff(xdp, buflen, &tfile->xdp_rxq);
  2119. xdp_set_data_meta_invalid(xdp);
  2120. act = bpf_prog_run_xdp(xdp_prog, xdp);
  2121. ret = tun_xdp_act(tun, xdp_prog, xdp, act);
  2122. if (ret < 0) {
  2123. put_page(virt_to_head_page(xdp->data));
  2124. return ret;
  2125. }
  2126. switch (ret) {
  2127. case XDP_REDIRECT:
  2128. *flush = true;
  2129. fallthrough;
  2130. case XDP_TX:
  2131. return 0;
  2132. case XDP_PASS:
  2133. break;
  2134. default:
  2135. page = virt_to_head_page(xdp->data);
  2136. if (tpage->page == page) {
  2137. ++tpage->count;
  2138. } else {
  2139. tun_put_page(tpage);
  2140. tpage->page = page;
  2141. tpage->count = 1;
  2142. }
  2143. return 0;
  2144. }
  2145. }
  2146. build:
  2147. skb = build_skb(xdp->data_hard_start, buflen);
  2148. if (!skb) {
  2149. ret = -ENOMEM;
  2150. goto out;
  2151. }
  2152. skb_reserve(skb, xdp->data - xdp->data_hard_start);
  2153. skb_put(skb, xdp->data_end - xdp->data);
  2154. if (virtio_net_hdr_to_skb(skb, gso, tun_is_little_endian(tun))) {
  2155. atomic_long_inc(&tun->rx_frame_errors);
  2156. kfree_skb(skb);
  2157. ret = -EINVAL;
  2158. goto out;
  2159. }
  2160. skb->protocol = eth_type_trans(skb, tun->dev);
  2161. skb_reset_network_header(skb);
  2162. skb_probe_transport_header(skb);
  2163. skb_record_rx_queue(skb, tfile->queue_index);
  2164. if (skb_xdp) {
  2165. ret = do_xdp_generic(xdp_prog, skb);
  2166. if (ret != XDP_PASS) {
  2167. ret = 0;
  2168. goto out;
  2169. }
  2170. }
  2171. if (!rcu_dereference(tun->steering_prog) && tun->numqueues > 1 &&
  2172. !tfile->detached)
  2173. rxhash = __skb_get_hash_symmetric(skb);
  2174. if (tfile->napi_enabled) {
  2175. queue = &tfile->sk.sk_write_queue;
  2176. spin_lock(&queue->lock);
  2177. if (unlikely(tfile->detached)) {
  2178. spin_unlock(&queue->lock);
  2179. kfree_skb(skb);
  2180. return -EBUSY;
  2181. }
  2182. __skb_queue_tail(queue, skb);
  2183. spin_unlock(&queue->lock);
  2184. ret = 1;
  2185. } else {
  2186. netif_receive_skb(skb);
  2187. ret = 0;
  2188. }
  2189. /* No need to disable preemption here since this function is
  2190. * always called with bh disabled
  2191. */
  2192. dev_sw_netstats_rx_add(tun->dev, datasize);
  2193. if (rxhash)
  2194. tun_flow_update(tun, rxhash, tfile);
  2195. out:
  2196. return ret;
  2197. }
  2198. static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
  2199. {
  2200. int ret, i;
  2201. struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  2202. struct tun_struct *tun = tun_get(tfile);
  2203. struct tun_msg_ctl *ctl = m->msg_control;
  2204. struct xdp_buff *xdp;
  2205. if (!tun)
  2206. return -EBADFD;
  2207. if (m->msg_controllen == sizeof(struct tun_msg_ctl) &&
  2208. ctl && ctl->type == TUN_MSG_PTR) {
  2209. struct tun_page tpage;
  2210. int n = ctl->num;
  2211. int flush = 0, queued = 0;
  2212. memset(&tpage, 0, sizeof(tpage));
  2213. local_bh_disable();
  2214. rcu_read_lock();
  2215. for (i = 0; i < n; i++) {
  2216. xdp = &((struct xdp_buff *)ctl->ptr)[i];
  2217. ret = tun_xdp_one(tun, tfile, xdp, &flush, &tpage);
  2218. if (ret > 0)
  2219. queued += ret;
  2220. }
  2221. if (flush)
  2222. xdp_do_flush();
  2223. if (tfile->napi_enabled && queued > 0)
  2224. napi_schedule(&tfile->napi);
  2225. rcu_read_unlock();
  2226. local_bh_enable();
  2227. tun_put_page(&tpage);
  2228. ret = total_len;
  2229. goto out;
  2230. }
  2231. ret = tun_get_user(tun, tfile, ctl ? ctl->ptr : NULL, &m->msg_iter,
  2232. m->msg_flags & MSG_DONTWAIT,
  2233. m->msg_flags & MSG_MORE);
  2234. out:
  2235. tun_put(tun);
  2236. return ret;
  2237. }
  2238. static int tun_recvmsg(struct socket *sock, struct msghdr *m, size_t total_len,
  2239. int flags)
  2240. {
  2241. struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  2242. struct tun_struct *tun = tun_get(tfile);
  2243. void *ptr = m->msg_control;
  2244. int ret;
  2245. if (!tun) {
  2246. ret = -EBADFD;
  2247. goto out_free;
  2248. }
  2249. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC|MSG_ERRQUEUE)) {
  2250. ret = -EINVAL;
  2251. goto out_put_tun;
  2252. }
  2253. if (flags & MSG_ERRQUEUE) {
  2254. ret = sock_recv_errqueue(sock->sk, m, total_len,
  2255. SOL_PACKET, TUN_TX_TIMESTAMP);
  2256. goto out;
  2257. }
  2258. ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, ptr);
  2259. if (ret > (ssize_t)total_len) {
  2260. m->msg_flags |= MSG_TRUNC;
  2261. ret = flags & MSG_TRUNC ? ret : total_len;
  2262. }
  2263. out:
  2264. tun_put(tun);
  2265. return ret;
  2266. out_put_tun:
  2267. tun_put(tun);
  2268. out_free:
  2269. tun_ptr_free(ptr);
  2270. return ret;
  2271. }
  2272. static int tun_ptr_peek_len(void *ptr)
  2273. {
  2274. if (likely(ptr)) {
  2275. if (tun_is_xdp_frame(ptr)) {
  2276. struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
  2277. return xdpf->len;
  2278. }
  2279. return __skb_array_len_with_tag(ptr);
  2280. } else {
  2281. return 0;
  2282. }
  2283. }
  2284. static int tun_peek_len(struct socket *sock)
  2285. {
  2286. struct tun_file *tfile = container_of(sock, struct tun_file, socket);
  2287. struct tun_struct *tun;
  2288. int ret = 0;
  2289. tun = tun_get(tfile);
  2290. if (!tun)
  2291. return 0;
  2292. ret = PTR_RING_PEEK_CALL(&tfile->tx_ring, tun_ptr_peek_len);
  2293. tun_put(tun);
  2294. return ret;
  2295. }
  2296. /* Ops structure to mimic raw sockets with tun */
  2297. static const struct proto_ops tun_socket_ops = {
  2298. .peek_len = tun_peek_len,
  2299. .sendmsg = tun_sendmsg,
  2300. .recvmsg = tun_recvmsg,
  2301. };
  2302. static struct proto tun_proto = {
  2303. .name = "tun",
  2304. .owner = THIS_MODULE,
  2305. .obj_size = sizeof(struct tun_file),
  2306. };
  2307. static int tun_flags(struct tun_struct *tun)
  2308. {
  2309. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN {
  2310. #ifdef CONFIG_KNOX_NCM
  2311. return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP | IFF_META_HDR);
  2312. #else
  2313. return tun->flags & (TUN_FEATURES | IFF_PERSIST | IFF_TUN | IFF_TAP);
  2314. #endif
  2315. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN }
  2316. }
  2317. static ssize_t tun_flags_show(struct device *dev, struct device_attribute *attr,
  2318. char *buf)
  2319. {
  2320. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  2321. return sysfs_emit(buf, "0x%x\n", tun_flags(tun));
  2322. }
  2323. static ssize_t owner_show(struct device *dev, struct device_attribute *attr,
  2324. char *buf)
  2325. {
  2326. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  2327. return uid_valid(tun->owner)?
  2328. sysfs_emit(buf, "%u\n",
  2329. from_kuid_munged(current_user_ns(), tun->owner)) :
  2330. sysfs_emit(buf, "-1\n");
  2331. }
  2332. static ssize_t group_show(struct device *dev, struct device_attribute *attr,
  2333. char *buf)
  2334. {
  2335. struct tun_struct *tun = netdev_priv(to_net_dev(dev));
  2336. return gid_valid(tun->group) ?
  2337. sysfs_emit(buf, "%u\n",
  2338. from_kgid_munged(current_user_ns(), tun->group)) :
  2339. sysfs_emit(buf, "-1\n");
  2340. }
  2341. static DEVICE_ATTR_RO(tun_flags);
  2342. static DEVICE_ATTR_RO(owner);
  2343. static DEVICE_ATTR_RO(group);
  2344. static struct attribute *tun_dev_attrs[] = {
  2345. &dev_attr_tun_flags.attr,
  2346. &dev_attr_owner.attr,
  2347. &dev_attr_group.attr,
  2348. NULL
  2349. };
  2350. static const struct attribute_group tun_attr_group = {
  2351. .attrs = tun_dev_attrs
  2352. };
  2353. static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
  2354. {
  2355. struct tun_struct *tun;
  2356. struct tun_file *tfile = file->private_data;
  2357. struct net_device *dev;
  2358. int err;
  2359. if (tfile->detached)
  2360. return -EINVAL;
  2361. if ((ifr->ifr_flags & IFF_NAPI_FRAGS)) {
  2362. if (!capable(CAP_NET_ADMIN))
  2363. return -EPERM;
  2364. if (!(ifr->ifr_flags & IFF_NAPI) ||
  2365. (ifr->ifr_flags & TUN_TYPE_MASK) != IFF_TAP)
  2366. return -EINVAL;
  2367. }
  2368. dev = __dev_get_by_name(net, ifr->ifr_name);
  2369. if (dev) {
  2370. if (ifr->ifr_flags & IFF_TUN_EXCL)
  2371. return -EBUSY;
  2372. if ((ifr->ifr_flags & IFF_TUN) && dev->netdev_ops == &tun_netdev_ops)
  2373. tun = netdev_priv(dev);
  2374. else if ((ifr->ifr_flags & IFF_TAP) && dev->netdev_ops == &tap_netdev_ops)
  2375. tun = netdev_priv(dev);
  2376. else
  2377. return -EINVAL;
  2378. if (!!(ifr->ifr_flags & IFF_MULTI_QUEUE) !=
  2379. !!(tun->flags & IFF_MULTI_QUEUE))
  2380. return -EINVAL;
  2381. if (tun_not_capable(tun))
  2382. return -EPERM;
  2383. err = security_tun_dev_open(tun->security);
  2384. if (err < 0)
  2385. return err;
  2386. err = tun_attach(tun, file, ifr->ifr_flags & IFF_NOFILTER,
  2387. ifr->ifr_flags & IFF_NAPI,
  2388. ifr->ifr_flags & IFF_NAPI_FRAGS, true);
  2389. if (err < 0)
  2390. return err;
  2391. if (tun->flags & IFF_MULTI_QUEUE &&
  2392. (tun->numqueues + tun->numdisabled > 1)) {
  2393. /* One or more queue has already been attached, no need
  2394. * to initialize the device again.
  2395. */
  2396. netdev_state_change(dev);
  2397. return 0;
  2398. }
  2399. tun->flags = (tun->flags & ~TUN_FEATURES) |
  2400. (ifr->ifr_flags & TUN_FEATURES);
  2401. netdev_state_change(dev);
  2402. } else {
  2403. char *name;
  2404. unsigned long flags = 0;
  2405. int queues = ifr->ifr_flags & IFF_MULTI_QUEUE ?
  2406. MAX_TAP_QUEUES : 1;
  2407. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2408. return -EPERM;
  2409. err = security_tun_dev_create();
  2410. if (err < 0)
  2411. return err;
  2412. /* Set dev type */
  2413. if (ifr->ifr_flags & IFF_TUN) {
  2414. /* TUN device */
  2415. flags |= IFF_TUN;
  2416. name = "tun%d";
  2417. } else if (ifr->ifr_flags & IFF_TAP) {
  2418. /* TAP device */
  2419. flags |= IFF_TAP;
  2420. name = "tap%d";
  2421. } else
  2422. return -EINVAL;
  2423. if (*ifr->ifr_name)
  2424. name = ifr->ifr_name;
  2425. dev = alloc_netdev_mqs(sizeof(struct tun_struct), name,
  2426. NET_NAME_UNKNOWN, tun_setup, queues,
  2427. queues);
  2428. if (!dev)
  2429. return -ENOMEM;
  2430. dev_net_set(dev, net);
  2431. dev->rtnl_link_ops = &tun_link_ops;
  2432. dev->ifindex = tfile->ifindex;
  2433. dev->sysfs_groups[0] = &tun_attr_group;
  2434. tun = netdev_priv(dev);
  2435. tun->dev = dev;
  2436. tun->flags = flags;
  2437. tun->txflt.count = 0;
  2438. tun->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  2439. tun->align = NET_SKB_PAD;
  2440. tun->filter_attached = false;
  2441. tun->sndbuf = tfile->socket.sk->sk_sndbuf;
  2442. tun->rx_batched = 0;
  2443. RCU_INIT_POINTER(tun->steering_prog, NULL);
  2444. tun->ifr = ifr;
  2445. tun->file = file;
  2446. tun_net_initialize(dev);
  2447. err = register_netdevice(tun->dev);
  2448. if (err < 0) {
  2449. free_netdev(dev);
  2450. return err;
  2451. }
  2452. /* free_netdev() won't check refcnt, to avoid race
  2453. * with dev_put() we need publish tun after registration.
  2454. */
  2455. rcu_assign_pointer(tfile->tun, tun);
  2456. }
  2457. if (ifr->ifr_flags & IFF_NO_CARRIER)
  2458. netif_carrier_off(tun->dev);
  2459. else
  2460. netif_carrier_on(tun->dev);
  2461. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN {
  2462. #ifdef CONFIG_KNOX_NCM
  2463. if (ifr->ifr_flags & IFF_META_HDR) {
  2464. tun->flags |= TUN_META_HDR;
  2465. } else {
  2466. tun->flags &= ~TUN_META_HDR;
  2467. }
  2468. #endif
  2469. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN {
  2470. /* Make sure persistent devices do not get stuck in
  2471. * xoff state.
  2472. */
  2473. if (netif_running(tun->dev))
  2474. netif_tx_wake_all_queues(tun->dev);
  2475. strcpy(ifr->ifr_name, tun->dev->name);
  2476. return 0;
  2477. }
  2478. static void tun_get_iff(struct tun_struct *tun, struct ifreq *ifr)
  2479. {
  2480. strcpy(ifr->ifr_name, tun->dev->name);
  2481. ifr->ifr_flags = tun_flags(tun);
  2482. }
  2483. /* This is like a cut-down ethtool ops, except done via tun fd so no
  2484. * privs required. */
  2485. static int set_offload(struct tun_struct *tun, unsigned long arg)
  2486. {
  2487. netdev_features_t features = 0;
  2488. if (arg & TUN_F_CSUM) {
  2489. features |= NETIF_F_HW_CSUM;
  2490. arg &= ~TUN_F_CSUM;
  2491. if (arg & (TUN_F_TSO4|TUN_F_TSO6)) {
  2492. if (arg & TUN_F_TSO_ECN) {
  2493. features |= NETIF_F_TSO_ECN;
  2494. arg &= ~TUN_F_TSO_ECN;
  2495. }
  2496. if (arg & TUN_F_TSO4)
  2497. features |= NETIF_F_TSO;
  2498. if (arg & TUN_F_TSO6)
  2499. features |= NETIF_F_TSO6;
  2500. arg &= ~(TUN_F_TSO4|TUN_F_TSO6);
  2501. }
  2502. arg &= ~TUN_F_UFO;
  2503. }
  2504. /* This gives the user a way to test for new features in future by
  2505. * trying to set them. */
  2506. if (arg)
  2507. return -EINVAL;
  2508. tun->set_features = features;
  2509. tun->dev->wanted_features &= ~TUN_USER_FEATURES;
  2510. tun->dev->wanted_features |= features;
  2511. netdev_update_features(tun->dev);
  2512. return 0;
  2513. }
  2514. static void tun_detach_filter(struct tun_struct *tun, int n)
  2515. {
  2516. int i;
  2517. struct tun_file *tfile;
  2518. for (i = 0; i < n; i++) {
  2519. tfile = rtnl_dereference(tun->tfiles[i]);
  2520. lock_sock(tfile->socket.sk);
  2521. sk_detach_filter(tfile->socket.sk);
  2522. release_sock(tfile->socket.sk);
  2523. }
  2524. tun->filter_attached = false;
  2525. }
  2526. static int tun_attach_filter(struct tun_struct *tun)
  2527. {
  2528. int i, ret = 0;
  2529. struct tun_file *tfile;
  2530. for (i = 0; i < tun->numqueues; i++) {
  2531. tfile = rtnl_dereference(tun->tfiles[i]);
  2532. lock_sock(tfile->socket.sk);
  2533. ret = sk_attach_filter(&tun->fprog, tfile->socket.sk);
  2534. release_sock(tfile->socket.sk);
  2535. if (ret) {
  2536. tun_detach_filter(tun, i);
  2537. return ret;
  2538. }
  2539. }
  2540. tun->filter_attached = true;
  2541. return ret;
  2542. }
  2543. static void tun_set_sndbuf(struct tun_struct *tun)
  2544. {
  2545. struct tun_file *tfile;
  2546. int i;
  2547. for (i = 0; i < tun->numqueues; i++) {
  2548. tfile = rtnl_dereference(tun->tfiles[i]);
  2549. tfile->socket.sk->sk_sndbuf = tun->sndbuf;
  2550. }
  2551. }
  2552. static int tun_set_queue(struct file *file, struct ifreq *ifr)
  2553. {
  2554. struct tun_file *tfile = file->private_data;
  2555. struct tun_struct *tun;
  2556. int ret = 0;
  2557. rtnl_lock();
  2558. if (ifr->ifr_flags & IFF_ATTACH_QUEUE) {
  2559. tun = tfile->detached;
  2560. if (!tun) {
  2561. ret = -EINVAL;
  2562. goto unlock;
  2563. }
  2564. ret = security_tun_dev_attach_queue(tun->security);
  2565. if (ret < 0)
  2566. goto unlock;
  2567. ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI,
  2568. tun->flags & IFF_NAPI_FRAGS, true);
  2569. } else if (ifr->ifr_flags & IFF_DETACH_QUEUE) {
  2570. tun = rtnl_dereference(tfile->tun);
  2571. if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached)
  2572. ret = -EINVAL;
  2573. else
  2574. __tun_detach(tfile, false);
  2575. } else
  2576. ret = -EINVAL;
  2577. if (ret >= 0)
  2578. netdev_state_change(tun->dev);
  2579. unlock:
  2580. rtnl_unlock();
  2581. return ret;
  2582. }
  2583. static int tun_set_ebpf(struct tun_struct *tun, struct tun_prog __rcu **prog_p,
  2584. void __user *data)
  2585. {
  2586. struct bpf_prog *prog;
  2587. int fd;
  2588. if (copy_from_user(&fd, data, sizeof(fd)))
  2589. return -EFAULT;
  2590. if (fd == -1) {
  2591. prog = NULL;
  2592. } else {
  2593. prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER);
  2594. if (IS_ERR(prog))
  2595. return PTR_ERR(prog);
  2596. }
  2597. return __tun_set_ebpf(tun, prog_p, prog);
  2598. }
  2599. /* Return correct value for tun->dev->addr_len based on tun->dev->type. */
  2600. static unsigned char tun_get_addr_len(unsigned short type)
  2601. {
  2602. switch (type) {
  2603. case ARPHRD_IP6GRE:
  2604. case ARPHRD_TUNNEL6:
  2605. return sizeof(struct in6_addr);
  2606. case ARPHRD_IPGRE:
  2607. case ARPHRD_TUNNEL:
  2608. case ARPHRD_SIT:
  2609. return 4;
  2610. case ARPHRD_ETHER:
  2611. return ETH_ALEN;
  2612. case ARPHRD_IEEE802154:
  2613. case ARPHRD_IEEE802154_MONITOR:
  2614. return IEEE802154_EXTENDED_ADDR_LEN;
  2615. case ARPHRD_PHONET_PIPE:
  2616. case ARPHRD_PPP:
  2617. case ARPHRD_NONE:
  2618. return 0;
  2619. case ARPHRD_6LOWPAN:
  2620. return EUI64_ADDR_LEN;
  2621. case ARPHRD_FDDI:
  2622. return FDDI_K_ALEN;
  2623. case ARPHRD_HIPPI:
  2624. return HIPPI_ALEN;
  2625. case ARPHRD_IEEE802:
  2626. return FC_ALEN;
  2627. case ARPHRD_ROSE:
  2628. return ROSE_ADDR_LEN;
  2629. case ARPHRD_NETROM:
  2630. return AX25_ADDR_LEN;
  2631. case ARPHRD_LOCALTLK:
  2632. return LTALK_ALEN;
  2633. default:
  2634. return 0;
  2635. }
  2636. }
  2637. static long __tun_chr_ioctl(struct file *file, unsigned int cmd,
  2638. unsigned long arg, int ifreq_len)
  2639. {
  2640. struct tun_file *tfile = file->private_data;
  2641. struct net *net = sock_net(&tfile->sk);
  2642. struct tun_struct *tun;
  2643. void __user* argp = (void __user*)arg;
  2644. unsigned int carrier;
  2645. struct ifreq ifr;
  2646. kuid_t owner;
  2647. kgid_t group;
  2648. int ifindex;
  2649. int sndbuf;
  2650. int vnet_hdr_sz;
  2651. int le;
  2652. int ret;
  2653. bool do_notify = false;
  2654. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN {
  2655. #ifdef CONFIG_KNOX_NCM
  2656. int knox_flag = 0;
  2657. int tun_meta_param;
  2658. int tun_meta_value;
  2659. #endif
  2660. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN }
  2661. if (cmd == TUNSETIFF || cmd == TUNSETQUEUE ||
  2662. (_IOC_TYPE(cmd) == SOCK_IOC_TYPE && cmd != SIOCGSKNS)) {
  2663. if (copy_from_user(&ifr, argp, ifreq_len))
  2664. return -EFAULT;
  2665. } else {
  2666. memset(&ifr, 0, sizeof(ifr));
  2667. }
  2668. if (cmd == TUNGETFEATURES) {
  2669. /* Currently this just means: "what IFF flags are valid?".
  2670. * This is needed because we never checked for invalid flags on
  2671. * TUNSETIFF.
  2672. */
  2673. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN {
  2674. #ifdef CONFIG_KNOX_NCM
  2675. knox_flag |= IFF_META_HDR;
  2676. return put_user(IFF_TUN | IFF_TAP| IFF_NO_CARRIER | TUN_FEATURES | knox_flag,
  2677. (unsigned int __user*)argp);
  2678. #else
  2679. return put_user(IFF_TUN | IFF_TAP | IFF_NO_CARRIER | TUN_FEATURES,
  2680. (unsigned int __user*)argp);
  2681. #endif
  2682. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN }
  2683. } else if (cmd == TUNSETQUEUE) {
  2684. return tun_set_queue(file, &ifr);
  2685. } else if (cmd == SIOCGSKNS) {
  2686. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2687. return -EPERM;
  2688. return open_related_ns(&net->ns, get_net_ns);
  2689. }
  2690. rtnl_lock();
  2691. tun = tun_get(tfile);
  2692. if (cmd == TUNSETIFF) {
  2693. ret = -EEXIST;
  2694. if (tun)
  2695. goto unlock;
  2696. ifr.ifr_name[IFNAMSIZ-1] = '\0';
  2697. ret = tun_set_iff(net, file, &ifr);
  2698. if (ret)
  2699. goto unlock;
  2700. if (copy_to_user(argp, &ifr, ifreq_len))
  2701. ret = -EFAULT;
  2702. goto unlock;
  2703. }
  2704. if (cmd == TUNSETIFINDEX) {
  2705. ret = -EPERM;
  2706. if (tun)
  2707. goto unlock;
  2708. ret = -EFAULT;
  2709. if (copy_from_user(&ifindex, argp, sizeof(ifindex)))
  2710. goto unlock;
  2711. ret = -EINVAL;
  2712. if (ifindex < 0)
  2713. goto unlock;
  2714. ret = 0;
  2715. tfile->ifindex = ifindex;
  2716. goto unlock;
  2717. }
  2718. ret = -EBADFD;
  2719. if (!tun)
  2720. goto unlock;
  2721. netif_info(tun, drv, tun->dev, "tun_chr_ioctl cmd %u\n", cmd);
  2722. net = dev_net(tun->dev);
  2723. ret = 0;
  2724. switch (cmd) {
  2725. case TUNGETIFF:
  2726. tun_get_iff(tun, &ifr);
  2727. if (tfile->detached)
  2728. ifr.ifr_flags |= IFF_DETACH_QUEUE;
  2729. if (!tfile->socket.sk->sk_filter)
  2730. ifr.ifr_flags |= IFF_NOFILTER;
  2731. if (copy_to_user(argp, &ifr, ifreq_len))
  2732. ret = -EFAULT;
  2733. break;
  2734. case TUNSETNOCSUM:
  2735. /* Disable/Enable checksum */
  2736. /* [unimplemented] */
  2737. netif_info(tun, drv, tun->dev, "ignored: set checksum %s\n",
  2738. arg ? "disabled" : "enabled");
  2739. break;
  2740. case TUNSETPERSIST:
  2741. /* Disable/Enable persist mode. Keep an extra reference to the
  2742. * module to prevent the module being unprobed.
  2743. */
  2744. if (arg && !(tun->flags & IFF_PERSIST)) {
  2745. tun->flags |= IFF_PERSIST;
  2746. __module_get(THIS_MODULE);
  2747. do_notify = true;
  2748. }
  2749. if (!arg && (tun->flags & IFF_PERSIST)) {
  2750. tun->flags &= ~IFF_PERSIST;
  2751. module_put(THIS_MODULE);
  2752. do_notify = true;
  2753. }
  2754. netif_info(tun, drv, tun->dev, "persist %s\n",
  2755. arg ? "enabled" : "disabled");
  2756. break;
  2757. case TUNSETOWNER:
  2758. /* Set owner of the device */
  2759. owner = make_kuid(current_user_ns(), arg);
  2760. if (!uid_valid(owner)) {
  2761. ret = -EINVAL;
  2762. break;
  2763. }
  2764. tun->owner = owner;
  2765. do_notify = true;
  2766. netif_info(tun, drv, tun->dev, "owner set to %u\n",
  2767. from_kuid(&init_user_ns, tun->owner));
  2768. break;
  2769. case TUNSETGROUP:
  2770. /* Set group of the device */
  2771. group = make_kgid(current_user_ns(), arg);
  2772. if (!gid_valid(group)) {
  2773. ret = -EINVAL;
  2774. break;
  2775. }
  2776. tun->group = group;
  2777. do_notify = true;
  2778. netif_info(tun, drv, tun->dev, "group set to %u\n",
  2779. from_kgid(&init_user_ns, tun->group));
  2780. break;
  2781. case TUNSETLINK:
  2782. /* Only allow setting the type when the interface is down */
  2783. if (tun->dev->flags & IFF_UP) {
  2784. netif_info(tun, drv, tun->dev,
  2785. "Linktype set failed because interface is up\n");
  2786. ret = -EBUSY;
  2787. } else {
  2788. ret = call_netdevice_notifiers(NETDEV_PRE_TYPE_CHANGE,
  2789. tun->dev);
  2790. ret = notifier_to_errno(ret);
  2791. if (ret) {
  2792. netif_info(tun, drv, tun->dev,
  2793. "Refused to change device type\n");
  2794. break;
  2795. }
  2796. tun->dev->type = (int) arg;
  2797. tun->dev->addr_len = tun_get_addr_len(tun->dev->type);
  2798. netif_info(tun, drv, tun->dev, "linktype set to %d\n",
  2799. tun->dev->type);
  2800. call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE,
  2801. tun->dev);
  2802. }
  2803. break;
  2804. case TUNSETDEBUG:
  2805. tun->msg_enable = (u32)arg;
  2806. break;
  2807. case TUNSETOFFLOAD:
  2808. ret = set_offload(tun, arg);
  2809. break;
  2810. case TUNSETTXFILTER:
  2811. /* Can be set only for TAPs */
  2812. ret = -EINVAL;
  2813. if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
  2814. break;
  2815. ret = update_filter(&tun->txflt, (void __user *)arg);
  2816. break;
  2817. case SIOCGIFHWADDR:
  2818. /* Get hw address */
  2819. dev_get_mac_address(&ifr.ifr_hwaddr, net, tun->dev->name);
  2820. if (copy_to_user(argp, &ifr, ifreq_len))
  2821. ret = -EFAULT;
  2822. break;
  2823. case SIOCSIFHWADDR:
  2824. /* Set hw address */
  2825. ret = dev_set_mac_address_user(tun->dev, &ifr.ifr_hwaddr, NULL);
  2826. break;
  2827. case TUNGETSNDBUF:
  2828. sndbuf = tfile->socket.sk->sk_sndbuf;
  2829. if (copy_to_user(argp, &sndbuf, sizeof(sndbuf)))
  2830. ret = -EFAULT;
  2831. break;
  2832. case TUNSETSNDBUF:
  2833. if (copy_from_user(&sndbuf, argp, sizeof(sndbuf))) {
  2834. ret = -EFAULT;
  2835. break;
  2836. }
  2837. if (sndbuf <= 0) {
  2838. ret = -EINVAL;
  2839. break;
  2840. }
  2841. tun->sndbuf = sndbuf;
  2842. tun_set_sndbuf(tun);
  2843. break;
  2844. case TUNGETVNETHDRSZ:
  2845. vnet_hdr_sz = tun->vnet_hdr_sz;
  2846. if (copy_to_user(argp, &vnet_hdr_sz, sizeof(vnet_hdr_sz)))
  2847. ret = -EFAULT;
  2848. break;
  2849. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN {
  2850. #ifdef CONFIG_KNOX_NCM
  2851. case TUNGETMETAPARAM:
  2852. if (copy_from_user(&tun_meta_param, argp,
  2853. sizeof(tun_meta_param))) {
  2854. ret = -EFAULT;
  2855. break;
  2856. }
  2857. ret = 0;
  2858. switch (tun_meta_param) {
  2859. case TUN_GET_META_HDR_SZ:
  2860. tun_meta_value = TUN_META_HDR_SZ;
  2861. break;
  2862. case TUN_GET_META_MARK_OFFSET:
  2863. tun_meta_value = TUN_META_MARK_OFFSET;
  2864. break;
  2865. default:
  2866. ret = -EINVAL;
  2867. break;
  2868. }
  2869. if (!ret) {
  2870. if (copy_to_user(argp, &tun_meta_value,
  2871. sizeof(tun_meta_value)))
  2872. ret = -EFAULT;
  2873. }
  2874. break;
  2875. #endif
  2876. // SEC_PRODUCT_FEATURE_KNOX_SUPPORT_VPN }
  2877. case TUNSETVNETHDRSZ:
  2878. if (copy_from_user(&vnet_hdr_sz, argp, sizeof(vnet_hdr_sz))) {
  2879. ret = -EFAULT;
  2880. break;
  2881. }
  2882. if (vnet_hdr_sz < (int)sizeof(struct virtio_net_hdr)) {
  2883. ret = -EINVAL;
  2884. break;
  2885. }
  2886. tun->vnet_hdr_sz = vnet_hdr_sz;
  2887. break;
  2888. case TUNGETVNETLE:
  2889. le = !!(tun->flags & TUN_VNET_LE);
  2890. if (put_user(le, (int __user *)argp))
  2891. ret = -EFAULT;
  2892. break;
  2893. case TUNSETVNETLE:
  2894. if (get_user(le, (int __user *)argp)) {
  2895. ret = -EFAULT;
  2896. break;
  2897. }
  2898. if (le)
  2899. tun->flags |= TUN_VNET_LE;
  2900. else
  2901. tun->flags &= ~TUN_VNET_LE;
  2902. break;
  2903. case TUNGETVNETBE:
  2904. ret = tun_get_vnet_be(tun, argp);
  2905. break;
  2906. case TUNSETVNETBE:
  2907. ret = tun_set_vnet_be(tun, argp);
  2908. break;
  2909. case TUNATTACHFILTER:
  2910. /* Can be set only for TAPs */
  2911. ret = -EINVAL;
  2912. if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
  2913. break;
  2914. ret = -EFAULT;
  2915. if (copy_from_user(&tun->fprog, argp, sizeof(tun->fprog)))
  2916. break;
  2917. ret = tun_attach_filter(tun);
  2918. break;
  2919. case TUNDETACHFILTER:
  2920. /* Can be set only for TAPs */
  2921. ret = -EINVAL;
  2922. if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
  2923. break;
  2924. ret = 0;
  2925. tun_detach_filter(tun, tun->numqueues);
  2926. break;
  2927. case TUNGETFILTER:
  2928. ret = -EINVAL;
  2929. if ((tun->flags & TUN_TYPE_MASK) != IFF_TAP)
  2930. break;
  2931. ret = -EFAULT;
  2932. if (copy_to_user(argp, &tun->fprog, sizeof(tun->fprog)))
  2933. break;
  2934. ret = 0;
  2935. break;
  2936. case TUNSETSTEERINGEBPF:
  2937. ret = tun_set_ebpf(tun, &tun->steering_prog, argp);
  2938. break;
  2939. case TUNSETFILTEREBPF:
  2940. ret = tun_set_ebpf(tun, &tun->filter_prog, argp);
  2941. break;
  2942. case TUNSETCARRIER:
  2943. ret = -EFAULT;
  2944. if (copy_from_user(&carrier, argp, sizeof(carrier)))
  2945. goto unlock;
  2946. ret = tun_net_change_carrier(tun->dev, (bool)carrier);
  2947. break;
  2948. case TUNGETDEVNETNS:
  2949. ret = -EPERM;
  2950. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  2951. goto unlock;
  2952. ret = open_related_ns(&net->ns, get_net_ns);
  2953. break;
  2954. default:
  2955. ret = -EINVAL;
  2956. break;
  2957. }
  2958. if (do_notify)
  2959. netdev_state_change(tun->dev);
  2960. unlock:
  2961. rtnl_unlock();
  2962. if (tun)
  2963. tun_put(tun);
  2964. return ret;
  2965. }
  2966. static long tun_chr_ioctl(struct file *file,
  2967. unsigned int cmd, unsigned long arg)
  2968. {
  2969. return __tun_chr_ioctl(file, cmd, arg, sizeof (struct ifreq));
  2970. }
  2971. #ifdef CONFIG_COMPAT
  2972. static long tun_chr_compat_ioctl(struct file *file,
  2973. unsigned int cmd, unsigned long arg)
  2974. {
  2975. switch (cmd) {
  2976. case TUNSETIFF:
  2977. case TUNGETIFF:
  2978. case TUNSETTXFILTER:
  2979. case TUNGETSNDBUF:
  2980. case TUNSETSNDBUF:
  2981. case SIOCGIFHWADDR:
  2982. case SIOCSIFHWADDR:
  2983. arg = (unsigned long)compat_ptr(arg);
  2984. break;
  2985. default:
  2986. arg = (compat_ulong_t)arg;
  2987. break;
  2988. }
  2989. /*
  2990. * compat_ifreq is shorter than ifreq, so we must not access beyond
  2991. * the end of that structure. All fields that are used in this
  2992. * driver are compatible though, we don't need to convert the
  2993. * contents.
  2994. */
  2995. return __tun_chr_ioctl(file, cmd, arg, sizeof(struct compat_ifreq));
  2996. }
  2997. #endif /* CONFIG_COMPAT */
  2998. static int tun_chr_fasync(int fd, struct file *file, int on)
  2999. {
  3000. struct tun_file *tfile = file->private_data;
  3001. int ret;
  3002. if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0)
  3003. goto out;
  3004. if (on) {
  3005. __f_setown(file, task_pid(current), PIDTYPE_TGID, 0);
  3006. tfile->flags |= TUN_FASYNC;
  3007. } else
  3008. tfile->flags &= ~TUN_FASYNC;
  3009. ret = 0;
  3010. out:
  3011. return ret;
  3012. }
  3013. static int tun_chr_open(struct inode *inode, struct file * file)
  3014. {
  3015. struct net *net = current->nsproxy->net_ns;
  3016. struct tun_file *tfile;
  3017. tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
  3018. &tun_proto, 0);
  3019. if (!tfile)
  3020. return -ENOMEM;
  3021. if (ptr_ring_init(&tfile->tx_ring, 0, GFP_KERNEL)) {
  3022. sk_free(&tfile->sk);
  3023. return -ENOMEM;
  3024. }
  3025. mutex_init(&tfile->napi_mutex);
  3026. RCU_INIT_POINTER(tfile->tun, NULL);
  3027. tfile->flags = 0;
  3028. tfile->ifindex = 0;
  3029. init_waitqueue_head(&tfile->socket.wq.wait);
  3030. tfile->socket.file = file;
  3031. tfile->socket.ops = &tun_socket_ops;
  3032. sock_init_data_uid(&tfile->socket, &tfile->sk, current_fsuid());
  3033. tfile->sk.sk_write_space = tun_sock_write_space;
  3034. tfile->sk.sk_sndbuf = INT_MAX;
  3035. file->private_data = tfile;
  3036. INIT_LIST_HEAD(&tfile->next);
  3037. sock_set_flag(&tfile->sk, SOCK_ZEROCOPY);
  3038. return 0;
  3039. }
  3040. static int tun_chr_close(struct inode *inode, struct file *file)
  3041. {
  3042. struct tun_file *tfile = file->private_data;
  3043. tun_detach(tfile, true);
  3044. return 0;
  3045. }
  3046. #ifdef CONFIG_PROC_FS
  3047. static void tun_chr_show_fdinfo(struct seq_file *m, struct file *file)
  3048. {
  3049. struct tun_file *tfile = file->private_data;
  3050. struct tun_struct *tun;
  3051. struct ifreq ifr;
  3052. memset(&ifr, 0, sizeof(ifr));
  3053. rtnl_lock();
  3054. tun = tun_get(tfile);
  3055. if (tun)
  3056. tun_get_iff(tun, &ifr);
  3057. rtnl_unlock();
  3058. if (tun)
  3059. tun_put(tun);
  3060. seq_printf(m, "iff:\t%s\n", ifr.ifr_name);
  3061. }
  3062. #endif
  3063. static const struct file_operations tun_fops = {
  3064. .owner = THIS_MODULE,
  3065. .llseek = no_llseek,
  3066. .read_iter = tun_chr_read_iter,
  3067. .write_iter = tun_chr_write_iter,
  3068. .poll = tun_chr_poll,
  3069. .unlocked_ioctl = tun_chr_ioctl,
  3070. #ifdef CONFIG_COMPAT
  3071. .compat_ioctl = tun_chr_compat_ioctl,
  3072. #endif
  3073. .open = tun_chr_open,
  3074. .release = tun_chr_close,
  3075. .fasync = tun_chr_fasync,
  3076. #ifdef CONFIG_PROC_FS
  3077. .show_fdinfo = tun_chr_show_fdinfo,
  3078. #endif
  3079. };
  3080. static struct miscdevice tun_miscdev = {
  3081. .minor = TUN_MINOR,
  3082. .name = "tun",
  3083. .nodename = "net/tun",
  3084. .fops = &tun_fops,
  3085. };
  3086. /* ethtool interface */
  3087. static void tun_default_link_ksettings(struct net_device *dev,
  3088. struct ethtool_link_ksettings *cmd)
  3089. {
  3090. ethtool_link_ksettings_zero_link_mode(cmd, supported);
  3091. ethtool_link_ksettings_zero_link_mode(cmd, advertising);
  3092. cmd->base.speed = SPEED_10;
  3093. cmd->base.duplex = DUPLEX_FULL;
  3094. cmd->base.port = PORT_TP;
  3095. cmd->base.phy_address = 0;
  3096. cmd->base.autoneg = AUTONEG_DISABLE;
  3097. }
  3098. static int tun_get_link_ksettings(struct net_device *dev,
  3099. struct ethtool_link_ksettings *cmd)
  3100. {
  3101. struct tun_struct *tun = netdev_priv(dev);
  3102. memcpy(cmd, &tun->link_ksettings, sizeof(*cmd));
  3103. return 0;
  3104. }
  3105. static int tun_set_link_ksettings(struct net_device *dev,
  3106. const struct ethtool_link_ksettings *cmd)
  3107. {
  3108. struct tun_struct *tun = netdev_priv(dev);
  3109. memcpy(&tun->link_ksettings, cmd, sizeof(*cmd));
  3110. return 0;
  3111. }
  3112. static void tun_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  3113. {
  3114. struct tun_struct *tun = netdev_priv(dev);
  3115. strscpy(info->driver, DRV_NAME, sizeof(info->driver));
  3116. strscpy(info->version, DRV_VERSION, sizeof(info->version));
  3117. switch (tun->flags & TUN_TYPE_MASK) {
  3118. case IFF_TUN:
  3119. strscpy(info->bus_info, "tun", sizeof(info->bus_info));
  3120. break;
  3121. case IFF_TAP:
  3122. strscpy(info->bus_info, "tap", sizeof(info->bus_info));
  3123. break;
  3124. }
  3125. }
  3126. static u32 tun_get_msglevel(struct net_device *dev)
  3127. {
  3128. struct tun_struct *tun = netdev_priv(dev);
  3129. return tun->msg_enable;
  3130. }
  3131. static void tun_set_msglevel(struct net_device *dev, u32 value)
  3132. {
  3133. struct tun_struct *tun = netdev_priv(dev);
  3134. tun->msg_enable = value;
  3135. }
  3136. static int tun_get_coalesce(struct net_device *dev,
  3137. struct ethtool_coalesce *ec,
  3138. struct kernel_ethtool_coalesce *kernel_coal,
  3139. struct netlink_ext_ack *extack)
  3140. {
  3141. struct tun_struct *tun = netdev_priv(dev);
  3142. ec->rx_max_coalesced_frames = tun->rx_batched;
  3143. return 0;
  3144. }
  3145. static int tun_set_coalesce(struct net_device *dev,
  3146. struct ethtool_coalesce *ec,
  3147. struct kernel_ethtool_coalesce *kernel_coal,
  3148. struct netlink_ext_ack *extack)
  3149. {
  3150. struct tun_struct *tun = netdev_priv(dev);
  3151. if (ec->rx_max_coalesced_frames > NAPI_POLL_WEIGHT)
  3152. tun->rx_batched = NAPI_POLL_WEIGHT;
  3153. else
  3154. tun->rx_batched = ec->rx_max_coalesced_frames;
  3155. return 0;
  3156. }
  3157. static const struct ethtool_ops tun_ethtool_ops = {
  3158. .supported_coalesce_params = ETHTOOL_COALESCE_RX_MAX_FRAMES,
  3159. .get_drvinfo = tun_get_drvinfo,
  3160. .get_msglevel = tun_get_msglevel,
  3161. .set_msglevel = tun_set_msglevel,
  3162. .get_link = ethtool_op_get_link,
  3163. .get_ts_info = ethtool_op_get_ts_info,
  3164. .get_coalesce = tun_get_coalesce,
  3165. .set_coalesce = tun_set_coalesce,
  3166. .get_link_ksettings = tun_get_link_ksettings,
  3167. .set_link_ksettings = tun_set_link_ksettings,
  3168. };
  3169. static int tun_queue_resize(struct tun_struct *tun)
  3170. {
  3171. struct net_device *dev = tun->dev;
  3172. struct tun_file *tfile;
  3173. struct ptr_ring **rings;
  3174. int n = tun->numqueues + tun->numdisabled;
  3175. int ret, i;
  3176. rings = kmalloc_array(n, sizeof(*rings), GFP_KERNEL);
  3177. if (!rings)
  3178. return -ENOMEM;
  3179. for (i = 0; i < tun->numqueues; i++) {
  3180. tfile = rtnl_dereference(tun->tfiles[i]);
  3181. rings[i] = &tfile->tx_ring;
  3182. }
  3183. list_for_each_entry(tfile, &tun->disabled, next)
  3184. rings[i++] = &tfile->tx_ring;
  3185. ret = ptr_ring_resize_multiple(rings, n,
  3186. dev->tx_queue_len, GFP_KERNEL,
  3187. tun_ptr_free);
  3188. kfree(rings);
  3189. return ret;
  3190. }
  3191. static int tun_device_event(struct notifier_block *unused,
  3192. unsigned long event, void *ptr)
  3193. {
  3194. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  3195. struct tun_struct *tun = netdev_priv(dev);
  3196. int i;
  3197. if (dev->rtnl_link_ops != &tun_link_ops)
  3198. return NOTIFY_DONE;
  3199. switch (event) {
  3200. case NETDEV_CHANGE_TX_QUEUE_LEN:
  3201. if (tun_queue_resize(tun))
  3202. return NOTIFY_BAD;
  3203. break;
  3204. case NETDEV_UP:
  3205. for (i = 0; i < tun->numqueues; i++) {
  3206. struct tun_file *tfile;
  3207. tfile = rtnl_dereference(tun->tfiles[i]);
  3208. tfile->socket.sk->sk_write_space(tfile->socket.sk);
  3209. }
  3210. break;
  3211. default:
  3212. break;
  3213. }
  3214. return NOTIFY_DONE;
  3215. }
  3216. static struct notifier_block tun_notifier_block __read_mostly = {
  3217. .notifier_call = tun_device_event,
  3218. };
  3219. static int __init tun_init(void)
  3220. {
  3221. int ret = 0;
  3222. pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
  3223. ret = rtnl_link_register(&tun_link_ops);
  3224. if (ret) {
  3225. pr_err("Can't register link_ops\n");
  3226. goto err_linkops;
  3227. }
  3228. ret = misc_register(&tun_miscdev);
  3229. if (ret) {
  3230. pr_err("Can't register misc device %d\n", TUN_MINOR);
  3231. goto err_misc;
  3232. }
  3233. ret = register_netdevice_notifier(&tun_notifier_block);
  3234. if (ret) {
  3235. pr_err("Can't register netdevice notifier\n");
  3236. goto err_notifier;
  3237. }
  3238. return 0;
  3239. err_notifier:
  3240. misc_deregister(&tun_miscdev);
  3241. err_misc:
  3242. rtnl_link_unregister(&tun_link_ops);
  3243. err_linkops:
  3244. return ret;
  3245. }
  3246. static void tun_cleanup(void)
  3247. {
  3248. misc_deregister(&tun_miscdev);
  3249. rtnl_link_unregister(&tun_link_ops);
  3250. unregister_netdevice_notifier(&tun_notifier_block);
  3251. }
  3252. /* Get an underlying socket object from tun file. Returns error unless file is
  3253. * attached to a device. The returned object works like a packet socket, it
  3254. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  3255. * holding a reference to the file for as long as the socket is in use. */
  3256. struct socket *tun_get_socket(struct file *file)
  3257. {
  3258. struct tun_file *tfile;
  3259. if (file->f_op != &tun_fops)
  3260. return ERR_PTR(-EINVAL);
  3261. tfile = file->private_data;
  3262. if (!tfile)
  3263. return ERR_PTR(-EBADFD);
  3264. return &tfile->socket;
  3265. }
  3266. EXPORT_SYMBOL_GPL(tun_get_socket);
  3267. struct ptr_ring *tun_get_tx_ring(struct file *file)
  3268. {
  3269. struct tun_file *tfile;
  3270. if (file->f_op != &tun_fops)
  3271. return ERR_PTR(-EINVAL);
  3272. tfile = file->private_data;
  3273. if (!tfile)
  3274. return ERR_PTR(-EBADFD);
  3275. return &tfile->tx_ring;
  3276. }
  3277. EXPORT_SYMBOL_GPL(tun_get_tx_ring);
  3278. module_init(tun_init);
  3279. module_exit(tun_cleanup);
  3280. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  3281. MODULE_AUTHOR(DRV_COPYRIGHT);
  3282. MODULE_LICENSE("GPL");
  3283. MODULE_ALIAS_MISCDEV(TUN_MINOR);
  3284. MODULE_ALIAS("devname:net/tun");