socket.c 76 KB

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