socket.c 79 KB

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