socket.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * NET An implementation of the SOCKET network access protocol.
  4. *
  5. * Version: @(#)socket.c 1.1.93 18/02/95
  6. *
  7. * Authors: Orest Zborowski, <obz@Kodak.COM>
  8. * Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10. *
  11. * Fixes:
  12. * Anonymous : NOTSOCK/BADF cleanup. Error fix in
  13. * shutdown()
  14. * Alan Cox : verify_area() fixes
  15. * Alan Cox : Removed DDI
  16. * Jonathan Kamens : SOCK_DGRAM reconnect bug
  17. * Alan Cox : Moved a load of checks to the very
  18. * top level.
  19. * Alan Cox : Move address structures to/from user
  20. * mode above the protocol layers.
  21. * Rob Janssen : Allow 0 length sends.
  22. * Alan Cox : Asynchronous I/O support (cribbed from the
  23. * tty drivers).
  24. * Niibe Yutaka : Asynchronous I/O for writes (4.4BSD style)
  25. * Jeff Uphoff : Made max number of sockets command-line
  26. * configurable.
  27. * Matti Aarnio : Made the number of sockets dynamic,
  28. * to be allocated when needed, and mr.
  29. * Uphoff's max is used as max to be
  30. * allowed to allocate.
  31. * Linus : Argh. removed all the socket allocation
  32. * altogether: it's in the inode now.
  33. * Alan Cox : Made sock_alloc()/sock_release() public
  34. * for NetROM and future kernel nfsd type
  35. * stuff.
  36. * Alan Cox : sendmsg/recvmsg basics.
  37. * Tom Dyas : Export net symbols.
  38. * Marcin Dalecki : Fixed problems with CONFIG_NET="n".
  39. * Alan Cox : Added thread locking to sys_* calls
  40. * for sockets. May have errors at the
  41. * moment.
  42. * Kevin Buhr : Fixed the dumb errors in the above.
  43. * Andi Kleen : Some small cleanups, optimizations,
  44. * and fixed a copy_from_user() bug.
  45. * Tigran Aivazian : sys_send(args) calls sys_sendto(args, NULL, 0)
  46. * Tigran Aivazian : Made listen(2) backlog sanity checks
  47. * protocol-independent
  48. *
  49. * This module is effectively the top level interface to the BSD socket
  50. * paradigm.
  51. *
  52. * Based upon Swansea University Computer Society NET3.039
  53. */
  54. #include <linux/bpf-cgroup.h>
  55. #include <linux/ethtool.h>
  56. #include <linux/mm.h>
  57. #include <linux/socket.h>
  58. #include <linux/file.h>
  59. #include <linux/splice.h>
  60. #include <linux/net.h>
  61. #include <linux/interrupt.h>
  62. #include <linux/thread_info.h>
  63. #include <linux/rcupdate.h>
  64. #include <linux/netdevice.h>
  65. #include <linux/proc_fs.h>
  66. #include <linux/seq_file.h>
  67. #include <linux/mutex.h>
  68. #include <linux/if_bridge.h>
  69. #include <linux/if_vlan.h>
  70. #include <linux/ptp_classify.h>
  71. #include <linux/init.h>
  72. #include <linux/poll.h>
  73. #include <linux/cache.h>
  74. #include <linux/module.h>
  75. #include <linux/highmem.h>
  76. #include <linux/mount.h>
  77. #include <linux/pseudo_fs.h>
  78. #include <linux/security.h>
  79. #include <linux/syscalls.h>
  80. #include <linux/compat.h>
  81. #include <linux/kmod.h>
  82. #include <linux/audit.h>
  83. #include <linux/wireless.h>
  84. #include <linux/nsproxy.h>
  85. #include <linux/magic.h>
  86. #include <linux/slab.h>
  87. #include <linux/xattr.h>
  88. #include <linux/nospec.h>
  89. #include <linux/indirect_call_wrapper.h>
  90. #include <linux/io_uring/net.h>
  91. #include <linux/uaccess.h>
  92. #include <asm/unistd.h>
  93. #include <net/compat.h>
  94. #include <net/wext.h>
  95. #include <net/cls_cgroup.h>
  96. #include <net/sock.h>
  97. #include <linux/netfilter.h>
  98. #include <linux/if_tun.h>
  99. #include <linux/ipv6_route.h>
  100. #include <linux/route.h>
  101. #include <linux/termios.h>
  102. #include <linux/sockios.h>
  103. #include <net/busy_poll.h>
  104. #include <linux/errqueue.h>
  105. #include <linux/ptp_clock_kernel.h>
  106. #include <trace/events/sock.h>
  107. #ifdef CONFIG_NET_RX_BUSY_POLL
  108. unsigned int sysctl_net_busy_read __read_mostly;
  109. unsigned int sysctl_net_busy_poll __read_mostly;
  110. #endif
  111. static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to);
  112. static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from);
  113. static int sock_mmap(struct file *file, struct vm_area_struct *vma);
  114. static int sock_close(struct inode *inode, struct file *file);
  115. static __poll_t sock_poll(struct file *file,
  116. struct poll_table_struct *wait);
  117. static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  118. #ifdef CONFIG_COMPAT
  119. static long compat_sock_ioctl(struct file *file,
  120. unsigned int cmd, unsigned long arg);
  121. #endif
  122. static int sock_fasync(int fd, struct file *filp, int on);
  123. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  124. struct pipe_inode_info *pipe, size_t len,
  125. unsigned int flags);
  126. static void sock_splice_eof(struct file *file);
  127. #ifdef CONFIG_PROC_FS
  128. static void sock_show_fdinfo(struct seq_file *m, struct file *f)
  129. {
  130. struct socket *sock = f->private_data;
  131. const struct proto_ops *ops = READ_ONCE(sock->ops);
  132. if (ops->show_fdinfo)
  133. ops->show_fdinfo(m, sock);
  134. }
  135. #else
  136. #define sock_show_fdinfo NULL
  137. #endif
  138. /*
  139. * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
  140. * in the operation structures but are done directly via the socketcall() multiplexor.
  141. */
  142. static const struct file_operations socket_file_ops = {
  143. .owner = THIS_MODULE,
  144. .read_iter = sock_read_iter,
  145. .write_iter = sock_write_iter,
  146. .poll = sock_poll,
  147. .unlocked_ioctl = sock_ioctl,
  148. #ifdef CONFIG_COMPAT
  149. .compat_ioctl = compat_sock_ioctl,
  150. #endif
  151. .uring_cmd = io_uring_cmd_sock,
  152. .mmap = sock_mmap,
  153. .release = sock_close,
  154. .fasync = sock_fasync,
  155. .splice_write = splice_to_socket,
  156. .splice_read = sock_splice_read,
  157. .splice_eof = sock_splice_eof,
  158. .show_fdinfo = sock_show_fdinfo,
  159. };
  160. static const char * const pf_family_names[] = {
  161. [PF_UNSPEC] = "PF_UNSPEC",
  162. [PF_UNIX] = "PF_UNIX/PF_LOCAL",
  163. [PF_INET] = "PF_INET",
  164. [PF_AX25] = "PF_AX25",
  165. [PF_IPX] = "PF_IPX",
  166. [PF_APPLETALK] = "PF_APPLETALK",
  167. [PF_NETROM] = "PF_NETROM",
  168. [PF_BRIDGE] = "PF_BRIDGE",
  169. [PF_ATMPVC] = "PF_ATMPVC",
  170. [PF_X25] = "PF_X25",
  171. [PF_INET6] = "PF_INET6",
  172. [PF_ROSE] = "PF_ROSE",
  173. [PF_DECnet] = "PF_DECnet",
  174. [PF_NETBEUI] = "PF_NETBEUI",
  175. [PF_SECURITY] = "PF_SECURITY",
  176. [PF_KEY] = "PF_KEY",
  177. [PF_NETLINK] = "PF_NETLINK/PF_ROUTE",
  178. [PF_PACKET] = "PF_PACKET",
  179. [PF_ASH] = "PF_ASH",
  180. [PF_ECONET] = "PF_ECONET",
  181. [PF_ATMSVC] = "PF_ATMSVC",
  182. [PF_RDS] = "PF_RDS",
  183. [PF_SNA] = "PF_SNA",
  184. [PF_IRDA] = "PF_IRDA",
  185. [PF_PPPOX] = "PF_PPPOX",
  186. [PF_WANPIPE] = "PF_WANPIPE",
  187. [PF_LLC] = "PF_LLC",
  188. [PF_IB] = "PF_IB",
  189. [PF_MPLS] = "PF_MPLS",
  190. [PF_CAN] = "PF_CAN",
  191. [PF_TIPC] = "PF_TIPC",
  192. [PF_BLUETOOTH] = "PF_BLUETOOTH",
  193. [PF_IUCV] = "PF_IUCV",
  194. [PF_RXRPC] = "PF_RXRPC",
  195. [PF_ISDN] = "PF_ISDN",
  196. [PF_PHONET] = "PF_PHONET",
  197. [PF_IEEE802154] = "PF_IEEE802154",
  198. [PF_CAIF] = "PF_CAIF",
  199. [PF_ALG] = "PF_ALG",
  200. [PF_NFC] = "PF_NFC",
  201. [PF_VSOCK] = "PF_VSOCK",
  202. [PF_KCM] = "PF_KCM",
  203. [PF_QIPCRTR] = "PF_QIPCRTR",
  204. [PF_SMC] = "PF_SMC",
  205. [PF_XDP] = "PF_XDP",
  206. [PF_MCTP] = "PF_MCTP",
  207. };
  208. /*
  209. * The protocol list. Each protocol is registered in here.
  210. */
  211. static DEFINE_SPINLOCK(net_family_lock);
  212. static const struct net_proto_family __rcu *net_families[NPROTO] __read_mostly;
  213. /*
  214. * Support routines.
  215. * Move socket addresses back and forth across the kernel/user
  216. * divide and look after the messy bits.
  217. */
  218. /**
  219. * move_addr_to_kernel - copy a socket address into kernel space
  220. * @uaddr: Address in user space
  221. * @kaddr: Address in kernel space
  222. * @ulen: Length in user space
  223. *
  224. * The address is copied into kernel space. If the provided address is
  225. * too long an error code of -EINVAL is returned. If the copy gives
  226. * invalid addresses -EFAULT is returned. On a success 0 is returned.
  227. */
  228. int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr)
  229. {
  230. if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
  231. return -EINVAL;
  232. if (ulen == 0)
  233. return 0;
  234. if (copy_from_user(kaddr, uaddr, ulen))
  235. return -EFAULT;
  236. return audit_sockaddr(ulen, kaddr);
  237. }
  238. /**
  239. * move_addr_to_user - copy an address to user space
  240. * @kaddr: kernel space address
  241. * @klen: length of address in kernel
  242. * @uaddr: user space address
  243. * @ulen: pointer to user length field
  244. *
  245. * The value pointed to by ulen on entry is the buffer length available.
  246. * This is overwritten with the buffer space used. -EINVAL is returned
  247. * if an overlong buffer is specified or a negative buffer size. -EFAULT
  248. * is returned if either the buffer or the length field are not
  249. * accessible.
  250. * After copying the data up to the limit the user specifies, the true
  251. * length of the data is written over the length limit the user
  252. * specified. Zero is returned for a success.
  253. */
  254. static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
  255. void __user *uaddr, int __user *ulen)
  256. {
  257. int err;
  258. int len;
  259. BUG_ON(klen > sizeof(struct sockaddr_storage));
  260. err = get_user(len, ulen);
  261. if (err)
  262. return err;
  263. if (len > klen)
  264. len = klen;
  265. if (len < 0)
  266. return -EINVAL;
  267. if (len) {
  268. if (audit_sockaddr(klen, kaddr))
  269. return -ENOMEM;
  270. if (copy_to_user(uaddr, kaddr, len))
  271. return -EFAULT;
  272. }
  273. /*
  274. * "fromlen shall refer to the value before truncation.."
  275. * 1003.1g
  276. */
  277. return __put_user(klen, ulen);
  278. }
  279. static struct kmem_cache *sock_inode_cachep __ro_after_init;
  280. static struct inode *sock_alloc_inode(struct super_block *sb)
  281. {
  282. struct socket_alloc *ei;
  283. ei = alloc_inode_sb(sb, sock_inode_cachep, GFP_KERNEL);
  284. if (!ei)
  285. return NULL;
  286. init_waitqueue_head(&ei->socket.wq.wait);
  287. ei->socket.wq.fasync_list = NULL;
  288. ei->socket.wq.flags = 0;
  289. ei->socket.state = SS_UNCONNECTED;
  290. ei->socket.flags = 0;
  291. ei->socket.ops = NULL;
  292. ei->socket.sk = NULL;
  293. ei->socket.file = NULL;
  294. return &ei->vfs_inode;
  295. }
  296. static void sock_free_inode(struct inode *inode)
  297. {
  298. struct socket_alloc *ei;
  299. ei = container_of(inode, struct socket_alloc, vfs_inode);
  300. kmem_cache_free(sock_inode_cachep, ei);
  301. }
  302. static void init_once(void *foo)
  303. {
  304. struct socket_alloc *ei = (struct socket_alloc *)foo;
  305. inode_init_once(&ei->vfs_inode);
  306. }
  307. static void init_inodecache(void)
  308. {
  309. sock_inode_cachep = kmem_cache_create("sock_inode_cache",
  310. sizeof(struct socket_alloc),
  311. 0,
  312. (SLAB_HWCACHE_ALIGN |
  313. SLAB_RECLAIM_ACCOUNT |
  314. SLAB_ACCOUNT),
  315. init_once);
  316. BUG_ON(sock_inode_cachep == NULL);
  317. }
  318. static const struct super_operations sockfs_ops = {
  319. .alloc_inode = sock_alloc_inode,
  320. .free_inode = sock_free_inode,
  321. .statfs = simple_statfs,
  322. };
  323. /*
  324. * sockfs_dname() is called from d_path().
  325. */
  326. static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
  327. {
  328. return dynamic_dname(buffer, buflen, "socket:[%lu]",
  329. d_inode(dentry)->i_ino);
  330. }
  331. static const struct dentry_operations sockfs_dentry_operations = {
  332. .d_dname = sockfs_dname,
  333. };
  334. static int sockfs_xattr_get(const struct xattr_handler *handler,
  335. struct dentry *dentry, struct inode *inode,
  336. const char *suffix, void *value, size_t size)
  337. {
  338. if (value) {
  339. if (dentry->d_name.len + 1 > size)
  340. return -ERANGE;
  341. memcpy(value, dentry->d_name.name, dentry->d_name.len + 1);
  342. }
  343. return dentry->d_name.len + 1;
  344. }
  345. #define XATTR_SOCKPROTONAME_SUFFIX "sockprotoname"
  346. #define XATTR_NAME_SOCKPROTONAME (XATTR_SYSTEM_PREFIX XATTR_SOCKPROTONAME_SUFFIX)
  347. #define XATTR_NAME_SOCKPROTONAME_LEN (sizeof(XATTR_NAME_SOCKPROTONAME)-1)
  348. static const struct xattr_handler sockfs_xattr_handler = {
  349. .name = XATTR_NAME_SOCKPROTONAME,
  350. .get = sockfs_xattr_get,
  351. };
  352. static int sockfs_security_xattr_set(const struct xattr_handler *handler,
  353. struct mnt_idmap *idmap,
  354. struct dentry *dentry, struct inode *inode,
  355. const char *suffix, const void *value,
  356. size_t size, int flags)
  357. {
  358. /* Handled by LSM. */
  359. return -EAGAIN;
  360. }
  361. static const struct xattr_handler sockfs_security_xattr_handler = {
  362. .prefix = XATTR_SECURITY_PREFIX,
  363. .set = sockfs_security_xattr_set,
  364. };
  365. static const struct xattr_handler * const sockfs_xattr_handlers[] = {
  366. &sockfs_xattr_handler,
  367. &sockfs_security_xattr_handler,
  368. NULL
  369. };
  370. static int sockfs_init_fs_context(struct fs_context *fc)
  371. {
  372. struct pseudo_fs_context *ctx = init_pseudo(fc, SOCKFS_MAGIC);
  373. if (!ctx)
  374. return -ENOMEM;
  375. ctx->ops = &sockfs_ops;
  376. ctx->dops = &sockfs_dentry_operations;
  377. ctx->xattr = sockfs_xattr_handlers;
  378. return 0;
  379. }
  380. static struct vfsmount *sock_mnt __read_mostly;
  381. static struct file_system_type sock_fs_type = {
  382. .name = "sockfs",
  383. .init_fs_context = sockfs_init_fs_context,
  384. .kill_sb = kill_anon_super,
  385. };
  386. /*
  387. * Obtains the first available file descriptor and sets it up for use.
  388. *
  389. * These functions create file structures and maps them to fd space
  390. * of the current process. On success it returns file descriptor
  391. * and file struct implicitly stored in sock->file.
  392. * Note that another thread may close file descriptor before we return
  393. * from this function. We use the fact that now we do not refer
  394. * to socket after mapping. If one day we will need it, this
  395. * function will increment ref. count on file by 1.
  396. *
  397. * In any case returned fd MAY BE not valid!
  398. * This race condition is unavoidable
  399. * with shared fd spaces, we cannot solve it inside kernel,
  400. * but we take care of internal coherence yet.
  401. */
  402. /**
  403. * sock_alloc_file - Bind a &socket to a &file
  404. * @sock: socket
  405. * @flags: file status flags
  406. * @dname: protocol name
  407. *
  408. * Returns the &file bound with @sock, implicitly storing it
  409. * in sock->file. If dname is %NULL, sets to "".
  410. *
  411. * On failure @sock is released, and an ERR pointer is returned.
  412. *
  413. * This function uses GFP_KERNEL internally.
  414. */
  415. struct file *sock_alloc_file(struct socket *sock, int flags, const char *dname)
  416. {
  417. struct file *file;
  418. if (!dname)
  419. dname = sock->sk ? sock->sk->sk_prot_creator->name : "";
  420. file = alloc_file_pseudo(SOCK_INODE(sock), sock_mnt, dname,
  421. O_RDWR | (flags & O_NONBLOCK),
  422. &socket_file_ops);
  423. if (IS_ERR(file)) {
  424. sock_release(sock);
  425. return file;
  426. }
  427. file->f_mode |= FMODE_NOWAIT;
  428. sock->file = file;
  429. file->private_data = sock;
  430. stream_open(SOCK_INODE(sock), file);
  431. return file;
  432. }
  433. EXPORT_SYMBOL(sock_alloc_file);
  434. static int sock_map_fd(struct socket *sock, int flags)
  435. {
  436. struct file *newfile;
  437. int fd = get_unused_fd_flags(flags);
  438. if (unlikely(fd < 0)) {
  439. sock_release(sock);
  440. return fd;
  441. }
  442. newfile = sock_alloc_file(sock, flags, NULL);
  443. if (!IS_ERR(newfile)) {
  444. fd_install(fd, newfile);
  445. return fd;
  446. }
  447. put_unused_fd(fd);
  448. return PTR_ERR(newfile);
  449. }
  450. /**
  451. * sock_from_file - Return the &socket bounded to @file.
  452. * @file: file
  453. *
  454. * On failure returns %NULL.
  455. */
  456. struct socket *sock_from_file(struct file *file)
  457. {
  458. if (file->f_op == &socket_file_ops)
  459. return file->private_data; /* set in sock_alloc_file */
  460. return NULL;
  461. }
  462. EXPORT_SYMBOL(sock_from_file);
  463. /**
  464. * sockfd_lookup - Go from a file number to its socket slot
  465. * @fd: file handle
  466. * @err: pointer to an error code return
  467. *
  468. * The file handle passed in is locked and the socket it is bound
  469. * to is returned. If an error occurs the err pointer is overwritten
  470. * with a negative errno code and NULL is returned. The function checks
  471. * for both invalid handles and passing a handle which is not a socket.
  472. *
  473. * On a success the socket object pointer is returned.
  474. */
  475. struct socket *sockfd_lookup(int fd, int *err)
  476. {
  477. struct file *file;
  478. struct socket *sock;
  479. file = fget(fd);
  480. if (!file) {
  481. *err = -EBADF;
  482. return NULL;
  483. }
  484. sock = sock_from_file(file);
  485. if (!sock) {
  486. *err = -ENOTSOCK;
  487. fput(file);
  488. }
  489. return sock;
  490. }
  491. EXPORT_SYMBOL(sockfd_lookup);
  492. static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
  493. {
  494. struct fd f = fdget(fd);
  495. struct socket *sock;
  496. *err = -EBADF;
  497. if (fd_file(f)) {
  498. sock = sock_from_file(fd_file(f));
  499. if (likely(sock)) {
  500. *fput_needed = f.word & FDPUT_FPUT;
  501. return sock;
  502. }
  503. *err = -ENOTSOCK;
  504. fdput(f);
  505. }
  506. return NULL;
  507. }
  508. static ssize_t sockfs_listxattr(struct dentry *dentry, char *buffer,
  509. size_t size)
  510. {
  511. ssize_t len;
  512. ssize_t used = 0;
  513. len = security_inode_listsecurity(d_inode(dentry), buffer, size);
  514. if (len < 0)
  515. return len;
  516. used += len;
  517. if (buffer) {
  518. if (size < used)
  519. return -ERANGE;
  520. buffer += len;
  521. }
  522. len = (XATTR_NAME_SOCKPROTONAME_LEN + 1);
  523. used += len;
  524. if (buffer) {
  525. if (size < used)
  526. return -ERANGE;
  527. memcpy(buffer, XATTR_NAME_SOCKPROTONAME, len);
  528. buffer += len;
  529. }
  530. return used;
  531. }
  532. static int sockfs_setattr(struct mnt_idmap *idmap,
  533. struct dentry *dentry, struct iattr *iattr)
  534. {
  535. int err = simple_setattr(&nop_mnt_idmap, dentry, iattr);
  536. if (!err && (iattr->ia_valid & ATTR_UID)) {
  537. struct socket *sock = SOCKET_I(d_inode(dentry));
  538. if (sock->sk)
  539. sock->sk->sk_uid = iattr->ia_uid;
  540. else
  541. err = -ENOENT;
  542. }
  543. return err;
  544. }
  545. static const struct inode_operations sockfs_inode_ops = {
  546. .listxattr = sockfs_listxattr,
  547. .setattr = sockfs_setattr,
  548. };
  549. /**
  550. * sock_alloc - allocate a socket
  551. *
  552. * Allocate a new inode and socket object. The two are bound together
  553. * and initialised. The socket is then returned. If we are out of inodes
  554. * NULL is returned. This functions uses GFP_KERNEL internally.
  555. */
  556. struct socket *sock_alloc(void)
  557. {
  558. struct inode *inode;
  559. struct socket *sock;
  560. inode = new_inode_pseudo(sock_mnt->mnt_sb);
  561. if (!inode)
  562. return NULL;
  563. sock = SOCKET_I(inode);
  564. inode->i_ino = get_next_ino();
  565. inode->i_mode = S_IFSOCK | S_IRWXUGO;
  566. inode->i_uid = current_fsuid();
  567. inode->i_gid = current_fsgid();
  568. inode->i_op = &sockfs_inode_ops;
  569. return sock;
  570. }
  571. EXPORT_SYMBOL(sock_alloc);
  572. static void __sock_release(struct socket *sock, struct inode *inode)
  573. {
  574. const struct proto_ops *ops = READ_ONCE(sock->ops);
  575. if (ops) {
  576. struct module *owner = ops->owner;
  577. if (inode)
  578. inode_lock(inode);
  579. ops->release(sock);
  580. sock->sk = NULL;
  581. if (inode)
  582. inode_unlock(inode);
  583. sock->ops = NULL;
  584. module_put(owner);
  585. }
  586. if (sock->wq.fasync_list)
  587. pr_err("%s: fasync list not empty!\n", __func__);
  588. if (!sock->file) {
  589. iput(SOCK_INODE(sock));
  590. return;
  591. }
  592. sock->file = NULL;
  593. }
  594. /**
  595. * sock_release - close a socket
  596. * @sock: socket to close
  597. *
  598. * The socket is released from the protocol stack if it has a release
  599. * callback, and the inode is then released if the socket is bound to
  600. * an inode not a file.
  601. */
  602. void sock_release(struct socket *sock)
  603. {
  604. __sock_release(sock, NULL);
  605. }
  606. EXPORT_SYMBOL(sock_release);
  607. void __sock_tx_timestamp(__u16 tsflags, __u8 *tx_flags)
  608. {
  609. u8 flags = *tx_flags;
  610. if (tsflags & SOF_TIMESTAMPING_TX_HARDWARE) {
  611. flags |= SKBTX_HW_TSTAMP;
  612. /* PTP hardware clocks can provide a free running cycle counter
  613. * as a time base for virtual clocks. Tell driver to use the
  614. * free running cycle counter for timestamp if socket is bound
  615. * to virtual clock.
  616. */
  617. if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
  618. flags |= SKBTX_HW_TSTAMP_USE_CYCLES;
  619. }
  620. if (tsflags & SOF_TIMESTAMPING_TX_SOFTWARE)
  621. flags |= SKBTX_SW_TSTAMP;
  622. if (tsflags & SOF_TIMESTAMPING_TX_SCHED)
  623. flags |= SKBTX_SCHED_TSTAMP;
  624. *tx_flags = flags;
  625. }
  626. EXPORT_SYMBOL(__sock_tx_timestamp);
  627. INDIRECT_CALLABLE_DECLARE(int inet_sendmsg(struct socket *, struct msghdr *,
  628. size_t));
  629. INDIRECT_CALLABLE_DECLARE(int inet6_sendmsg(struct socket *, struct msghdr *,
  630. size_t));
  631. static noinline void call_trace_sock_send_length(struct sock *sk, int ret,
  632. int flags)
  633. {
  634. trace_sock_send_length(sk, ret, 0);
  635. }
  636. static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
  637. {
  638. int ret = INDIRECT_CALL_INET(READ_ONCE(sock->ops)->sendmsg, inet6_sendmsg,
  639. inet_sendmsg, sock, msg,
  640. msg_data_left(msg));
  641. BUG_ON(ret == -EIOCBQUEUED);
  642. if (trace_sock_send_length_enabled())
  643. call_trace_sock_send_length(sock->sk, ret, 0);
  644. return ret;
  645. }
  646. static int __sock_sendmsg(struct socket *sock, struct msghdr *msg)
  647. {
  648. int err = security_socket_sendmsg(sock, msg,
  649. msg_data_left(msg));
  650. return err ?: sock_sendmsg_nosec(sock, msg);
  651. }
  652. /**
  653. * sock_sendmsg - send a message through @sock
  654. * @sock: socket
  655. * @msg: message to send
  656. *
  657. * Sends @msg through @sock, passing through LSM.
  658. * Returns the number of bytes sent, or an error code.
  659. */
  660. int sock_sendmsg(struct socket *sock, struct msghdr *msg)
  661. {
  662. struct sockaddr_storage *save_addr = (struct sockaddr_storage *)msg->msg_name;
  663. struct sockaddr_storage address;
  664. int save_len = msg->msg_namelen;
  665. int ret;
  666. if (msg->msg_name) {
  667. memcpy(&address, msg->msg_name, msg->msg_namelen);
  668. msg->msg_name = &address;
  669. }
  670. ret = __sock_sendmsg(sock, msg);
  671. msg->msg_name = save_addr;
  672. msg->msg_namelen = save_len;
  673. return ret;
  674. }
  675. EXPORT_SYMBOL(sock_sendmsg);
  676. /**
  677. * kernel_sendmsg - send a message through @sock (kernel-space)
  678. * @sock: socket
  679. * @msg: message header
  680. * @vec: kernel vec
  681. * @num: vec array length
  682. * @size: total message data size
  683. *
  684. * Builds the message data with @vec and sends it through @sock.
  685. * Returns the number of bytes sent, or an error code.
  686. */
  687. int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
  688. struct kvec *vec, size_t num, size_t size)
  689. {
  690. iov_iter_kvec(&msg->msg_iter, ITER_SOURCE, vec, num, size);
  691. return sock_sendmsg(sock, msg);
  692. }
  693. EXPORT_SYMBOL(kernel_sendmsg);
  694. /**
  695. * kernel_sendmsg_locked - send a message through @sock (kernel-space)
  696. * @sk: sock
  697. * @msg: message header
  698. * @vec: output s/g array
  699. * @num: output s/g array length
  700. * @size: total message data size
  701. *
  702. * Builds the message data with @vec and sends it through @sock.
  703. * Returns the number of bytes sent, or an error code.
  704. * Caller must hold @sk.
  705. */
  706. int kernel_sendmsg_locked(struct sock *sk, struct msghdr *msg,
  707. struct kvec *vec, size_t num, size_t size)
  708. {
  709. struct socket *sock = sk->sk_socket;
  710. const struct proto_ops *ops = READ_ONCE(sock->ops);
  711. if (!ops->sendmsg_locked)
  712. return sock_no_sendmsg_locked(sk, msg, size);
  713. iov_iter_kvec(&msg->msg_iter, ITER_SOURCE, vec, num, size);
  714. return ops->sendmsg_locked(sk, msg, msg_data_left(msg));
  715. }
  716. EXPORT_SYMBOL(kernel_sendmsg_locked);
  717. static bool skb_is_err_queue(const struct sk_buff *skb)
  718. {
  719. /* pkt_type of skbs enqueued on the error queue are set to
  720. * PACKET_OUTGOING in skb_set_err_queue(). This is only safe to do
  721. * in recvmsg, since skbs received on a local socket will never
  722. * have a pkt_type of PACKET_OUTGOING.
  723. */
  724. return skb->pkt_type == PACKET_OUTGOING;
  725. }
  726. /* On transmit, software and hardware timestamps are returned independently.
  727. * As the two skb clones share the hardware timestamp, which may be updated
  728. * before the software timestamp is received, a hardware TX timestamp may be
  729. * returned only if there is no software TX timestamp. Ignore false software
  730. * timestamps, which may be made in the __sock_recv_timestamp() call when the
  731. * option SO_TIMESTAMP_OLD(NS) is enabled on the socket, even when the skb has a
  732. * hardware timestamp.
  733. */
  734. static bool skb_is_swtx_tstamp(const struct sk_buff *skb, int false_tstamp)
  735. {
  736. return skb->tstamp && !false_tstamp && skb_is_err_queue(skb);
  737. }
  738. static ktime_t get_timestamp(struct sock *sk, struct sk_buff *skb, int *if_index)
  739. {
  740. bool cycles = READ_ONCE(sk->sk_tsflags) & SOF_TIMESTAMPING_BIND_PHC;
  741. struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
  742. struct net_device *orig_dev;
  743. ktime_t hwtstamp;
  744. rcu_read_lock();
  745. orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
  746. if (orig_dev) {
  747. *if_index = orig_dev->ifindex;
  748. hwtstamp = netdev_get_tstamp(orig_dev, shhwtstamps, cycles);
  749. } else {
  750. hwtstamp = shhwtstamps->hwtstamp;
  751. }
  752. rcu_read_unlock();
  753. return hwtstamp;
  754. }
  755. static void put_ts_pktinfo(struct msghdr *msg, struct sk_buff *skb,
  756. int if_index)
  757. {
  758. struct scm_ts_pktinfo ts_pktinfo;
  759. struct net_device *orig_dev;
  760. if (!skb_mac_header_was_set(skb))
  761. return;
  762. memset(&ts_pktinfo, 0, sizeof(ts_pktinfo));
  763. if (!if_index) {
  764. rcu_read_lock();
  765. orig_dev = dev_get_by_napi_id(skb_napi_id(skb));
  766. if (orig_dev)
  767. if_index = orig_dev->ifindex;
  768. rcu_read_unlock();
  769. }
  770. ts_pktinfo.if_index = if_index;
  771. ts_pktinfo.pkt_length = skb->len - skb_mac_offset(skb);
  772. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_PKTINFO,
  773. sizeof(ts_pktinfo), &ts_pktinfo);
  774. }
  775. /*
  776. * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
  777. */
  778. void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
  779. struct sk_buff *skb)
  780. {
  781. int need_software_tstamp = sock_flag(sk, SOCK_RCVTSTAMP);
  782. int new_tstamp = sock_flag(sk, SOCK_TSTAMP_NEW);
  783. struct scm_timestamping_internal tss;
  784. int empty = 1, false_tstamp = 0;
  785. struct skb_shared_hwtstamps *shhwtstamps =
  786. skb_hwtstamps(skb);
  787. int if_index;
  788. ktime_t hwtstamp;
  789. u32 tsflags;
  790. /* Race occurred between timestamp enabling and packet
  791. receiving. Fill in the current time for now. */
  792. if (need_software_tstamp && skb->tstamp == 0) {
  793. __net_timestamp(skb);
  794. false_tstamp = 1;
  795. }
  796. if (need_software_tstamp) {
  797. if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
  798. if (new_tstamp) {
  799. struct __kernel_sock_timeval tv;
  800. skb_get_new_timestamp(skb, &tv);
  801. put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_NEW,
  802. sizeof(tv), &tv);
  803. } else {
  804. struct __kernel_old_timeval tv;
  805. skb_get_timestamp(skb, &tv);
  806. put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMP_OLD,
  807. sizeof(tv), &tv);
  808. }
  809. } else {
  810. if (new_tstamp) {
  811. struct __kernel_timespec ts;
  812. skb_get_new_timestampns(skb, &ts);
  813. put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_NEW,
  814. sizeof(ts), &ts);
  815. } else {
  816. struct __kernel_old_timespec ts;
  817. skb_get_timestampns(skb, &ts);
  818. put_cmsg(msg, SOL_SOCKET, SO_TIMESTAMPNS_OLD,
  819. sizeof(ts), &ts);
  820. }
  821. }
  822. }
  823. memset(&tss, 0, sizeof(tss));
  824. tsflags = READ_ONCE(sk->sk_tsflags);
  825. if ((tsflags & SOF_TIMESTAMPING_SOFTWARE &&
  826. (tsflags & SOF_TIMESTAMPING_RX_SOFTWARE ||
  827. skb_is_err_queue(skb) ||
  828. !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER))) &&
  829. ktime_to_timespec64_cond(skb->tstamp, tss.ts + 0))
  830. empty = 0;
  831. if (shhwtstamps &&
  832. (tsflags & SOF_TIMESTAMPING_RAW_HARDWARE &&
  833. (tsflags & SOF_TIMESTAMPING_RX_HARDWARE ||
  834. skb_is_err_queue(skb) ||
  835. !(tsflags & SOF_TIMESTAMPING_OPT_RX_FILTER))) &&
  836. !skb_is_swtx_tstamp(skb, false_tstamp)) {
  837. if_index = 0;
  838. if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP_NETDEV)
  839. hwtstamp = get_timestamp(sk, skb, &if_index);
  840. else
  841. hwtstamp = shhwtstamps->hwtstamp;
  842. if (tsflags & SOF_TIMESTAMPING_BIND_PHC)
  843. hwtstamp = ptp_convert_timestamp(&hwtstamp,
  844. READ_ONCE(sk->sk_bind_phc));
  845. if (ktime_to_timespec64_cond(hwtstamp, tss.ts + 2)) {
  846. empty = 0;
  847. if ((tsflags & SOF_TIMESTAMPING_OPT_PKTINFO) &&
  848. !skb_is_err_queue(skb))
  849. put_ts_pktinfo(msg, skb, if_index);
  850. }
  851. }
  852. if (!empty) {
  853. if (sock_flag(sk, SOCK_TSTAMP_NEW))
  854. put_cmsg_scm_timestamping64(msg, &tss);
  855. else
  856. put_cmsg_scm_timestamping(msg, &tss);
  857. if (skb_is_err_queue(skb) && skb->len &&
  858. SKB_EXT_ERR(skb)->opt_stats)
  859. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPING_OPT_STATS,
  860. skb->len, skb->data);
  861. }
  862. }
  863. EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
  864. #ifdef CONFIG_WIRELESS
  865. void __sock_recv_wifi_status(struct msghdr *msg, struct sock *sk,
  866. struct sk_buff *skb)
  867. {
  868. int ack;
  869. if (!sock_flag(sk, SOCK_WIFI_STATUS))
  870. return;
  871. if (!skb->wifi_acked_valid)
  872. return;
  873. ack = skb->wifi_acked;
  874. put_cmsg(msg, SOL_SOCKET, SCM_WIFI_STATUS, sizeof(ack), &ack);
  875. }
  876. EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
  877. #endif
  878. static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
  879. struct sk_buff *skb)
  880. {
  881. if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
  882. put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
  883. sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
  884. }
  885. static void sock_recv_mark(struct msghdr *msg, struct sock *sk,
  886. struct sk_buff *skb)
  887. {
  888. if (sock_flag(sk, SOCK_RCVMARK) && skb) {
  889. /* We must use a bounce buffer for CONFIG_HARDENED_USERCOPY=y */
  890. __u32 mark = skb->mark;
  891. put_cmsg(msg, SOL_SOCKET, SO_MARK, sizeof(__u32), &mark);
  892. }
  893. }
  894. void __sock_recv_cmsgs(struct msghdr *msg, struct sock *sk,
  895. struct sk_buff *skb)
  896. {
  897. sock_recv_timestamp(msg, sk, skb);
  898. sock_recv_drops(msg, sk, skb);
  899. sock_recv_mark(msg, sk, skb);
  900. }
  901. EXPORT_SYMBOL_GPL(__sock_recv_cmsgs);
  902. INDIRECT_CALLABLE_DECLARE(int inet_recvmsg(struct socket *, struct msghdr *,
  903. size_t, int));
  904. INDIRECT_CALLABLE_DECLARE(int inet6_recvmsg(struct socket *, struct msghdr *,
  905. size_t, int));
  906. static noinline void call_trace_sock_recv_length(struct sock *sk, int ret, int flags)
  907. {
  908. trace_sock_recv_length(sk, ret, flags);
  909. }
  910. static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
  911. int flags)
  912. {
  913. int ret = INDIRECT_CALL_INET(READ_ONCE(sock->ops)->recvmsg,
  914. inet6_recvmsg,
  915. inet_recvmsg, sock, msg,
  916. msg_data_left(msg), flags);
  917. if (trace_sock_recv_length_enabled())
  918. call_trace_sock_recv_length(sock->sk, ret, flags);
  919. return ret;
  920. }
  921. /**
  922. * sock_recvmsg - receive a message from @sock
  923. * @sock: socket
  924. * @msg: message to receive
  925. * @flags: message flags
  926. *
  927. * Receives @msg from @sock, passing through LSM. Returns the total number
  928. * of bytes received, or an error.
  929. */
  930. int sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags)
  931. {
  932. int err = security_socket_recvmsg(sock, msg, msg_data_left(msg), flags);
  933. return err ?: sock_recvmsg_nosec(sock, msg, flags);
  934. }
  935. EXPORT_SYMBOL(sock_recvmsg);
  936. /**
  937. * kernel_recvmsg - Receive a message from a socket (kernel space)
  938. * @sock: The socket to receive the message from
  939. * @msg: Received message
  940. * @vec: Input s/g array for message data
  941. * @num: Size of input s/g array
  942. * @size: Number of bytes to read
  943. * @flags: Message flags (MSG_DONTWAIT, etc...)
  944. *
  945. * On return the msg structure contains the scatter/gather array passed in the
  946. * vec argument. The array is modified so that it consists of the unfilled
  947. * portion of the original array.
  948. *
  949. * The returned value is the total number of bytes received, or an error.
  950. */
  951. int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
  952. struct kvec *vec, size_t num, size_t size, int flags)
  953. {
  954. msg->msg_control_is_user = false;
  955. iov_iter_kvec(&msg->msg_iter, ITER_DEST, vec, num, size);
  956. return sock_recvmsg(sock, msg, flags);
  957. }
  958. EXPORT_SYMBOL(kernel_recvmsg);
  959. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  960. struct pipe_inode_info *pipe, size_t len,
  961. unsigned int flags)
  962. {
  963. struct socket *sock = file->private_data;
  964. const struct proto_ops *ops;
  965. ops = READ_ONCE(sock->ops);
  966. if (unlikely(!ops->splice_read))
  967. return copy_splice_read(file, ppos, pipe, len, flags);
  968. return ops->splice_read(sock, ppos, pipe, len, flags);
  969. }
  970. static void sock_splice_eof(struct file *file)
  971. {
  972. struct socket *sock = file->private_data;
  973. const struct proto_ops *ops;
  974. ops = READ_ONCE(sock->ops);
  975. if (ops->splice_eof)
  976. ops->splice_eof(sock);
  977. }
  978. static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
  979. {
  980. struct file *file = iocb->ki_filp;
  981. struct socket *sock = file->private_data;
  982. struct msghdr msg = {.msg_iter = *to,
  983. .msg_iocb = iocb};
  984. ssize_t res;
  985. if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
  986. msg.msg_flags = MSG_DONTWAIT;
  987. if (iocb->ki_pos != 0)
  988. return -ESPIPE;
  989. if (!iov_iter_count(to)) /* Match SYS5 behaviour */
  990. return 0;
  991. res = sock_recvmsg(sock, &msg, msg.msg_flags);
  992. *to = msg.msg_iter;
  993. return res;
  994. }
  995. static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
  996. {
  997. struct file *file = iocb->ki_filp;
  998. struct socket *sock = file->private_data;
  999. struct msghdr msg = {.msg_iter = *from,
  1000. .msg_iocb = iocb};
  1001. ssize_t res;
  1002. if (iocb->ki_pos != 0)
  1003. return -ESPIPE;
  1004. if (file->f_flags & O_NONBLOCK || (iocb->ki_flags & IOCB_NOWAIT))
  1005. msg.msg_flags = MSG_DONTWAIT;
  1006. if (sock->type == SOCK_SEQPACKET)
  1007. msg.msg_flags |= MSG_EOR;
  1008. res = __sock_sendmsg(sock, &msg);
  1009. *from = msg.msg_iter;
  1010. return res;
  1011. }
  1012. /*
  1013. * Atomic setting of ioctl hooks to avoid race
  1014. * with module unload.
  1015. */
  1016. static DEFINE_MUTEX(br_ioctl_mutex);
  1017. static int (*br_ioctl_hook)(struct net *net, struct net_bridge *br,
  1018. unsigned int cmd, struct ifreq *ifr,
  1019. void __user *uarg);
  1020. void brioctl_set(int (*hook)(struct net *net, struct net_bridge *br,
  1021. unsigned int cmd, struct ifreq *ifr,
  1022. void __user *uarg))
  1023. {
  1024. mutex_lock(&br_ioctl_mutex);
  1025. br_ioctl_hook = hook;
  1026. mutex_unlock(&br_ioctl_mutex);
  1027. }
  1028. EXPORT_SYMBOL(brioctl_set);
  1029. int br_ioctl_call(struct net *net, struct net_bridge *br, unsigned int cmd,
  1030. struct ifreq *ifr, void __user *uarg)
  1031. {
  1032. int err = -ENOPKG;
  1033. if (!br_ioctl_hook)
  1034. request_module("bridge");
  1035. mutex_lock(&br_ioctl_mutex);
  1036. if (br_ioctl_hook)
  1037. err = br_ioctl_hook(net, br, cmd, ifr, uarg);
  1038. mutex_unlock(&br_ioctl_mutex);
  1039. return err;
  1040. }
  1041. static DEFINE_MUTEX(vlan_ioctl_mutex);
  1042. static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
  1043. void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
  1044. {
  1045. mutex_lock(&vlan_ioctl_mutex);
  1046. vlan_ioctl_hook = hook;
  1047. mutex_unlock(&vlan_ioctl_mutex);
  1048. }
  1049. EXPORT_SYMBOL(vlan_ioctl_set);
  1050. static long sock_do_ioctl(struct net *net, struct socket *sock,
  1051. unsigned int cmd, unsigned long arg)
  1052. {
  1053. const struct proto_ops *ops = READ_ONCE(sock->ops);
  1054. struct ifreq ifr;
  1055. bool need_copyout;
  1056. int err;
  1057. void __user *argp = (void __user *)arg;
  1058. void __user *data;
  1059. err = ops->ioctl(sock, cmd, arg);
  1060. /*
  1061. * If this ioctl is unknown try to hand it down
  1062. * to the NIC driver.
  1063. */
  1064. if (err != -ENOIOCTLCMD)
  1065. return err;
  1066. if (!is_socket_ioctl_cmd(cmd))
  1067. return -ENOTTY;
  1068. if (get_user_ifreq(&ifr, &data, argp))
  1069. return -EFAULT;
  1070. err = dev_ioctl(net, cmd, &ifr, data, &need_copyout);
  1071. if (!err && need_copyout)
  1072. if (put_user_ifreq(&ifr, argp))
  1073. return -EFAULT;
  1074. return err;
  1075. }
  1076. /*
  1077. * With an ioctl, arg may well be a user mode pointer, but we don't know
  1078. * what to do with it - that's up to the protocol still.
  1079. */
  1080. static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  1081. {
  1082. const struct proto_ops *ops;
  1083. struct socket *sock;
  1084. struct sock *sk;
  1085. void __user *argp = (void __user *)arg;
  1086. int pid, err;
  1087. struct net *net;
  1088. sock = file->private_data;
  1089. ops = READ_ONCE(sock->ops);
  1090. sk = sock->sk;
  1091. net = sock_net(sk);
  1092. if (unlikely(cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))) {
  1093. struct ifreq ifr;
  1094. void __user *data;
  1095. bool need_copyout;
  1096. if (get_user_ifreq(&ifr, &data, argp))
  1097. return -EFAULT;
  1098. err = dev_ioctl(net, cmd, &ifr, data, &need_copyout);
  1099. if (!err && need_copyout)
  1100. if (put_user_ifreq(&ifr, argp))
  1101. return -EFAULT;
  1102. } else
  1103. #ifdef CONFIG_WEXT_CORE
  1104. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
  1105. err = wext_handle_ioctl(net, cmd, argp);
  1106. } else
  1107. #endif
  1108. switch (cmd) {
  1109. case FIOSETOWN:
  1110. case SIOCSPGRP:
  1111. err = -EFAULT;
  1112. if (get_user(pid, (int __user *)argp))
  1113. break;
  1114. err = f_setown(sock->file, pid, 1);
  1115. break;
  1116. case FIOGETOWN:
  1117. case SIOCGPGRP:
  1118. err = put_user(f_getown(sock->file),
  1119. (int __user *)argp);
  1120. break;
  1121. case SIOCGIFBR:
  1122. case SIOCSIFBR:
  1123. case SIOCBRADDBR:
  1124. case SIOCBRDELBR:
  1125. err = br_ioctl_call(net, NULL, cmd, NULL, argp);
  1126. break;
  1127. case SIOCGIFVLAN:
  1128. case SIOCSIFVLAN:
  1129. err = -ENOPKG;
  1130. if (!vlan_ioctl_hook)
  1131. request_module("8021q");
  1132. mutex_lock(&vlan_ioctl_mutex);
  1133. if (vlan_ioctl_hook)
  1134. err = vlan_ioctl_hook(net, argp);
  1135. mutex_unlock(&vlan_ioctl_mutex);
  1136. break;
  1137. case SIOCGSKNS:
  1138. err = -EPERM;
  1139. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1140. break;
  1141. err = open_related_ns(&net->ns, get_net_ns);
  1142. break;
  1143. case SIOCGSTAMP_OLD:
  1144. case SIOCGSTAMPNS_OLD:
  1145. if (!ops->gettstamp) {
  1146. err = -ENOIOCTLCMD;
  1147. break;
  1148. }
  1149. err = ops->gettstamp(sock, argp,
  1150. cmd == SIOCGSTAMP_OLD,
  1151. !IS_ENABLED(CONFIG_64BIT));
  1152. break;
  1153. case SIOCGSTAMP_NEW:
  1154. case SIOCGSTAMPNS_NEW:
  1155. if (!ops->gettstamp) {
  1156. err = -ENOIOCTLCMD;
  1157. break;
  1158. }
  1159. err = ops->gettstamp(sock, argp,
  1160. cmd == SIOCGSTAMP_NEW,
  1161. false);
  1162. break;
  1163. case SIOCGIFCONF:
  1164. err = dev_ifconf(net, argp);
  1165. break;
  1166. default:
  1167. err = sock_do_ioctl(net, sock, cmd, arg);
  1168. break;
  1169. }
  1170. return err;
  1171. }
  1172. /**
  1173. * sock_create_lite - creates a socket
  1174. * @family: protocol family (AF_INET, ...)
  1175. * @type: communication type (SOCK_STREAM, ...)
  1176. * @protocol: protocol (0, ...)
  1177. * @res: new socket
  1178. *
  1179. * Creates a new socket and assigns it to @res, passing through LSM.
  1180. * The new socket initialization is not complete, see kernel_accept().
  1181. * Returns 0 or an error. On failure @res is set to %NULL.
  1182. * This function internally uses GFP_KERNEL.
  1183. */
  1184. int sock_create_lite(int family, int type, int protocol, struct socket **res)
  1185. {
  1186. int err;
  1187. struct socket *sock = NULL;
  1188. err = security_socket_create(family, type, protocol, 1);
  1189. if (err)
  1190. goto out;
  1191. sock = sock_alloc();
  1192. if (!sock) {
  1193. err = -ENOMEM;
  1194. goto out;
  1195. }
  1196. sock->type = type;
  1197. err = security_socket_post_create(sock, family, type, protocol, 1);
  1198. if (err)
  1199. goto out_release;
  1200. out:
  1201. *res = sock;
  1202. return err;
  1203. out_release:
  1204. sock_release(sock);
  1205. sock = NULL;
  1206. goto out;
  1207. }
  1208. EXPORT_SYMBOL(sock_create_lite);
  1209. /* No kernel lock held - perfect */
  1210. static __poll_t sock_poll(struct file *file, poll_table *wait)
  1211. {
  1212. struct socket *sock = file->private_data;
  1213. const struct proto_ops *ops = READ_ONCE(sock->ops);
  1214. __poll_t events = poll_requested_events(wait), flag = 0;
  1215. if (!ops->poll)
  1216. return 0;
  1217. if (sk_can_busy_loop(sock->sk)) {
  1218. /* poll once if requested by the syscall */
  1219. if (events & POLL_BUSY_LOOP)
  1220. sk_busy_loop(sock->sk, 1);
  1221. /* if this socket can poll_ll, tell the system call */
  1222. flag = POLL_BUSY_LOOP;
  1223. }
  1224. return ops->poll(file, sock, wait) | flag;
  1225. }
  1226. static int sock_mmap(struct file *file, struct vm_area_struct *vma)
  1227. {
  1228. struct socket *sock = file->private_data;
  1229. return READ_ONCE(sock->ops)->mmap(file, sock, vma);
  1230. }
  1231. static int sock_close(struct inode *inode, struct file *filp)
  1232. {
  1233. __sock_release(SOCKET_I(inode), inode);
  1234. return 0;
  1235. }
  1236. /*
  1237. * Update the socket async list
  1238. *
  1239. * Fasync_list locking strategy.
  1240. *
  1241. * 1. fasync_list is modified only under process context socket lock
  1242. * i.e. under semaphore.
  1243. * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
  1244. * or under socket lock
  1245. */
  1246. static int sock_fasync(int fd, struct file *filp, int on)
  1247. {
  1248. struct socket *sock = filp->private_data;
  1249. struct sock *sk = sock->sk;
  1250. struct socket_wq *wq = &sock->wq;
  1251. if (sk == NULL)
  1252. return -EINVAL;
  1253. lock_sock(sk);
  1254. fasync_helper(fd, filp, on, &wq->fasync_list);
  1255. if (!wq->fasync_list)
  1256. sock_reset_flag(sk, SOCK_FASYNC);
  1257. else
  1258. sock_set_flag(sk, SOCK_FASYNC);
  1259. release_sock(sk);
  1260. return 0;
  1261. }
  1262. /* This function may be called only under rcu_lock */
  1263. int sock_wake_async(struct socket_wq *wq, int how, int band)
  1264. {
  1265. if (!wq || !wq->fasync_list)
  1266. return -1;
  1267. switch (how) {
  1268. case SOCK_WAKE_WAITD:
  1269. if (test_bit(SOCKWQ_ASYNC_WAITDATA, &wq->flags))
  1270. break;
  1271. goto call_kill;
  1272. case SOCK_WAKE_SPACE:
  1273. if (!test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags))
  1274. break;
  1275. fallthrough;
  1276. case SOCK_WAKE_IO:
  1277. call_kill:
  1278. kill_fasync(&wq->fasync_list, SIGIO, band);
  1279. break;
  1280. case SOCK_WAKE_URG:
  1281. kill_fasync(&wq->fasync_list, SIGURG, band);
  1282. }
  1283. return 0;
  1284. }
  1285. EXPORT_SYMBOL(sock_wake_async);
  1286. /**
  1287. * __sock_create - creates a socket
  1288. * @net: net namespace
  1289. * @family: protocol family (AF_INET, ...)
  1290. * @type: communication type (SOCK_STREAM, ...)
  1291. * @protocol: protocol (0, ...)
  1292. * @res: new socket
  1293. * @kern: boolean for kernel space sockets
  1294. *
  1295. * Creates a new socket and assigns it to @res, passing through LSM.
  1296. * Returns 0 or an error. On failure @res is set to %NULL. @kern must
  1297. * be set to true if the socket resides in kernel space.
  1298. * This function internally uses GFP_KERNEL.
  1299. */
  1300. int __sock_create(struct net *net, int family, int type, int protocol,
  1301. struct socket **res, int kern)
  1302. {
  1303. int err;
  1304. struct socket *sock;
  1305. const struct net_proto_family *pf;
  1306. /*
  1307. * Check protocol is in range
  1308. */
  1309. if (family < 0 || family >= NPROTO)
  1310. return -EAFNOSUPPORT;
  1311. if (type < 0 || type >= SOCK_MAX)
  1312. return -EINVAL;
  1313. /* Compatibility.
  1314. This uglymoron is moved from INET layer to here to avoid
  1315. deadlock in module load.
  1316. */
  1317. if (family == PF_INET && type == SOCK_PACKET) {
  1318. pr_info_once("%s uses obsolete (PF_INET,SOCK_PACKET)\n",
  1319. current->comm);
  1320. family = PF_PACKET;
  1321. }
  1322. err = security_socket_create(family, type, protocol, kern);
  1323. if (err)
  1324. return err;
  1325. /*
  1326. * Allocate the socket and allow the family to set things up. if
  1327. * the protocol is 0, the family is instructed to select an appropriate
  1328. * default.
  1329. */
  1330. sock = sock_alloc();
  1331. if (!sock) {
  1332. net_warn_ratelimited("socket: no more sockets\n");
  1333. return -ENFILE; /* Not exactly a match, but its the
  1334. closest posix thing */
  1335. }
  1336. sock->type = type;
  1337. #ifdef CONFIG_MODULES
  1338. /* Attempt to load a protocol module if the find failed.
  1339. *
  1340. * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
  1341. * requested real, full-featured networking support upon configuration.
  1342. * Otherwise module support will break!
  1343. */
  1344. if (rcu_access_pointer(net_families[family]) == NULL)
  1345. request_module("net-pf-%d", family);
  1346. #endif
  1347. rcu_read_lock();
  1348. pf = rcu_dereference(net_families[family]);
  1349. err = -EAFNOSUPPORT;
  1350. if (!pf)
  1351. goto out_release;
  1352. /*
  1353. * We will call the ->create function, that possibly is in a loadable
  1354. * module, so we have to bump that loadable module refcnt first.
  1355. */
  1356. if (!try_module_get(pf->owner))
  1357. goto out_release;
  1358. /* Now protected by module ref count */
  1359. rcu_read_unlock();
  1360. err = pf->create(net, sock, protocol, kern);
  1361. if (err < 0) {
  1362. /* ->create should release the allocated sock->sk object on error
  1363. * but it may leave the dangling pointer
  1364. */
  1365. sock->sk = NULL;
  1366. goto out_module_put;
  1367. }
  1368. /*
  1369. * Now to bump the refcnt of the [loadable] module that owns this
  1370. * socket at sock_release time we decrement its refcnt.
  1371. */
  1372. if (!try_module_get(sock->ops->owner))
  1373. goto out_module_busy;
  1374. /*
  1375. * Now that we're done with the ->create function, the [loadable]
  1376. * module can have its refcnt decremented
  1377. */
  1378. module_put(pf->owner);
  1379. err = security_socket_post_create(sock, family, type, protocol, kern);
  1380. if (err)
  1381. goto out_sock_release;
  1382. *res = sock;
  1383. return 0;
  1384. out_module_busy:
  1385. err = -EAFNOSUPPORT;
  1386. out_module_put:
  1387. sock->ops = NULL;
  1388. module_put(pf->owner);
  1389. out_sock_release:
  1390. sock_release(sock);
  1391. return err;
  1392. out_release:
  1393. rcu_read_unlock();
  1394. goto out_sock_release;
  1395. }
  1396. EXPORT_SYMBOL(__sock_create);
  1397. /**
  1398. * sock_create - creates a socket
  1399. * @family: protocol family (AF_INET, ...)
  1400. * @type: communication type (SOCK_STREAM, ...)
  1401. * @protocol: protocol (0, ...)
  1402. * @res: new socket
  1403. *
  1404. * A wrapper around __sock_create().
  1405. * Returns 0 or an error. This function internally uses GFP_KERNEL.
  1406. */
  1407. int sock_create(int family, int type, int protocol, struct socket **res)
  1408. {
  1409. return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
  1410. }
  1411. EXPORT_SYMBOL(sock_create);
  1412. /**
  1413. * sock_create_kern - creates a socket (kernel space)
  1414. * @net: net namespace
  1415. * @family: protocol family (AF_INET, ...)
  1416. * @type: communication type (SOCK_STREAM, ...)
  1417. * @protocol: protocol (0, ...)
  1418. * @res: new socket
  1419. *
  1420. * A wrapper around __sock_create().
  1421. * Returns 0 or an error. This function internally uses GFP_KERNEL.
  1422. */
  1423. int sock_create_kern(struct net *net, int family, int type, int protocol, struct socket **res)
  1424. {
  1425. return __sock_create(net, family, type, protocol, res, 1);
  1426. }
  1427. EXPORT_SYMBOL(sock_create_kern);
  1428. static struct socket *__sys_socket_create(int family, int type, int protocol)
  1429. {
  1430. struct socket *sock;
  1431. int retval;
  1432. /* Check the SOCK_* constants for consistency. */
  1433. BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
  1434. BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
  1435. BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
  1436. BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
  1437. if ((type & ~SOCK_TYPE_MASK) & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1438. return ERR_PTR(-EINVAL);
  1439. type &= SOCK_TYPE_MASK;
  1440. retval = sock_create(family, type, protocol, &sock);
  1441. if (retval < 0)
  1442. return ERR_PTR(retval);
  1443. return sock;
  1444. }
  1445. struct file *__sys_socket_file(int family, int type, int protocol)
  1446. {
  1447. struct socket *sock;
  1448. int flags;
  1449. sock = __sys_socket_create(family, type, protocol);
  1450. if (IS_ERR(sock))
  1451. return ERR_CAST(sock);
  1452. flags = type & ~SOCK_TYPE_MASK;
  1453. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1454. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1455. return sock_alloc_file(sock, flags, NULL);
  1456. }
  1457. /* A hook for bpf progs to attach to and update socket protocol.
  1458. *
  1459. * A static noinline declaration here could cause the compiler to
  1460. * optimize away the function. A global noinline declaration will
  1461. * keep the definition, but may optimize away the callsite.
  1462. * Therefore, __weak is needed to ensure that the call is still
  1463. * emitted, by telling the compiler that we don't know what the
  1464. * function might eventually be.
  1465. */
  1466. __bpf_hook_start();
  1467. __weak noinline int update_socket_protocol(int family, int type, int protocol)
  1468. {
  1469. return protocol;
  1470. }
  1471. __bpf_hook_end();
  1472. int __sys_socket(int family, int type, int protocol)
  1473. {
  1474. struct socket *sock;
  1475. int flags;
  1476. sock = __sys_socket_create(family, type,
  1477. update_socket_protocol(family, type, protocol));
  1478. if (IS_ERR(sock))
  1479. return PTR_ERR(sock);
  1480. flags = type & ~SOCK_TYPE_MASK;
  1481. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1482. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1483. return sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
  1484. }
  1485. SYSCALL_DEFINE3(socket, int, family, int, type, int, protocol)
  1486. {
  1487. return __sys_socket(family, type, protocol);
  1488. }
  1489. /*
  1490. * Create a pair of connected sockets.
  1491. */
  1492. int __sys_socketpair(int family, int type, int protocol, int __user *usockvec)
  1493. {
  1494. struct socket *sock1, *sock2;
  1495. int fd1, fd2, err;
  1496. struct file *newfile1, *newfile2;
  1497. int flags;
  1498. flags = type & ~SOCK_TYPE_MASK;
  1499. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1500. return -EINVAL;
  1501. type &= SOCK_TYPE_MASK;
  1502. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1503. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1504. /*
  1505. * reserve descriptors and make sure we won't fail
  1506. * to return them to userland.
  1507. */
  1508. fd1 = get_unused_fd_flags(flags);
  1509. if (unlikely(fd1 < 0))
  1510. return fd1;
  1511. fd2 = get_unused_fd_flags(flags);
  1512. if (unlikely(fd2 < 0)) {
  1513. put_unused_fd(fd1);
  1514. return fd2;
  1515. }
  1516. err = put_user(fd1, &usockvec[0]);
  1517. if (err)
  1518. goto out;
  1519. err = put_user(fd2, &usockvec[1]);
  1520. if (err)
  1521. goto out;
  1522. /*
  1523. * Obtain the first socket and check if the underlying protocol
  1524. * supports the socketpair call.
  1525. */
  1526. err = sock_create(family, type, protocol, &sock1);
  1527. if (unlikely(err < 0))
  1528. goto out;
  1529. err = sock_create(family, type, protocol, &sock2);
  1530. if (unlikely(err < 0)) {
  1531. sock_release(sock1);
  1532. goto out;
  1533. }
  1534. err = security_socket_socketpair(sock1, sock2);
  1535. if (unlikely(err)) {
  1536. sock_release(sock2);
  1537. sock_release(sock1);
  1538. goto out;
  1539. }
  1540. err = READ_ONCE(sock1->ops)->socketpair(sock1, sock2);
  1541. if (unlikely(err < 0)) {
  1542. sock_release(sock2);
  1543. sock_release(sock1);
  1544. goto out;
  1545. }
  1546. newfile1 = sock_alloc_file(sock1, flags, NULL);
  1547. if (IS_ERR(newfile1)) {
  1548. err = PTR_ERR(newfile1);
  1549. sock_release(sock2);
  1550. goto out;
  1551. }
  1552. newfile2 = sock_alloc_file(sock2, flags, NULL);
  1553. if (IS_ERR(newfile2)) {
  1554. err = PTR_ERR(newfile2);
  1555. fput(newfile1);
  1556. goto out;
  1557. }
  1558. audit_fd_pair(fd1, fd2);
  1559. fd_install(fd1, newfile1);
  1560. fd_install(fd2, newfile2);
  1561. return 0;
  1562. out:
  1563. put_unused_fd(fd2);
  1564. put_unused_fd(fd1);
  1565. return err;
  1566. }
  1567. SYSCALL_DEFINE4(socketpair, int, family, int, type, int, protocol,
  1568. int __user *, usockvec)
  1569. {
  1570. return __sys_socketpair(family, type, protocol, usockvec);
  1571. }
  1572. int __sys_bind_socket(struct socket *sock, struct sockaddr_storage *address,
  1573. int addrlen)
  1574. {
  1575. int err;
  1576. err = security_socket_bind(sock, (struct sockaddr *)address,
  1577. addrlen);
  1578. if (!err)
  1579. err = READ_ONCE(sock->ops)->bind(sock,
  1580. (struct sockaddr *)address,
  1581. addrlen);
  1582. return err;
  1583. }
  1584. /*
  1585. * Bind a name to a socket. Nothing much to do here since it's
  1586. * the protocol's responsibility to handle the local address.
  1587. *
  1588. * We move the socket address to kernel space before we call
  1589. * the protocol layer (having also checked the address is ok).
  1590. */
  1591. int __sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
  1592. {
  1593. struct socket *sock;
  1594. struct sockaddr_storage address;
  1595. int err, fput_needed;
  1596. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1597. if (sock) {
  1598. err = move_addr_to_kernel(umyaddr, addrlen, &address);
  1599. if (!err)
  1600. err = __sys_bind_socket(sock, &address, addrlen);
  1601. fput_light(sock->file, fput_needed);
  1602. }
  1603. return err;
  1604. }
  1605. SYSCALL_DEFINE3(bind, int, fd, struct sockaddr __user *, umyaddr, int, addrlen)
  1606. {
  1607. return __sys_bind(fd, umyaddr, addrlen);
  1608. }
  1609. /*
  1610. * Perform a listen. Basically, we allow the protocol to do anything
  1611. * necessary for a listen, and if that works, we mark the socket as
  1612. * ready for listening.
  1613. */
  1614. int __sys_listen_socket(struct socket *sock, int backlog)
  1615. {
  1616. int somaxconn, err;
  1617. somaxconn = READ_ONCE(sock_net(sock->sk)->core.sysctl_somaxconn);
  1618. if ((unsigned int)backlog > somaxconn)
  1619. backlog = somaxconn;
  1620. err = security_socket_listen(sock, backlog);
  1621. if (!err)
  1622. err = READ_ONCE(sock->ops)->listen(sock, backlog);
  1623. return err;
  1624. }
  1625. int __sys_listen(int fd, int backlog)
  1626. {
  1627. struct socket *sock;
  1628. int err, fput_needed;
  1629. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1630. if (sock) {
  1631. err = __sys_listen_socket(sock, backlog);
  1632. fput_light(sock->file, fput_needed);
  1633. }
  1634. return err;
  1635. }
  1636. SYSCALL_DEFINE2(listen, int, fd, int, backlog)
  1637. {
  1638. return __sys_listen(fd, backlog);
  1639. }
  1640. struct file *do_accept(struct file *file, struct proto_accept_arg *arg,
  1641. struct sockaddr __user *upeer_sockaddr,
  1642. int __user *upeer_addrlen, int flags)
  1643. {
  1644. struct socket *sock, *newsock;
  1645. struct file *newfile;
  1646. int err, len;
  1647. struct sockaddr_storage address;
  1648. const struct proto_ops *ops;
  1649. sock = sock_from_file(file);
  1650. if (!sock)
  1651. return ERR_PTR(-ENOTSOCK);
  1652. newsock = sock_alloc();
  1653. if (!newsock)
  1654. return ERR_PTR(-ENFILE);
  1655. ops = READ_ONCE(sock->ops);
  1656. newsock->type = sock->type;
  1657. newsock->ops = ops;
  1658. /*
  1659. * We don't need try_module_get here, as the listening socket (sock)
  1660. * has the protocol module (sock->ops->owner) held.
  1661. */
  1662. __module_get(ops->owner);
  1663. newfile = sock_alloc_file(newsock, flags, sock->sk->sk_prot_creator->name);
  1664. if (IS_ERR(newfile))
  1665. return newfile;
  1666. err = security_socket_accept(sock, newsock);
  1667. if (err)
  1668. goto out_fd;
  1669. arg->flags |= sock->file->f_flags;
  1670. err = ops->accept(sock, newsock, arg);
  1671. if (err < 0)
  1672. goto out_fd;
  1673. if (upeer_sockaddr) {
  1674. len = ops->getname(newsock, (struct sockaddr *)&address, 2);
  1675. if (len < 0) {
  1676. err = -ECONNABORTED;
  1677. goto out_fd;
  1678. }
  1679. err = move_addr_to_user(&address,
  1680. len, upeer_sockaddr, upeer_addrlen);
  1681. if (err < 0)
  1682. goto out_fd;
  1683. }
  1684. /* File flags are not inherited via accept() unlike another OSes. */
  1685. return newfile;
  1686. out_fd:
  1687. fput(newfile);
  1688. return ERR_PTR(err);
  1689. }
  1690. static int __sys_accept4_file(struct file *file, struct sockaddr __user *upeer_sockaddr,
  1691. int __user *upeer_addrlen, int flags)
  1692. {
  1693. struct proto_accept_arg arg = { };
  1694. struct file *newfile;
  1695. int newfd;
  1696. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1697. return -EINVAL;
  1698. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1699. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1700. newfd = get_unused_fd_flags(flags);
  1701. if (unlikely(newfd < 0))
  1702. return newfd;
  1703. newfile = do_accept(file, &arg, upeer_sockaddr, upeer_addrlen,
  1704. flags);
  1705. if (IS_ERR(newfile)) {
  1706. put_unused_fd(newfd);
  1707. return PTR_ERR(newfile);
  1708. }
  1709. fd_install(newfd, newfile);
  1710. return newfd;
  1711. }
  1712. /*
  1713. * For accept, we attempt to create a new socket, set up the link
  1714. * with the client, wake up the client, then return the new
  1715. * connected fd. We collect the address of the connector in kernel
  1716. * space and move it to user at the very end. This is unclean because
  1717. * we open the socket then return an error.
  1718. *
  1719. * 1003.1g adds the ability to recvmsg() to query connection pending
  1720. * status to recvmsg. We need to add that support in a way thats
  1721. * clean when we restructure accept also.
  1722. */
  1723. int __sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
  1724. int __user *upeer_addrlen, int flags)
  1725. {
  1726. int ret = -EBADF;
  1727. struct fd f;
  1728. f = fdget(fd);
  1729. if (fd_file(f)) {
  1730. ret = __sys_accept4_file(fd_file(f), upeer_sockaddr,
  1731. upeer_addrlen, flags);
  1732. fdput(f);
  1733. }
  1734. return ret;
  1735. }
  1736. SYSCALL_DEFINE4(accept4, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1737. int __user *, upeer_addrlen, int, flags)
  1738. {
  1739. return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, flags);
  1740. }
  1741. SYSCALL_DEFINE3(accept, int, fd, struct sockaddr __user *, upeer_sockaddr,
  1742. int __user *, upeer_addrlen)
  1743. {
  1744. return __sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
  1745. }
  1746. /*
  1747. * Attempt to connect to a socket with the server address. The address
  1748. * is in user space so we verify it is OK and move it to kernel space.
  1749. *
  1750. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1751. * break bindings
  1752. *
  1753. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1754. * other SEQPACKET protocols that take time to connect() as it doesn't
  1755. * include the -EINPROGRESS status for such sockets.
  1756. */
  1757. int __sys_connect_file(struct file *file, struct sockaddr_storage *address,
  1758. int addrlen, int file_flags)
  1759. {
  1760. struct socket *sock;
  1761. int err;
  1762. sock = sock_from_file(file);
  1763. if (!sock) {
  1764. err = -ENOTSOCK;
  1765. goto out;
  1766. }
  1767. err =
  1768. security_socket_connect(sock, (struct sockaddr *)address, addrlen);
  1769. if (err)
  1770. goto out;
  1771. err = READ_ONCE(sock->ops)->connect(sock, (struct sockaddr *)address,
  1772. addrlen, sock->file->f_flags | file_flags);
  1773. out:
  1774. return err;
  1775. }
  1776. int __sys_connect(int fd, struct sockaddr __user *uservaddr, int addrlen)
  1777. {
  1778. int ret = -EBADF;
  1779. struct fd f;
  1780. f = fdget(fd);
  1781. if (fd_file(f)) {
  1782. struct sockaddr_storage address;
  1783. ret = move_addr_to_kernel(uservaddr, addrlen, &address);
  1784. if (!ret)
  1785. ret = __sys_connect_file(fd_file(f), &address, addrlen, 0);
  1786. fdput(f);
  1787. }
  1788. return ret;
  1789. }
  1790. SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
  1791. int, addrlen)
  1792. {
  1793. return __sys_connect(fd, uservaddr, addrlen);
  1794. }
  1795. /*
  1796. * Get the local address ('name') of a socket object. Move the obtained
  1797. * name to user space.
  1798. */
  1799. int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
  1800. int __user *usockaddr_len)
  1801. {
  1802. struct socket *sock;
  1803. struct sockaddr_storage address;
  1804. int err, fput_needed;
  1805. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1806. if (!sock)
  1807. goto out;
  1808. err = security_socket_getsockname(sock);
  1809. if (err)
  1810. goto out_put;
  1811. err = READ_ONCE(sock->ops)->getname(sock, (struct sockaddr *)&address, 0);
  1812. if (err < 0)
  1813. goto out_put;
  1814. /* "err" is actually length in this case */
  1815. err = move_addr_to_user(&address, err, usockaddr, usockaddr_len);
  1816. out_put:
  1817. fput_light(sock->file, fput_needed);
  1818. out:
  1819. return err;
  1820. }
  1821. SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
  1822. int __user *, usockaddr_len)
  1823. {
  1824. return __sys_getsockname(fd, usockaddr, usockaddr_len);
  1825. }
  1826. /*
  1827. * Get the remote address ('name') of a socket object. Move the obtained
  1828. * name to user space.
  1829. */
  1830. int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
  1831. int __user *usockaddr_len)
  1832. {
  1833. struct socket *sock;
  1834. struct sockaddr_storage address;
  1835. int err, fput_needed;
  1836. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1837. if (sock != NULL) {
  1838. const struct proto_ops *ops = READ_ONCE(sock->ops);
  1839. err = security_socket_getpeername(sock);
  1840. if (err) {
  1841. fput_light(sock->file, fput_needed);
  1842. return err;
  1843. }
  1844. err = ops->getname(sock, (struct sockaddr *)&address, 1);
  1845. if (err >= 0)
  1846. /* "err" is actually length in this case */
  1847. err = move_addr_to_user(&address, err, usockaddr,
  1848. usockaddr_len);
  1849. fput_light(sock->file, fput_needed);
  1850. }
  1851. return err;
  1852. }
  1853. SYSCALL_DEFINE3(getpeername, int, fd, struct sockaddr __user *, usockaddr,
  1854. int __user *, usockaddr_len)
  1855. {
  1856. return __sys_getpeername(fd, usockaddr, usockaddr_len);
  1857. }
  1858. /*
  1859. * Send a datagram to a given address. We move the address into kernel
  1860. * space and check the user space data area is readable before invoking
  1861. * the protocol.
  1862. */
  1863. int __sys_sendto(int fd, void __user *buff, size_t len, unsigned int flags,
  1864. struct sockaddr __user *addr, int addr_len)
  1865. {
  1866. struct socket *sock;
  1867. struct sockaddr_storage address;
  1868. int err;
  1869. struct msghdr msg;
  1870. int fput_needed;
  1871. err = import_ubuf(ITER_SOURCE, buff, len, &msg.msg_iter);
  1872. if (unlikely(err))
  1873. return err;
  1874. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1875. if (!sock)
  1876. goto out;
  1877. msg.msg_name = NULL;
  1878. msg.msg_control = NULL;
  1879. msg.msg_controllen = 0;
  1880. msg.msg_namelen = 0;
  1881. msg.msg_ubuf = NULL;
  1882. if (addr) {
  1883. err = move_addr_to_kernel(addr, addr_len, &address);
  1884. if (err < 0)
  1885. goto out_put;
  1886. msg.msg_name = (struct sockaddr *)&address;
  1887. msg.msg_namelen = addr_len;
  1888. }
  1889. flags &= ~MSG_INTERNAL_SENDMSG_FLAGS;
  1890. if (sock->file->f_flags & O_NONBLOCK)
  1891. flags |= MSG_DONTWAIT;
  1892. msg.msg_flags = flags;
  1893. err = __sock_sendmsg(sock, &msg);
  1894. out_put:
  1895. fput_light(sock->file, fput_needed);
  1896. out:
  1897. return err;
  1898. }
  1899. SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
  1900. unsigned int, flags, struct sockaddr __user *, addr,
  1901. int, addr_len)
  1902. {
  1903. return __sys_sendto(fd, buff, len, flags, addr, addr_len);
  1904. }
  1905. /*
  1906. * Send a datagram down a socket.
  1907. */
  1908. SYSCALL_DEFINE4(send, int, fd, void __user *, buff, size_t, len,
  1909. unsigned int, flags)
  1910. {
  1911. return __sys_sendto(fd, buff, len, flags, NULL, 0);
  1912. }
  1913. /*
  1914. * Receive a frame from the socket and optionally record the address of the
  1915. * sender. We verify the buffers are writable and if needed move the
  1916. * sender address from kernel to user space.
  1917. */
  1918. int __sys_recvfrom(int fd, void __user *ubuf, size_t size, unsigned int flags,
  1919. struct sockaddr __user *addr, int __user *addr_len)
  1920. {
  1921. struct sockaddr_storage address;
  1922. struct msghdr msg = {
  1923. /* Save some cycles and don't copy the address if not needed */
  1924. .msg_name = addr ? (struct sockaddr *)&address : NULL,
  1925. };
  1926. struct socket *sock;
  1927. int err, err2;
  1928. int fput_needed;
  1929. err = import_ubuf(ITER_DEST, ubuf, size, &msg.msg_iter);
  1930. if (unlikely(err))
  1931. return err;
  1932. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1933. if (!sock)
  1934. goto out;
  1935. if (sock->file->f_flags & O_NONBLOCK)
  1936. flags |= MSG_DONTWAIT;
  1937. err = sock_recvmsg(sock, &msg, flags);
  1938. if (err >= 0 && addr != NULL) {
  1939. err2 = move_addr_to_user(&address,
  1940. msg.msg_namelen, addr, addr_len);
  1941. if (err2 < 0)
  1942. err = err2;
  1943. }
  1944. fput_light(sock->file, fput_needed);
  1945. out:
  1946. return err;
  1947. }
  1948. SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
  1949. unsigned int, flags, struct sockaddr __user *, addr,
  1950. int __user *, addr_len)
  1951. {
  1952. return __sys_recvfrom(fd, ubuf, size, flags, addr, addr_len);
  1953. }
  1954. /*
  1955. * Receive a datagram from a socket.
  1956. */
  1957. SYSCALL_DEFINE4(recv, int, fd, void __user *, ubuf, size_t, size,
  1958. unsigned int, flags)
  1959. {
  1960. return __sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
  1961. }
  1962. static bool sock_use_custom_sol_socket(const struct socket *sock)
  1963. {
  1964. return test_bit(SOCK_CUSTOM_SOCKOPT, &sock->flags);
  1965. }
  1966. int do_sock_setsockopt(struct socket *sock, bool compat, int level,
  1967. int optname, sockptr_t optval, int optlen)
  1968. {
  1969. const struct proto_ops *ops;
  1970. char *kernel_optval = NULL;
  1971. int err;
  1972. if (optlen < 0)
  1973. return -EINVAL;
  1974. err = security_socket_setsockopt(sock, level, optname);
  1975. if (err)
  1976. goto out_put;
  1977. if (!compat)
  1978. err = BPF_CGROUP_RUN_PROG_SETSOCKOPT(sock->sk, &level, &optname,
  1979. optval, &optlen,
  1980. &kernel_optval);
  1981. if (err < 0)
  1982. goto out_put;
  1983. if (err > 0) {
  1984. err = 0;
  1985. goto out_put;
  1986. }
  1987. if (kernel_optval)
  1988. optval = KERNEL_SOCKPTR(kernel_optval);
  1989. ops = READ_ONCE(sock->ops);
  1990. if (level == SOL_SOCKET && !sock_use_custom_sol_socket(sock))
  1991. err = sock_setsockopt(sock, level, optname, optval, optlen);
  1992. else if (unlikely(!ops->setsockopt))
  1993. err = -EOPNOTSUPP;
  1994. else
  1995. err = ops->setsockopt(sock, level, optname, optval,
  1996. optlen);
  1997. kfree(kernel_optval);
  1998. out_put:
  1999. return err;
  2000. }
  2001. EXPORT_SYMBOL(do_sock_setsockopt);
  2002. /* Set a socket option. Because we don't know the option lengths we have
  2003. * to pass the user mode parameter for the protocols to sort out.
  2004. */
  2005. int __sys_setsockopt(int fd, int level, int optname, char __user *user_optval,
  2006. int optlen)
  2007. {
  2008. sockptr_t optval = USER_SOCKPTR(user_optval);
  2009. bool compat = in_compat_syscall();
  2010. int err, fput_needed;
  2011. struct socket *sock;
  2012. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  2013. if (!sock)
  2014. return err;
  2015. err = do_sock_setsockopt(sock, compat, level, optname, optval, optlen);
  2016. fput_light(sock->file, fput_needed);
  2017. return err;
  2018. }
  2019. SYSCALL_DEFINE5(setsockopt, int, fd, int, level, int, optname,
  2020. char __user *, optval, int, optlen)
  2021. {
  2022. return __sys_setsockopt(fd, level, optname, optval, optlen);
  2023. }
  2024. INDIRECT_CALLABLE_DECLARE(bool tcp_bpf_bypass_getsockopt(int level,
  2025. int optname));
  2026. int do_sock_getsockopt(struct socket *sock, bool compat, int level,
  2027. int optname, sockptr_t optval, sockptr_t optlen)
  2028. {
  2029. int max_optlen __maybe_unused = 0;
  2030. const struct proto_ops *ops;
  2031. int err;
  2032. err = security_socket_getsockopt(sock, level, optname);
  2033. if (err)
  2034. return err;
  2035. if (!compat)
  2036. copy_from_sockptr(&max_optlen, optlen, sizeof(int));
  2037. ops = READ_ONCE(sock->ops);
  2038. if (level == SOL_SOCKET) {
  2039. err = sk_getsockopt(sock->sk, level, optname, optval, optlen);
  2040. } else if (unlikely(!ops->getsockopt)) {
  2041. err = -EOPNOTSUPP;
  2042. } else {
  2043. if (WARN_ONCE(optval.is_kernel || optlen.is_kernel,
  2044. "Invalid argument type"))
  2045. return -EOPNOTSUPP;
  2046. err = ops->getsockopt(sock, level, optname, optval.user,
  2047. optlen.user);
  2048. }
  2049. if (!compat)
  2050. err = BPF_CGROUP_RUN_PROG_GETSOCKOPT(sock->sk, level, optname,
  2051. optval, optlen, max_optlen,
  2052. err);
  2053. return err;
  2054. }
  2055. EXPORT_SYMBOL(do_sock_getsockopt);
  2056. /*
  2057. * Get a socket option. Because we don't know the option lengths we have
  2058. * to pass a user mode parameter for the protocols to sort out.
  2059. */
  2060. int __sys_getsockopt(int fd, int level, int optname, char __user *optval,
  2061. int __user *optlen)
  2062. {
  2063. int err, fput_needed;
  2064. struct socket *sock;
  2065. bool compat;
  2066. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  2067. if (!sock)
  2068. return err;
  2069. compat = in_compat_syscall();
  2070. err = do_sock_getsockopt(sock, compat, level, optname,
  2071. USER_SOCKPTR(optval), USER_SOCKPTR(optlen));
  2072. fput_light(sock->file, fput_needed);
  2073. return err;
  2074. }
  2075. SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
  2076. char __user *, optval, int __user *, optlen)
  2077. {
  2078. return __sys_getsockopt(fd, level, optname, optval, optlen);
  2079. }
  2080. /*
  2081. * Shutdown a socket.
  2082. */
  2083. int __sys_shutdown_sock(struct socket *sock, int how)
  2084. {
  2085. int err;
  2086. err = security_socket_shutdown(sock, how);
  2087. if (!err)
  2088. err = READ_ONCE(sock->ops)->shutdown(sock, how);
  2089. return err;
  2090. }
  2091. int __sys_shutdown(int fd, int how)
  2092. {
  2093. int err, fput_needed;
  2094. struct socket *sock;
  2095. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  2096. if (sock != NULL) {
  2097. err = __sys_shutdown_sock(sock, how);
  2098. fput_light(sock->file, fput_needed);
  2099. }
  2100. return err;
  2101. }
  2102. SYSCALL_DEFINE2(shutdown, int, fd, int, how)
  2103. {
  2104. return __sys_shutdown(fd, how);
  2105. }
  2106. /* A couple of helpful macros for getting the address of the 32/64 bit
  2107. * fields which are the same type (int / unsigned) on our platforms.
  2108. */
  2109. #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
  2110. #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
  2111. #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
  2112. struct used_address {
  2113. struct sockaddr_storage name;
  2114. unsigned int name_len;
  2115. };
  2116. int __copy_msghdr(struct msghdr *kmsg,
  2117. struct user_msghdr *msg,
  2118. struct sockaddr __user **save_addr)
  2119. {
  2120. ssize_t err;
  2121. kmsg->msg_control_is_user = true;
  2122. kmsg->msg_get_inq = 0;
  2123. kmsg->msg_control_user = msg->msg_control;
  2124. kmsg->msg_controllen = msg->msg_controllen;
  2125. kmsg->msg_flags = msg->msg_flags;
  2126. kmsg->msg_namelen = msg->msg_namelen;
  2127. if (!msg->msg_name)
  2128. kmsg->msg_namelen = 0;
  2129. if (kmsg->msg_namelen < 0)
  2130. return -EINVAL;
  2131. if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
  2132. kmsg->msg_namelen = sizeof(struct sockaddr_storage);
  2133. if (save_addr)
  2134. *save_addr = msg->msg_name;
  2135. if (msg->msg_name && kmsg->msg_namelen) {
  2136. if (!save_addr) {
  2137. err = move_addr_to_kernel(msg->msg_name,
  2138. kmsg->msg_namelen,
  2139. kmsg->msg_name);
  2140. if (err < 0)
  2141. return err;
  2142. }
  2143. } else {
  2144. kmsg->msg_name = NULL;
  2145. kmsg->msg_namelen = 0;
  2146. }
  2147. if (msg->msg_iovlen > UIO_MAXIOV)
  2148. return -EMSGSIZE;
  2149. kmsg->msg_iocb = NULL;
  2150. kmsg->msg_ubuf = NULL;
  2151. return 0;
  2152. }
  2153. static int copy_msghdr_from_user(struct msghdr *kmsg,
  2154. struct user_msghdr __user *umsg,
  2155. struct sockaddr __user **save_addr,
  2156. struct iovec **iov)
  2157. {
  2158. struct user_msghdr msg;
  2159. ssize_t err;
  2160. if (copy_from_user(&msg, umsg, sizeof(*umsg)))
  2161. return -EFAULT;
  2162. err = __copy_msghdr(kmsg, &msg, save_addr);
  2163. if (err)
  2164. return err;
  2165. err = import_iovec(save_addr ? ITER_DEST : ITER_SOURCE,
  2166. msg.msg_iov, msg.msg_iovlen,
  2167. UIO_FASTIOV, iov, &kmsg->msg_iter);
  2168. return err < 0 ? err : 0;
  2169. }
  2170. static int ____sys_sendmsg(struct socket *sock, struct msghdr *msg_sys,
  2171. unsigned int flags, struct used_address *used_address,
  2172. unsigned int allowed_msghdr_flags)
  2173. {
  2174. unsigned char ctl[sizeof(struct cmsghdr) + 20]
  2175. __aligned(sizeof(__kernel_size_t));
  2176. /* 20 is size of ipv6_pktinfo */
  2177. unsigned char *ctl_buf = ctl;
  2178. int ctl_len;
  2179. ssize_t err;
  2180. err = -ENOBUFS;
  2181. if (msg_sys->msg_controllen > INT_MAX)
  2182. goto out;
  2183. flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
  2184. ctl_len = msg_sys->msg_controllen;
  2185. if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
  2186. err =
  2187. cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
  2188. sizeof(ctl));
  2189. if (err)
  2190. goto out;
  2191. ctl_buf = msg_sys->msg_control;
  2192. ctl_len = msg_sys->msg_controllen;
  2193. } else if (ctl_len) {
  2194. BUILD_BUG_ON(sizeof(struct cmsghdr) !=
  2195. CMSG_ALIGN(sizeof(struct cmsghdr)));
  2196. if (ctl_len > sizeof(ctl)) {
  2197. ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
  2198. if (ctl_buf == NULL)
  2199. goto out;
  2200. }
  2201. err = -EFAULT;
  2202. if (copy_from_user(ctl_buf, msg_sys->msg_control_user, ctl_len))
  2203. goto out_freectl;
  2204. msg_sys->msg_control = ctl_buf;
  2205. msg_sys->msg_control_is_user = false;
  2206. }
  2207. flags &= ~MSG_INTERNAL_SENDMSG_FLAGS;
  2208. msg_sys->msg_flags = flags;
  2209. if (sock->file->f_flags & O_NONBLOCK)
  2210. msg_sys->msg_flags |= MSG_DONTWAIT;
  2211. /*
  2212. * If this is sendmmsg() and current destination address is same as
  2213. * previously succeeded address, omit asking LSM's decision.
  2214. * used_address->name_len is initialized to UINT_MAX so that the first
  2215. * destination address never matches.
  2216. */
  2217. if (used_address && msg_sys->msg_name &&
  2218. used_address->name_len == msg_sys->msg_namelen &&
  2219. !memcmp(&used_address->name, msg_sys->msg_name,
  2220. used_address->name_len)) {
  2221. err = sock_sendmsg_nosec(sock, msg_sys);
  2222. goto out_freectl;
  2223. }
  2224. err = __sock_sendmsg(sock, msg_sys);
  2225. /*
  2226. * If this is sendmmsg() and sending to current destination address was
  2227. * successful, remember it.
  2228. */
  2229. if (used_address && err >= 0) {
  2230. used_address->name_len = msg_sys->msg_namelen;
  2231. if (msg_sys->msg_name)
  2232. memcpy(&used_address->name, msg_sys->msg_name,
  2233. used_address->name_len);
  2234. }
  2235. out_freectl:
  2236. if (ctl_buf != ctl)
  2237. sock_kfree_s(sock->sk, ctl_buf, ctl_len);
  2238. out:
  2239. return err;
  2240. }
  2241. static int sendmsg_copy_msghdr(struct msghdr *msg,
  2242. struct user_msghdr __user *umsg, unsigned flags,
  2243. struct iovec **iov)
  2244. {
  2245. int err;
  2246. if (flags & MSG_CMSG_COMPAT) {
  2247. struct compat_msghdr __user *msg_compat;
  2248. msg_compat = (struct compat_msghdr __user *) umsg;
  2249. err = get_compat_msghdr(msg, msg_compat, NULL, iov);
  2250. } else {
  2251. err = copy_msghdr_from_user(msg, umsg, NULL, iov);
  2252. }
  2253. if (err < 0)
  2254. return err;
  2255. return 0;
  2256. }
  2257. static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
  2258. struct msghdr *msg_sys, unsigned int flags,
  2259. struct used_address *used_address,
  2260. unsigned int allowed_msghdr_flags)
  2261. {
  2262. struct sockaddr_storage address;
  2263. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  2264. ssize_t err;
  2265. msg_sys->msg_name = &address;
  2266. err = sendmsg_copy_msghdr(msg_sys, msg, flags, &iov);
  2267. if (err < 0)
  2268. return err;
  2269. err = ____sys_sendmsg(sock, msg_sys, flags, used_address,
  2270. allowed_msghdr_flags);
  2271. kfree(iov);
  2272. return err;
  2273. }
  2274. /*
  2275. * BSD sendmsg interface
  2276. */
  2277. long __sys_sendmsg_sock(struct socket *sock, struct msghdr *msg,
  2278. unsigned int flags)
  2279. {
  2280. return ____sys_sendmsg(sock, msg, flags, NULL, 0);
  2281. }
  2282. long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
  2283. bool forbid_cmsg_compat)
  2284. {
  2285. int fput_needed, err;
  2286. struct msghdr msg_sys;
  2287. struct socket *sock;
  2288. if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
  2289. return -EINVAL;
  2290. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  2291. if (!sock)
  2292. goto out;
  2293. err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
  2294. fput_light(sock->file, fput_needed);
  2295. out:
  2296. return err;
  2297. }
  2298. SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
  2299. {
  2300. return __sys_sendmsg(fd, msg, flags, true);
  2301. }
  2302. /*
  2303. * Linux sendmmsg interface
  2304. */
  2305. int __sys_sendmmsg(int fd, struct mmsghdr __user *mmsg, unsigned int vlen,
  2306. unsigned int flags, bool forbid_cmsg_compat)
  2307. {
  2308. int fput_needed, err, datagrams;
  2309. struct socket *sock;
  2310. struct mmsghdr __user *entry;
  2311. struct compat_mmsghdr __user *compat_entry;
  2312. struct msghdr msg_sys;
  2313. struct used_address used_address;
  2314. unsigned int oflags = flags;
  2315. if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
  2316. return -EINVAL;
  2317. if (vlen > UIO_MAXIOV)
  2318. vlen = UIO_MAXIOV;
  2319. datagrams = 0;
  2320. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  2321. if (!sock)
  2322. return err;
  2323. used_address.name_len = UINT_MAX;
  2324. entry = mmsg;
  2325. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  2326. err = 0;
  2327. flags |= MSG_BATCH;
  2328. while (datagrams < vlen) {
  2329. if (datagrams == vlen - 1)
  2330. flags = oflags;
  2331. if (MSG_CMSG_COMPAT & flags) {
  2332. err = ___sys_sendmsg(sock, (struct user_msghdr __user *)compat_entry,
  2333. &msg_sys, flags, &used_address, MSG_EOR);
  2334. if (err < 0)
  2335. break;
  2336. err = __put_user(err, &compat_entry->msg_len);
  2337. ++compat_entry;
  2338. } else {
  2339. err = ___sys_sendmsg(sock,
  2340. (struct user_msghdr __user *)entry,
  2341. &msg_sys, flags, &used_address, MSG_EOR);
  2342. if (err < 0)
  2343. break;
  2344. err = put_user(err, &entry->msg_len);
  2345. ++entry;
  2346. }
  2347. if (err)
  2348. break;
  2349. ++datagrams;
  2350. if (msg_data_left(&msg_sys))
  2351. break;
  2352. cond_resched();
  2353. }
  2354. fput_light(sock->file, fput_needed);
  2355. /* We only return an error if no datagrams were able to be sent */
  2356. if (datagrams != 0)
  2357. return datagrams;
  2358. return err;
  2359. }
  2360. SYSCALL_DEFINE4(sendmmsg, int, fd, struct mmsghdr __user *, mmsg,
  2361. unsigned int, vlen, unsigned int, flags)
  2362. {
  2363. return __sys_sendmmsg(fd, mmsg, vlen, flags, true);
  2364. }
  2365. static int recvmsg_copy_msghdr(struct msghdr *msg,
  2366. struct user_msghdr __user *umsg, unsigned flags,
  2367. struct sockaddr __user **uaddr,
  2368. struct iovec **iov)
  2369. {
  2370. ssize_t err;
  2371. if (MSG_CMSG_COMPAT & flags) {
  2372. struct compat_msghdr __user *msg_compat;
  2373. msg_compat = (struct compat_msghdr __user *) umsg;
  2374. err = get_compat_msghdr(msg, msg_compat, uaddr, iov);
  2375. } else {
  2376. err = copy_msghdr_from_user(msg, umsg, uaddr, iov);
  2377. }
  2378. if (err < 0)
  2379. return err;
  2380. return 0;
  2381. }
  2382. static int ____sys_recvmsg(struct socket *sock, struct msghdr *msg_sys,
  2383. struct user_msghdr __user *msg,
  2384. struct sockaddr __user *uaddr,
  2385. unsigned int flags, int nosec)
  2386. {
  2387. struct compat_msghdr __user *msg_compat =
  2388. (struct compat_msghdr __user *) msg;
  2389. int __user *uaddr_len = COMPAT_NAMELEN(msg);
  2390. struct sockaddr_storage addr;
  2391. unsigned long cmsg_ptr;
  2392. int len;
  2393. ssize_t err;
  2394. msg_sys->msg_name = &addr;
  2395. cmsg_ptr = (unsigned long)msg_sys->msg_control;
  2396. msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
  2397. /* We assume all kernel code knows the size of sockaddr_storage */
  2398. msg_sys->msg_namelen = 0;
  2399. if (sock->file->f_flags & O_NONBLOCK)
  2400. flags |= MSG_DONTWAIT;
  2401. if (unlikely(nosec))
  2402. err = sock_recvmsg_nosec(sock, msg_sys, flags);
  2403. else
  2404. err = sock_recvmsg(sock, msg_sys, flags);
  2405. if (err < 0)
  2406. goto out;
  2407. len = err;
  2408. if (uaddr != NULL) {
  2409. err = move_addr_to_user(&addr,
  2410. msg_sys->msg_namelen, uaddr,
  2411. uaddr_len);
  2412. if (err < 0)
  2413. goto out;
  2414. }
  2415. err = __put_user((msg_sys->msg_flags & ~MSG_CMSG_COMPAT),
  2416. COMPAT_FLAGS(msg));
  2417. if (err)
  2418. goto out;
  2419. if (MSG_CMSG_COMPAT & flags)
  2420. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  2421. &msg_compat->msg_controllen);
  2422. else
  2423. err = __put_user((unsigned long)msg_sys->msg_control - cmsg_ptr,
  2424. &msg->msg_controllen);
  2425. if (err)
  2426. goto out;
  2427. err = len;
  2428. out:
  2429. return err;
  2430. }
  2431. static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
  2432. struct msghdr *msg_sys, unsigned int flags, int nosec)
  2433. {
  2434. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  2435. /* user mode address pointers */
  2436. struct sockaddr __user *uaddr;
  2437. ssize_t err;
  2438. err = recvmsg_copy_msghdr(msg_sys, msg, flags, &uaddr, &iov);
  2439. if (err < 0)
  2440. return err;
  2441. err = ____sys_recvmsg(sock, msg_sys, msg, uaddr, flags, nosec);
  2442. kfree(iov);
  2443. return err;
  2444. }
  2445. /*
  2446. * BSD recvmsg interface
  2447. */
  2448. long __sys_recvmsg_sock(struct socket *sock, struct msghdr *msg,
  2449. struct user_msghdr __user *umsg,
  2450. struct sockaddr __user *uaddr, unsigned int flags)
  2451. {
  2452. return ____sys_recvmsg(sock, msg, umsg, uaddr, flags, 0);
  2453. }
  2454. long __sys_recvmsg(int fd, struct user_msghdr __user *msg, unsigned int flags,
  2455. bool forbid_cmsg_compat)
  2456. {
  2457. int fput_needed, err;
  2458. struct msghdr msg_sys;
  2459. struct socket *sock;
  2460. if (forbid_cmsg_compat && (flags & MSG_CMSG_COMPAT))
  2461. return -EINVAL;
  2462. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  2463. if (!sock)
  2464. goto out;
  2465. err = ___sys_recvmsg(sock, msg, &msg_sys, flags, 0);
  2466. fput_light(sock->file, fput_needed);
  2467. out:
  2468. return err;
  2469. }
  2470. SYSCALL_DEFINE3(recvmsg, int, fd, struct user_msghdr __user *, msg,
  2471. unsigned int, flags)
  2472. {
  2473. return __sys_recvmsg(fd, msg, flags, true);
  2474. }
  2475. /*
  2476. * Linux recvmmsg interface
  2477. */
  2478. static int do_recvmmsg(int fd, struct mmsghdr __user *mmsg,
  2479. unsigned int vlen, unsigned int flags,
  2480. struct timespec64 *timeout)
  2481. {
  2482. int fput_needed, err, datagrams;
  2483. struct socket *sock;
  2484. struct mmsghdr __user *entry;
  2485. struct compat_mmsghdr __user *compat_entry;
  2486. struct msghdr msg_sys;
  2487. struct timespec64 end_time;
  2488. struct timespec64 timeout64;
  2489. if (timeout &&
  2490. poll_select_set_timeout(&end_time, timeout->tv_sec,
  2491. timeout->tv_nsec))
  2492. return -EINVAL;
  2493. datagrams = 0;
  2494. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  2495. if (!sock)
  2496. return err;
  2497. if (likely(!(flags & MSG_ERRQUEUE))) {
  2498. err = sock_error(sock->sk);
  2499. if (err) {
  2500. datagrams = err;
  2501. goto out_put;
  2502. }
  2503. }
  2504. entry = mmsg;
  2505. compat_entry = (struct compat_mmsghdr __user *)mmsg;
  2506. while (datagrams < vlen) {
  2507. /*
  2508. * No need to ask LSM for more than the first datagram.
  2509. */
  2510. if (MSG_CMSG_COMPAT & flags) {
  2511. err = ___sys_recvmsg(sock, (struct user_msghdr __user *)compat_entry,
  2512. &msg_sys, flags & ~MSG_WAITFORONE,
  2513. datagrams);
  2514. if (err < 0)
  2515. break;
  2516. err = __put_user(err, &compat_entry->msg_len);
  2517. ++compat_entry;
  2518. } else {
  2519. err = ___sys_recvmsg(sock,
  2520. (struct user_msghdr __user *)entry,
  2521. &msg_sys, flags & ~MSG_WAITFORONE,
  2522. datagrams);
  2523. if (err < 0)
  2524. break;
  2525. err = put_user(err, &entry->msg_len);
  2526. ++entry;
  2527. }
  2528. if (err)
  2529. break;
  2530. ++datagrams;
  2531. /* MSG_WAITFORONE turns on MSG_DONTWAIT after one packet */
  2532. if (flags & MSG_WAITFORONE)
  2533. flags |= MSG_DONTWAIT;
  2534. if (timeout) {
  2535. ktime_get_ts64(&timeout64);
  2536. *timeout = timespec64_sub(end_time, timeout64);
  2537. if (timeout->tv_sec < 0) {
  2538. timeout->tv_sec = timeout->tv_nsec = 0;
  2539. break;
  2540. }
  2541. /* Timeout, return less than vlen datagrams */
  2542. if (timeout->tv_nsec == 0 && timeout->tv_sec == 0)
  2543. break;
  2544. }
  2545. /* Out of band data, return right away */
  2546. if (msg_sys.msg_flags & MSG_OOB)
  2547. break;
  2548. cond_resched();
  2549. }
  2550. if (err == 0)
  2551. goto out_put;
  2552. if (datagrams == 0) {
  2553. datagrams = err;
  2554. goto out_put;
  2555. }
  2556. /*
  2557. * We may return less entries than requested (vlen) if the
  2558. * sock is non block and there aren't enough datagrams...
  2559. */
  2560. if (err != -EAGAIN) {
  2561. /*
  2562. * ... or if recvmsg returns an error after we
  2563. * received some datagrams, where we record the
  2564. * error to return on the next call or if the
  2565. * app asks about it using getsockopt(SO_ERROR).
  2566. */
  2567. WRITE_ONCE(sock->sk->sk_err, -err);
  2568. }
  2569. out_put:
  2570. fput_light(sock->file, fput_needed);
  2571. return datagrams;
  2572. }
  2573. int __sys_recvmmsg(int fd, struct mmsghdr __user *mmsg,
  2574. unsigned int vlen, unsigned int flags,
  2575. struct __kernel_timespec __user *timeout,
  2576. struct old_timespec32 __user *timeout32)
  2577. {
  2578. int datagrams;
  2579. struct timespec64 timeout_sys;
  2580. if (timeout && get_timespec64(&timeout_sys, timeout))
  2581. return -EFAULT;
  2582. if (timeout32 && get_old_timespec32(&timeout_sys, timeout32))
  2583. return -EFAULT;
  2584. if (!timeout && !timeout32)
  2585. return do_recvmmsg(fd, mmsg, vlen, flags, NULL);
  2586. datagrams = do_recvmmsg(fd, mmsg, vlen, flags, &timeout_sys);
  2587. if (datagrams <= 0)
  2588. return datagrams;
  2589. if (timeout && put_timespec64(&timeout_sys, timeout))
  2590. datagrams = -EFAULT;
  2591. if (timeout32 && put_old_timespec32(&timeout_sys, timeout32))
  2592. datagrams = -EFAULT;
  2593. return datagrams;
  2594. }
  2595. SYSCALL_DEFINE5(recvmmsg, int, fd, struct mmsghdr __user *, mmsg,
  2596. unsigned int, vlen, unsigned int, flags,
  2597. struct __kernel_timespec __user *, timeout)
  2598. {
  2599. if (flags & MSG_CMSG_COMPAT)
  2600. return -EINVAL;
  2601. return __sys_recvmmsg(fd, mmsg, vlen, flags, timeout, NULL);
  2602. }
  2603. #ifdef CONFIG_COMPAT_32BIT_TIME
  2604. SYSCALL_DEFINE5(recvmmsg_time32, int, fd, struct mmsghdr __user *, mmsg,
  2605. unsigned int, vlen, unsigned int, flags,
  2606. struct old_timespec32 __user *, timeout)
  2607. {
  2608. if (flags & MSG_CMSG_COMPAT)
  2609. return -EINVAL;
  2610. return __sys_recvmmsg(fd, mmsg, vlen, flags, NULL, timeout);
  2611. }
  2612. #endif
  2613. #ifdef __ARCH_WANT_SYS_SOCKETCALL
  2614. /* Argument list sizes for sys_socketcall */
  2615. #define AL(x) ((x) * sizeof(unsigned long))
  2616. static const unsigned char nargs[21] = {
  2617. AL(0), AL(3), AL(3), AL(3), AL(2), AL(3),
  2618. AL(3), AL(3), AL(4), AL(4), AL(4), AL(6),
  2619. AL(6), AL(2), AL(5), AL(5), AL(3), AL(3),
  2620. AL(4), AL(5), AL(4)
  2621. };
  2622. #undef AL
  2623. /*
  2624. * System call vectors.
  2625. *
  2626. * Argument checking cleaned up. Saved 20% in size.
  2627. * This function doesn't need to set the kernel lock because
  2628. * it is set by the callees.
  2629. */
  2630. SYSCALL_DEFINE2(socketcall, int, call, unsigned long __user *, args)
  2631. {
  2632. unsigned long a[AUDITSC_ARGS];
  2633. unsigned long a0, a1;
  2634. int err;
  2635. unsigned int len;
  2636. if (call < 1 || call > SYS_SENDMMSG)
  2637. return -EINVAL;
  2638. call = array_index_nospec(call, SYS_SENDMMSG + 1);
  2639. len = nargs[call];
  2640. if (len > sizeof(a))
  2641. return -EINVAL;
  2642. /* copy_from_user should be SMP safe. */
  2643. if (copy_from_user(a, args, len))
  2644. return -EFAULT;
  2645. err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
  2646. if (err)
  2647. return err;
  2648. a0 = a[0];
  2649. a1 = a[1];
  2650. switch (call) {
  2651. case SYS_SOCKET:
  2652. err = __sys_socket(a0, a1, a[2]);
  2653. break;
  2654. case SYS_BIND:
  2655. err = __sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
  2656. break;
  2657. case SYS_CONNECT:
  2658. err = __sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
  2659. break;
  2660. case SYS_LISTEN:
  2661. err = __sys_listen(a0, a1);
  2662. break;
  2663. case SYS_ACCEPT:
  2664. err = __sys_accept4(a0, (struct sockaddr __user *)a1,
  2665. (int __user *)a[2], 0);
  2666. break;
  2667. case SYS_GETSOCKNAME:
  2668. err =
  2669. __sys_getsockname(a0, (struct sockaddr __user *)a1,
  2670. (int __user *)a[2]);
  2671. break;
  2672. case SYS_GETPEERNAME:
  2673. err =
  2674. __sys_getpeername(a0, (struct sockaddr __user *)a1,
  2675. (int __user *)a[2]);
  2676. break;
  2677. case SYS_SOCKETPAIR:
  2678. err = __sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
  2679. break;
  2680. case SYS_SEND:
  2681. err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
  2682. NULL, 0);
  2683. break;
  2684. case SYS_SENDTO:
  2685. err = __sys_sendto(a0, (void __user *)a1, a[2], a[3],
  2686. (struct sockaddr __user *)a[4], a[5]);
  2687. break;
  2688. case SYS_RECV:
  2689. err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  2690. NULL, NULL);
  2691. break;
  2692. case SYS_RECVFROM:
  2693. err = __sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  2694. (struct sockaddr __user *)a[4],
  2695. (int __user *)a[5]);
  2696. break;
  2697. case SYS_SHUTDOWN:
  2698. err = __sys_shutdown(a0, a1);
  2699. break;
  2700. case SYS_SETSOCKOPT:
  2701. err = __sys_setsockopt(a0, a1, a[2], (char __user *)a[3],
  2702. a[4]);
  2703. break;
  2704. case SYS_GETSOCKOPT:
  2705. err =
  2706. __sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
  2707. (int __user *)a[4]);
  2708. break;
  2709. case SYS_SENDMSG:
  2710. err = __sys_sendmsg(a0, (struct user_msghdr __user *)a1,
  2711. a[2], true);
  2712. break;
  2713. case SYS_SENDMMSG:
  2714. err = __sys_sendmmsg(a0, (struct mmsghdr __user *)a1, a[2],
  2715. a[3], true);
  2716. break;
  2717. case SYS_RECVMSG:
  2718. err = __sys_recvmsg(a0, (struct user_msghdr __user *)a1,
  2719. a[2], true);
  2720. break;
  2721. case SYS_RECVMMSG:
  2722. if (IS_ENABLED(CONFIG_64BIT))
  2723. err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
  2724. a[2], a[3],
  2725. (struct __kernel_timespec __user *)a[4],
  2726. NULL);
  2727. else
  2728. err = __sys_recvmmsg(a0, (struct mmsghdr __user *)a1,
  2729. a[2], a[3], NULL,
  2730. (struct old_timespec32 __user *)a[4]);
  2731. break;
  2732. case SYS_ACCEPT4:
  2733. err = __sys_accept4(a0, (struct sockaddr __user *)a1,
  2734. (int __user *)a[2], a[3]);
  2735. break;
  2736. default:
  2737. err = -EINVAL;
  2738. break;
  2739. }
  2740. return err;
  2741. }
  2742. #endif /* __ARCH_WANT_SYS_SOCKETCALL */
  2743. /**
  2744. * sock_register - add a socket protocol handler
  2745. * @ops: description of protocol
  2746. *
  2747. * This function is called by a protocol handler that wants to
  2748. * advertise its address family, and have it linked into the
  2749. * socket interface. The value ops->family corresponds to the
  2750. * socket system call protocol family.
  2751. */
  2752. int sock_register(const struct net_proto_family *ops)
  2753. {
  2754. int err;
  2755. if (ops->family >= NPROTO) {
  2756. pr_crit("protocol %d >= NPROTO(%d)\n", ops->family, NPROTO);
  2757. return -ENOBUFS;
  2758. }
  2759. spin_lock(&net_family_lock);
  2760. if (rcu_dereference_protected(net_families[ops->family],
  2761. lockdep_is_held(&net_family_lock)))
  2762. err = -EEXIST;
  2763. else {
  2764. rcu_assign_pointer(net_families[ops->family], ops);
  2765. err = 0;
  2766. }
  2767. spin_unlock(&net_family_lock);
  2768. pr_info("NET: Registered %s protocol family\n", pf_family_names[ops->family]);
  2769. return err;
  2770. }
  2771. EXPORT_SYMBOL(sock_register);
  2772. /**
  2773. * sock_unregister - remove a protocol handler
  2774. * @family: protocol family to remove
  2775. *
  2776. * This function is called by a protocol handler that wants to
  2777. * remove its address family, and have it unlinked from the
  2778. * new socket creation.
  2779. *
  2780. * If protocol handler is a module, then it can use module reference
  2781. * counts to protect against new references. If protocol handler is not
  2782. * a module then it needs to provide its own protection in
  2783. * the ops->create routine.
  2784. */
  2785. void sock_unregister(int family)
  2786. {
  2787. BUG_ON(family < 0 || family >= NPROTO);
  2788. spin_lock(&net_family_lock);
  2789. RCU_INIT_POINTER(net_families[family], NULL);
  2790. spin_unlock(&net_family_lock);
  2791. synchronize_rcu();
  2792. pr_info("NET: Unregistered %s protocol family\n", pf_family_names[family]);
  2793. }
  2794. EXPORT_SYMBOL(sock_unregister);
  2795. bool sock_is_registered(int family)
  2796. {
  2797. return family < NPROTO && rcu_access_pointer(net_families[family]);
  2798. }
  2799. static int __init sock_init(void)
  2800. {
  2801. int err;
  2802. /*
  2803. * Initialize the network sysctl infrastructure.
  2804. */
  2805. err = net_sysctl_init();
  2806. if (err)
  2807. goto out;
  2808. /*
  2809. * Initialize skbuff SLAB cache
  2810. */
  2811. skb_init();
  2812. /*
  2813. * Initialize the protocols module.
  2814. */
  2815. init_inodecache();
  2816. err = register_filesystem(&sock_fs_type);
  2817. if (err)
  2818. goto out;
  2819. sock_mnt = kern_mount(&sock_fs_type);
  2820. if (IS_ERR(sock_mnt)) {
  2821. err = PTR_ERR(sock_mnt);
  2822. goto out_mount;
  2823. }
  2824. /* The real protocol initialization is performed in later initcalls.
  2825. */
  2826. #ifdef CONFIG_NETFILTER
  2827. err = netfilter_init();
  2828. if (err)
  2829. goto out;
  2830. #endif
  2831. ptp_classifier_init();
  2832. out:
  2833. return err;
  2834. out_mount:
  2835. unregister_filesystem(&sock_fs_type);
  2836. goto out;
  2837. }
  2838. core_initcall(sock_init); /* early initcall */
  2839. #ifdef CONFIG_PROC_FS
  2840. void socket_seq_show(struct seq_file *seq)
  2841. {
  2842. seq_printf(seq, "sockets: used %d\n",
  2843. sock_inuse_get(seq->private));
  2844. }
  2845. #endif /* CONFIG_PROC_FS */
  2846. /* Handle the fact that while struct ifreq has the same *layout* on
  2847. * 32/64 for everything but ifreq::ifru_ifmap and ifreq::ifru_data,
  2848. * which are handled elsewhere, it still has different *size* due to
  2849. * ifreq::ifru_ifmap (which is 16 bytes on 32 bit, 24 bytes on 64-bit,
  2850. * resulting in struct ifreq being 32 and 40 bytes respectively).
  2851. * As a result, if the struct happens to be at the end of a page and
  2852. * the next page isn't readable/writable, we get a fault. To prevent
  2853. * that, copy back and forth to the full size.
  2854. */
  2855. int get_user_ifreq(struct ifreq *ifr, void __user **ifrdata, void __user *arg)
  2856. {
  2857. if (in_compat_syscall()) {
  2858. struct compat_ifreq *ifr32 = (struct compat_ifreq *)ifr;
  2859. memset(ifr, 0, sizeof(*ifr));
  2860. if (copy_from_user(ifr32, arg, sizeof(*ifr32)))
  2861. return -EFAULT;
  2862. if (ifrdata)
  2863. *ifrdata = compat_ptr(ifr32->ifr_data);
  2864. return 0;
  2865. }
  2866. if (copy_from_user(ifr, arg, sizeof(*ifr)))
  2867. return -EFAULT;
  2868. if (ifrdata)
  2869. *ifrdata = ifr->ifr_data;
  2870. return 0;
  2871. }
  2872. EXPORT_SYMBOL(get_user_ifreq);
  2873. int put_user_ifreq(struct ifreq *ifr, void __user *arg)
  2874. {
  2875. size_t size = sizeof(*ifr);
  2876. if (in_compat_syscall())
  2877. size = sizeof(struct compat_ifreq);
  2878. if (copy_to_user(arg, ifr, size))
  2879. return -EFAULT;
  2880. return 0;
  2881. }
  2882. EXPORT_SYMBOL(put_user_ifreq);
  2883. #ifdef CONFIG_COMPAT
  2884. static int compat_siocwandev(struct net *net, struct compat_ifreq __user *uifr32)
  2885. {
  2886. compat_uptr_t uptr32;
  2887. struct ifreq ifr;
  2888. void __user *saved;
  2889. int err;
  2890. if (get_user_ifreq(&ifr, NULL, uifr32))
  2891. return -EFAULT;
  2892. if (get_user(uptr32, &uifr32->ifr_settings.ifs_ifsu))
  2893. return -EFAULT;
  2894. saved = ifr.ifr_settings.ifs_ifsu.raw_hdlc;
  2895. ifr.ifr_settings.ifs_ifsu.raw_hdlc = compat_ptr(uptr32);
  2896. err = dev_ioctl(net, SIOCWANDEV, &ifr, NULL, NULL);
  2897. if (!err) {
  2898. ifr.ifr_settings.ifs_ifsu.raw_hdlc = saved;
  2899. if (put_user_ifreq(&ifr, uifr32))
  2900. err = -EFAULT;
  2901. }
  2902. return err;
  2903. }
  2904. /* Handle ioctls that use ifreq::ifr_data and just need struct ifreq converted */
  2905. static int compat_ifr_data_ioctl(struct net *net, unsigned int cmd,
  2906. struct compat_ifreq __user *u_ifreq32)
  2907. {
  2908. struct ifreq ifreq;
  2909. void __user *data;
  2910. if (!is_socket_ioctl_cmd(cmd))
  2911. return -ENOTTY;
  2912. if (get_user_ifreq(&ifreq, &data, u_ifreq32))
  2913. return -EFAULT;
  2914. ifreq.ifr_data = data;
  2915. return dev_ioctl(net, cmd, &ifreq, data, NULL);
  2916. }
  2917. static int compat_sock_ioctl_trans(struct file *file, struct socket *sock,
  2918. unsigned int cmd, unsigned long arg)
  2919. {
  2920. void __user *argp = compat_ptr(arg);
  2921. struct sock *sk = sock->sk;
  2922. struct net *net = sock_net(sk);
  2923. const struct proto_ops *ops;
  2924. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15))
  2925. return sock_ioctl(file, cmd, (unsigned long)argp);
  2926. switch (cmd) {
  2927. case SIOCWANDEV:
  2928. return compat_siocwandev(net, argp);
  2929. case SIOCGSTAMP_OLD:
  2930. case SIOCGSTAMPNS_OLD:
  2931. ops = READ_ONCE(sock->ops);
  2932. if (!ops->gettstamp)
  2933. return -ENOIOCTLCMD;
  2934. return ops->gettstamp(sock, argp, cmd == SIOCGSTAMP_OLD,
  2935. !COMPAT_USE_64BIT_TIME);
  2936. case SIOCETHTOOL:
  2937. case SIOCBONDSLAVEINFOQUERY:
  2938. case SIOCBONDINFOQUERY:
  2939. case SIOCSHWTSTAMP:
  2940. case SIOCGHWTSTAMP:
  2941. return compat_ifr_data_ioctl(net, cmd, argp);
  2942. case FIOSETOWN:
  2943. case SIOCSPGRP:
  2944. case FIOGETOWN:
  2945. case SIOCGPGRP:
  2946. case SIOCBRADDBR:
  2947. case SIOCBRDELBR:
  2948. case SIOCGIFVLAN:
  2949. case SIOCSIFVLAN:
  2950. case SIOCGSKNS:
  2951. case SIOCGSTAMP_NEW:
  2952. case SIOCGSTAMPNS_NEW:
  2953. case SIOCGIFCONF:
  2954. case SIOCSIFBR:
  2955. case SIOCGIFBR:
  2956. return sock_ioctl(file, cmd, arg);
  2957. case SIOCGIFFLAGS:
  2958. case SIOCSIFFLAGS:
  2959. case SIOCGIFMAP:
  2960. case SIOCSIFMAP:
  2961. case SIOCGIFMETRIC:
  2962. case SIOCSIFMETRIC:
  2963. case SIOCGIFMTU:
  2964. case SIOCSIFMTU:
  2965. case SIOCGIFMEM:
  2966. case SIOCSIFMEM:
  2967. case SIOCGIFHWADDR:
  2968. case SIOCSIFHWADDR:
  2969. case SIOCADDMULTI:
  2970. case SIOCDELMULTI:
  2971. case SIOCGIFINDEX:
  2972. case SIOCGIFADDR:
  2973. case SIOCSIFADDR:
  2974. case SIOCSIFHWBROADCAST:
  2975. case SIOCDIFADDR:
  2976. case SIOCGIFBRDADDR:
  2977. case SIOCSIFBRDADDR:
  2978. case SIOCGIFDSTADDR:
  2979. case SIOCSIFDSTADDR:
  2980. case SIOCGIFNETMASK:
  2981. case SIOCSIFNETMASK:
  2982. case SIOCSIFPFLAGS:
  2983. case SIOCGIFPFLAGS:
  2984. case SIOCGIFTXQLEN:
  2985. case SIOCSIFTXQLEN:
  2986. case SIOCBRADDIF:
  2987. case SIOCBRDELIF:
  2988. case SIOCGIFNAME:
  2989. case SIOCSIFNAME:
  2990. case SIOCGMIIPHY:
  2991. case SIOCGMIIREG:
  2992. case SIOCSMIIREG:
  2993. case SIOCBONDENSLAVE:
  2994. case SIOCBONDRELEASE:
  2995. case SIOCBONDSETHWADDR:
  2996. case SIOCBONDCHANGEACTIVE:
  2997. case SIOCSARP:
  2998. case SIOCGARP:
  2999. case SIOCDARP:
  3000. case SIOCOUTQ:
  3001. case SIOCOUTQNSD:
  3002. case SIOCATMARK:
  3003. return sock_do_ioctl(net, sock, cmd, arg);
  3004. }
  3005. return -ENOIOCTLCMD;
  3006. }
  3007. static long compat_sock_ioctl(struct file *file, unsigned int cmd,
  3008. unsigned long arg)
  3009. {
  3010. struct socket *sock = file->private_data;
  3011. const struct proto_ops *ops = READ_ONCE(sock->ops);
  3012. int ret = -ENOIOCTLCMD;
  3013. struct sock *sk;
  3014. struct net *net;
  3015. sk = sock->sk;
  3016. net = sock_net(sk);
  3017. if (ops->compat_ioctl)
  3018. ret = ops->compat_ioctl(sock, cmd, arg);
  3019. if (ret == -ENOIOCTLCMD &&
  3020. (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
  3021. ret = compat_wext_handle_ioctl(net, cmd, arg);
  3022. if (ret == -ENOIOCTLCMD)
  3023. ret = compat_sock_ioctl_trans(file, sock, cmd, arg);
  3024. return ret;
  3025. }
  3026. #endif
  3027. /**
  3028. * kernel_bind - bind an address to a socket (kernel space)
  3029. * @sock: socket
  3030. * @addr: address
  3031. * @addrlen: length of address
  3032. *
  3033. * Returns 0 or an error.
  3034. */
  3035. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
  3036. {
  3037. struct sockaddr_storage address;
  3038. memcpy(&address, addr, addrlen);
  3039. return READ_ONCE(sock->ops)->bind(sock, (struct sockaddr *)&address,
  3040. addrlen);
  3041. }
  3042. EXPORT_SYMBOL(kernel_bind);
  3043. /**
  3044. * kernel_listen - move socket to listening state (kernel space)
  3045. * @sock: socket
  3046. * @backlog: pending connections queue size
  3047. *
  3048. * Returns 0 or an error.
  3049. */
  3050. int kernel_listen(struct socket *sock, int backlog)
  3051. {
  3052. return READ_ONCE(sock->ops)->listen(sock, backlog);
  3053. }
  3054. EXPORT_SYMBOL(kernel_listen);
  3055. /**
  3056. * kernel_accept - accept a connection (kernel space)
  3057. * @sock: listening socket
  3058. * @newsock: new connected socket
  3059. * @flags: flags
  3060. *
  3061. * @flags must be SOCK_CLOEXEC, SOCK_NONBLOCK or 0.
  3062. * If it fails, @newsock is guaranteed to be %NULL.
  3063. * Returns 0 or an error.
  3064. */
  3065. int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
  3066. {
  3067. struct sock *sk = sock->sk;
  3068. const struct proto_ops *ops = READ_ONCE(sock->ops);
  3069. struct proto_accept_arg arg = {
  3070. .flags = flags,
  3071. .kern = true,
  3072. };
  3073. int err;
  3074. err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
  3075. newsock);
  3076. if (err < 0)
  3077. goto done;
  3078. err = ops->accept(sock, *newsock, &arg);
  3079. if (err < 0) {
  3080. sock_release(*newsock);
  3081. *newsock = NULL;
  3082. goto done;
  3083. }
  3084. (*newsock)->ops = ops;
  3085. __module_get(ops->owner);
  3086. done:
  3087. return err;
  3088. }
  3089. EXPORT_SYMBOL(kernel_accept);
  3090. /**
  3091. * kernel_connect - connect a socket (kernel space)
  3092. * @sock: socket
  3093. * @addr: address
  3094. * @addrlen: address length
  3095. * @flags: flags (O_NONBLOCK, ...)
  3096. *
  3097. * For datagram sockets, @addr is the address to which datagrams are sent
  3098. * by default, and the only address from which datagrams are received.
  3099. * For stream sockets, attempts to connect to @addr.
  3100. * Returns 0 or an error code.
  3101. */
  3102. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  3103. int flags)
  3104. {
  3105. struct sockaddr_storage address;
  3106. memcpy(&address, addr, addrlen);
  3107. return READ_ONCE(sock->ops)->connect(sock, (struct sockaddr *)&address,
  3108. addrlen, flags);
  3109. }
  3110. EXPORT_SYMBOL(kernel_connect);
  3111. /**
  3112. * kernel_getsockname - get the address which the socket is bound (kernel space)
  3113. * @sock: socket
  3114. * @addr: address holder
  3115. *
  3116. * Fills the @addr pointer with the address which the socket is bound.
  3117. * Returns the length of the address in bytes or an error code.
  3118. */
  3119. int kernel_getsockname(struct socket *sock, struct sockaddr *addr)
  3120. {
  3121. return READ_ONCE(sock->ops)->getname(sock, addr, 0);
  3122. }
  3123. EXPORT_SYMBOL(kernel_getsockname);
  3124. /**
  3125. * kernel_getpeername - get the address which the socket is connected (kernel space)
  3126. * @sock: socket
  3127. * @addr: address holder
  3128. *
  3129. * Fills the @addr pointer with the address which the socket is connected.
  3130. * Returns the length of the address in bytes or an error code.
  3131. */
  3132. int kernel_getpeername(struct socket *sock, struct sockaddr *addr)
  3133. {
  3134. return READ_ONCE(sock->ops)->getname(sock, addr, 1);
  3135. }
  3136. EXPORT_SYMBOL(kernel_getpeername);
  3137. /**
  3138. * kernel_sock_shutdown - shut down part of a full-duplex connection (kernel space)
  3139. * @sock: socket
  3140. * @how: connection part
  3141. *
  3142. * Returns 0 or an error.
  3143. */
  3144. int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
  3145. {
  3146. return READ_ONCE(sock->ops)->shutdown(sock, how);
  3147. }
  3148. EXPORT_SYMBOL(kernel_sock_shutdown);
  3149. /**
  3150. * kernel_sock_ip_overhead - returns the IP overhead imposed by a socket
  3151. * @sk: socket
  3152. *
  3153. * This routine returns the IP overhead imposed by a socket i.e.
  3154. * the length of the underlying IP header, depending on whether
  3155. * this is an IPv4 or IPv6 socket and the length from IP options turned
  3156. * on at the socket. Assumes that the caller has a lock on the socket.
  3157. */
  3158. u32 kernel_sock_ip_overhead(struct sock *sk)
  3159. {
  3160. struct inet_sock *inet;
  3161. struct ip_options_rcu *opt;
  3162. u32 overhead = 0;
  3163. #if IS_ENABLED(CONFIG_IPV6)
  3164. struct ipv6_pinfo *np;
  3165. struct ipv6_txoptions *optv6 = NULL;
  3166. #endif /* IS_ENABLED(CONFIG_IPV6) */
  3167. if (!sk)
  3168. return overhead;
  3169. switch (sk->sk_family) {
  3170. case AF_INET:
  3171. inet = inet_sk(sk);
  3172. overhead += sizeof(struct iphdr);
  3173. opt = rcu_dereference_protected(inet->inet_opt,
  3174. sock_owned_by_user(sk));
  3175. if (opt)
  3176. overhead += opt->opt.optlen;
  3177. return overhead;
  3178. #if IS_ENABLED(CONFIG_IPV6)
  3179. case AF_INET6:
  3180. np = inet6_sk(sk);
  3181. overhead += sizeof(struct ipv6hdr);
  3182. if (np)
  3183. optv6 = rcu_dereference_protected(np->opt,
  3184. sock_owned_by_user(sk));
  3185. if (optv6)
  3186. overhead += (optv6->opt_flen + optv6->opt_nflen);
  3187. return overhead;
  3188. #endif /* IS_ENABLED(CONFIG_IPV6) */
  3189. default: /* Returns 0 overhead if the socket is not ipv4 or ipv6 */
  3190. return overhead;
  3191. }
  3192. }
  3193. EXPORT_SYMBOL(kernel_sock_ip_overhead);