socket.c 82 KB

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