socket.c 81 KB

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