socket.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415
  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/thread_info.h>
  72. #include <linux/wanrouter.h>
  73. #include <linux/if_bridge.h>
  74. #include <linux/if_frad.h>
  75. #include <linux/if_vlan.h>
  76. #include <linux/init.h>
  77. #include <linux/poll.h>
  78. #include <linux/cache.h>
  79. #include <linux/module.h>
  80. #include <linux/highmem.h>
  81. #include <linux/mount.h>
  82. #include <linux/security.h>
  83. #include <linux/syscalls.h>
  84. #include <linux/compat.h>
  85. #include <linux/kmod.h>
  86. #include <linux/audit.h>
  87. #include <linux/wireless.h>
  88. #include <linux/nsproxy.h>
  89. #include <asm/uaccess.h>
  90. #include <asm/unistd.h>
  91. #include <net/compat.h>
  92. #include <net/wext.h>
  93. #include <net/sock.h>
  94. #include <linux/netfilter.h>
  95. static int sock_no_open(struct inode *irrelevant, struct file *dontcare);
  96. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  97. unsigned long nr_segs, loff_t pos);
  98. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  99. unsigned long nr_segs, loff_t pos);
  100. static int sock_mmap(struct file *file, struct vm_area_struct *vma);
  101. static int sock_close(struct inode *inode, struct file *file);
  102. static unsigned int sock_poll(struct file *file,
  103. struct poll_table_struct *wait);
  104. static long sock_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  105. #ifdef CONFIG_COMPAT
  106. static long compat_sock_ioctl(struct file *file,
  107. unsigned int cmd, unsigned long arg);
  108. #endif
  109. static int sock_fasync(int fd, struct file *filp, int on);
  110. static ssize_t sock_sendpage(struct file *file, struct page *page,
  111. int offset, size_t size, loff_t *ppos, int more);
  112. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  113. struct pipe_inode_info *pipe, size_t len,
  114. unsigned int flags);
  115. /*
  116. * Socket files have a set of 'special' operations as well as the generic file ones. These don't appear
  117. * in the operation structures but are done directly via the socketcall() multiplexor.
  118. */
  119. static const struct file_operations socket_file_ops = {
  120. .owner = THIS_MODULE,
  121. .llseek = no_llseek,
  122. .aio_read = sock_aio_read,
  123. .aio_write = sock_aio_write,
  124. .poll = sock_poll,
  125. .unlocked_ioctl = sock_ioctl,
  126. #ifdef CONFIG_COMPAT
  127. .compat_ioctl = compat_sock_ioctl,
  128. #endif
  129. .mmap = sock_mmap,
  130. .open = sock_no_open, /* special open code to disallow open via /proc */
  131. .release = sock_close,
  132. .fasync = sock_fasync,
  133. .sendpage = sock_sendpage,
  134. .splice_write = generic_splice_sendpage,
  135. .splice_read = sock_splice_read,
  136. };
  137. /*
  138. * The protocol list. Each protocol is registered in here.
  139. */
  140. static DEFINE_SPINLOCK(net_family_lock);
  141. static const struct net_proto_family *net_families[NPROTO] __read_mostly;
  142. /*
  143. * Statistics counters of the socket lists
  144. */
  145. static DEFINE_PER_CPU(int, sockets_in_use) = 0;
  146. /*
  147. * Support routines.
  148. * Move socket addresses back and forth across the kernel/user
  149. * divide and look after the messy bits.
  150. */
  151. #define MAX_SOCK_ADDR 128 /* 108 for Unix domain -
  152. 16 for IP, 16 for IPX,
  153. 24 for IPv6,
  154. about 80 for AX.25
  155. must be at least one bigger than
  156. the AF_UNIX size (see net/unix/af_unix.c
  157. :unix_mkname()).
  158. */
  159. /**
  160. * move_addr_to_kernel - copy a socket address into kernel space
  161. * @uaddr: Address in user space
  162. * @kaddr: Address in kernel space
  163. * @ulen: Length in user space
  164. *
  165. * The address is copied into kernel space. If the provided address is
  166. * too long an error code of -EINVAL is returned. If the copy gives
  167. * invalid addresses -EFAULT is returned. On a success 0 is returned.
  168. */
  169. int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr *kaddr)
  170. {
  171. if (ulen < 0 || ulen > sizeof(struct sockaddr_storage))
  172. return -EINVAL;
  173. if (ulen == 0)
  174. return 0;
  175. if (copy_from_user(kaddr, uaddr, ulen))
  176. return -EFAULT;
  177. return audit_sockaddr(ulen, kaddr);
  178. }
  179. /**
  180. * move_addr_to_user - copy an address to user space
  181. * @kaddr: kernel space address
  182. * @klen: length of address in kernel
  183. * @uaddr: user space address
  184. * @ulen: pointer to user length field
  185. *
  186. * The value pointed to by ulen on entry is the buffer length available.
  187. * This is overwritten with the buffer space used. -EINVAL is returned
  188. * if an overlong buffer is specified or a negative buffer size. -EFAULT
  189. * is returned if either the buffer or the length field are not
  190. * accessible.
  191. * After copying the data up to the limit the user specifies, the true
  192. * length of the data is written over the length limit the user
  193. * specified. Zero is returned for a success.
  194. */
  195. int move_addr_to_user(struct sockaddr *kaddr, int klen, void __user *uaddr,
  196. int __user *ulen)
  197. {
  198. int err;
  199. int len;
  200. err = get_user(len, ulen);
  201. if (err)
  202. return err;
  203. if (len > klen)
  204. len = klen;
  205. if (len < 0 || len > sizeof(struct sockaddr_storage))
  206. return -EINVAL;
  207. if (len) {
  208. if (audit_sockaddr(klen, kaddr))
  209. return -ENOMEM;
  210. if (copy_to_user(uaddr, kaddr, len))
  211. return -EFAULT;
  212. }
  213. /*
  214. * "fromlen shall refer to the value before truncation.."
  215. * 1003.1g
  216. */
  217. return __put_user(klen, ulen);
  218. }
  219. #define SOCKFS_MAGIC 0x534F434B
  220. static struct kmem_cache *sock_inode_cachep __read_mostly;
  221. static struct inode *sock_alloc_inode(struct super_block *sb)
  222. {
  223. struct socket_alloc *ei;
  224. ei = kmem_cache_alloc(sock_inode_cachep, GFP_KERNEL);
  225. if (!ei)
  226. return NULL;
  227. init_waitqueue_head(&ei->socket.wait);
  228. ei->socket.fasync_list = NULL;
  229. ei->socket.state = SS_UNCONNECTED;
  230. ei->socket.flags = 0;
  231. ei->socket.ops = NULL;
  232. ei->socket.sk = NULL;
  233. ei->socket.file = NULL;
  234. return &ei->vfs_inode;
  235. }
  236. static void sock_destroy_inode(struct inode *inode)
  237. {
  238. kmem_cache_free(sock_inode_cachep,
  239. container_of(inode, struct socket_alloc, vfs_inode));
  240. }
  241. static void init_once(void *foo)
  242. {
  243. struct socket_alloc *ei = (struct socket_alloc *)foo;
  244. inode_init_once(&ei->vfs_inode);
  245. }
  246. static int init_inodecache(void)
  247. {
  248. sock_inode_cachep = kmem_cache_create("sock_inode_cache",
  249. sizeof(struct socket_alloc),
  250. 0,
  251. (SLAB_HWCACHE_ALIGN |
  252. SLAB_RECLAIM_ACCOUNT |
  253. SLAB_MEM_SPREAD),
  254. init_once);
  255. if (sock_inode_cachep == NULL)
  256. return -ENOMEM;
  257. return 0;
  258. }
  259. static struct super_operations sockfs_ops = {
  260. .alloc_inode = sock_alloc_inode,
  261. .destroy_inode =sock_destroy_inode,
  262. .statfs = simple_statfs,
  263. };
  264. static int sockfs_get_sb(struct file_system_type *fs_type,
  265. int flags, const char *dev_name, void *data,
  266. struct vfsmount *mnt)
  267. {
  268. return get_sb_pseudo(fs_type, "socket:", &sockfs_ops, SOCKFS_MAGIC,
  269. mnt);
  270. }
  271. static struct vfsmount *sock_mnt __read_mostly;
  272. static struct file_system_type sock_fs_type = {
  273. .name = "sockfs",
  274. .get_sb = sockfs_get_sb,
  275. .kill_sb = kill_anon_super,
  276. };
  277. static int sockfs_delete_dentry(struct dentry *dentry)
  278. {
  279. /*
  280. * At creation time, we pretended this dentry was hashed
  281. * (by clearing DCACHE_UNHASHED bit in d_flags)
  282. * At delete time, we restore the truth : not hashed.
  283. * (so that dput() can proceed correctly)
  284. */
  285. dentry->d_flags |= DCACHE_UNHASHED;
  286. return 0;
  287. }
  288. /*
  289. * sockfs_dname() is called from d_path().
  290. */
  291. static char *sockfs_dname(struct dentry *dentry, char *buffer, int buflen)
  292. {
  293. return dynamic_dname(dentry, buffer, buflen, "socket:[%lu]",
  294. dentry->d_inode->i_ino);
  295. }
  296. static struct dentry_operations sockfs_dentry_operations = {
  297. .d_delete = sockfs_delete_dentry,
  298. .d_dname = sockfs_dname,
  299. };
  300. /*
  301. * Obtains the first available file descriptor and sets it up for use.
  302. *
  303. * These functions create file structures and maps them to fd space
  304. * of the current process. On success it returns file descriptor
  305. * and file struct implicitly stored in sock->file.
  306. * Note that another thread may close file descriptor before we return
  307. * from this function. We use the fact that now we do not refer
  308. * to socket after mapping. If one day we will need it, this
  309. * function will increment ref. count on file by 1.
  310. *
  311. * In any case returned fd MAY BE not valid!
  312. * This race condition is unavoidable
  313. * with shared fd spaces, we cannot solve it inside kernel,
  314. * but we take care of internal coherence yet.
  315. */
  316. static int sock_alloc_fd(struct file **filep, int flags)
  317. {
  318. int fd;
  319. fd = get_unused_fd_flags(flags);
  320. if (likely(fd >= 0)) {
  321. struct file *file = get_empty_filp();
  322. *filep = file;
  323. if (unlikely(!file)) {
  324. put_unused_fd(fd);
  325. return -ENFILE;
  326. }
  327. } else
  328. *filep = NULL;
  329. return fd;
  330. }
  331. static int sock_attach_fd(struct socket *sock, struct file *file, int flags)
  332. {
  333. struct dentry *dentry;
  334. struct qstr name = { .name = "" };
  335. dentry = d_alloc(sock_mnt->mnt_sb->s_root, &name);
  336. if (unlikely(!dentry))
  337. return -ENOMEM;
  338. dentry->d_op = &sockfs_dentry_operations;
  339. /*
  340. * We dont want to push this dentry into global dentry hash table.
  341. * We pretend dentry is already hashed, by unsetting DCACHE_UNHASHED
  342. * This permits a working /proc/$pid/fd/XXX on sockets
  343. */
  344. dentry->d_flags &= ~DCACHE_UNHASHED;
  345. d_instantiate(dentry, SOCK_INODE(sock));
  346. sock->file = file;
  347. init_file(file, sock_mnt, dentry, FMODE_READ | FMODE_WRITE,
  348. &socket_file_ops);
  349. SOCK_INODE(sock)->i_fop = &socket_file_ops;
  350. file->f_flags = O_RDWR | (flags & O_NONBLOCK);
  351. file->f_pos = 0;
  352. file->private_data = sock;
  353. return 0;
  354. }
  355. int sock_map_fd(struct socket *sock, int flags)
  356. {
  357. struct file *newfile;
  358. int fd = sock_alloc_fd(&newfile, flags);
  359. if (likely(fd >= 0)) {
  360. int err = sock_attach_fd(sock, newfile, flags);
  361. if (unlikely(err < 0)) {
  362. put_filp(newfile);
  363. put_unused_fd(fd);
  364. return err;
  365. }
  366. fd_install(fd, newfile);
  367. }
  368. return fd;
  369. }
  370. static struct socket *sock_from_file(struct file *file, int *err)
  371. {
  372. if (file->f_op == &socket_file_ops)
  373. return file->private_data; /* set in sock_map_fd */
  374. *err = -ENOTSOCK;
  375. return NULL;
  376. }
  377. /**
  378. * sockfd_lookup - Go from a file number to its socket slot
  379. * @fd: file handle
  380. * @err: pointer to an error code return
  381. *
  382. * The file handle passed in is locked and the socket it is bound
  383. * too is returned. If an error occurs the err pointer is overwritten
  384. * with a negative errno code and NULL is returned. The function checks
  385. * for both invalid handles and passing a handle which is not a socket.
  386. *
  387. * On a success the socket object pointer is returned.
  388. */
  389. struct socket *sockfd_lookup(int fd, int *err)
  390. {
  391. struct file *file;
  392. struct socket *sock;
  393. file = fget(fd);
  394. if (!file) {
  395. *err = -EBADF;
  396. return NULL;
  397. }
  398. sock = sock_from_file(file, err);
  399. if (!sock)
  400. fput(file);
  401. return sock;
  402. }
  403. static struct socket *sockfd_lookup_light(int fd, int *err, int *fput_needed)
  404. {
  405. struct file *file;
  406. struct socket *sock;
  407. *err = -EBADF;
  408. file = fget_light(fd, fput_needed);
  409. if (file) {
  410. sock = sock_from_file(file, err);
  411. if (sock)
  412. return sock;
  413. fput_light(file, *fput_needed);
  414. }
  415. return NULL;
  416. }
  417. /**
  418. * sock_alloc - allocate a socket
  419. *
  420. * Allocate a new inode and socket object. The two are bound together
  421. * and initialised. The socket is then returned. If we are out of inodes
  422. * NULL is returned.
  423. */
  424. static struct socket *sock_alloc(void)
  425. {
  426. struct inode *inode;
  427. struct socket *sock;
  428. inode = new_inode(sock_mnt->mnt_sb);
  429. if (!inode)
  430. return NULL;
  431. sock = SOCKET_I(inode);
  432. inode->i_mode = S_IFSOCK | S_IRWXUGO;
  433. inode->i_uid = current->fsuid;
  434. inode->i_gid = current->fsgid;
  435. get_cpu_var(sockets_in_use)++;
  436. put_cpu_var(sockets_in_use);
  437. return sock;
  438. }
  439. /*
  440. * In theory you can't get an open on this inode, but /proc provides
  441. * a back door. Remember to keep it shut otherwise you'll let the
  442. * creepy crawlies in.
  443. */
  444. static int sock_no_open(struct inode *irrelevant, struct file *dontcare)
  445. {
  446. return -ENXIO;
  447. }
  448. const struct file_operations bad_sock_fops = {
  449. .owner = THIS_MODULE,
  450. .open = sock_no_open,
  451. };
  452. /**
  453. * sock_release - close a socket
  454. * @sock: socket to close
  455. *
  456. * The socket is released from the protocol stack if it has a release
  457. * callback, and the inode is then released if the socket is bound to
  458. * an inode not a file.
  459. */
  460. void sock_release(struct socket *sock)
  461. {
  462. if (sock->ops) {
  463. struct module *owner = sock->ops->owner;
  464. sock->ops->release(sock);
  465. sock->ops = NULL;
  466. module_put(owner);
  467. }
  468. if (sock->fasync_list)
  469. printk(KERN_ERR "sock_release: fasync list not empty!\n");
  470. get_cpu_var(sockets_in_use)--;
  471. put_cpu_var(sockets_in_use);
  472. if (!sock->file) {
  473. iput(SOCK_INODE(sock));
  474. return;
  475. }
  476. sock->file = NULL;
  477. }
  478. static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
  479. struct msghdr *msg, size_t size)
  480. {
  481. struct sock_iocb *si = kiocb_to_siocb(iocb);
  482. int err;
  483. si->sock = sock;
  484. si->scm = NULL;
  485. si->msg = msg;
  486. si->size = size;
  487. err = security_socket_sendmsg(sock, msg, size);
  488. if (err)
  489. return err;
  490. return sock->ops->sendmsg(iocb, sock, msg, size);
  491. }
  492. int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  493. {
  494. struct kiocb iocb;
  495. struct sock_iocb siocb;
  496. int ret;
  497. init_sync_kiocb(&iocb, NULL);
  498. iocb.private = &siocb;
  499. ret = __sock_sendmsg(&iocb, sock, msg, size);
  500. if (-EIOCBQUEUED == ret)
  501. ret = wait_on_sync_kiocb(&iocb);
  502. return ret;
  503. }
  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. /*
  521. * called from sock_recv_timestamp() if sock_flag(sk, SOCK_RCVTSTAMP)
  522. */
  523. void __sock_recv_timestamp(struct msghdr *msg, struct sock *sk,
  524. struct sk_buff *skb)
  525. {
  526. ktime_t kt = skb->tstamp;
  527. if (!sock_flag(sk, SOCK_RCVTSTAMPNS)) {
  528. struct timeval tv;
  529. /* Race occurred between timestamp enabling and packet
  530. receiving. Fill in the current time for now. */
  531. if (kt.tv64 == 0)
  532. kt = ktime_get_real();
  533. skb->tstamp = kt;
  534. tv = ktime_to_timeval(kt);
  535. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMP, sizeof(tv), &tv);
  536. } else {
  537. struct timespec ts;
  538. /* Race occurred between timestamp enabling and packet
  539. receiving. Fill in the current time for now. */
  540. if (kt.tv64 == 0)
  541. kt = ktime_get_real();
  542. skb->tstamp = kt;
  543. ts = ktime_to_timespec(kt);
  544. put_cmsg(msg, SOL_SOCKET, SCM_TIMESTAMPNS, sizeof(ts), &ts);
  545. }
  546. }
  547. EXPORT_SYMBOL_GPL(__sock_recv_timestamp);
  548. static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock,
  549. struct msghdr *msg, size_t size, int flags)
  550. {
  551. int err;
  552. struct sock_iocb *si = kiocb_to_siocb(iocb);
  553. si->sock = sock;
  554. si->scm = NULL;
  555. si->msg = msg;
  556. si->size = size;
  557. si->flags = flags;
  558. err = security_socket_recvmsg(sock, msg, size, flags);
  559. if (err)
  560. return err;
  561. return sock->ops->recvmsg(iocb, sock, msg, size, flags);
  562. }
  563. int sock_recvmsg(struct socket *sock, struct msghdr *msg,
  564. size_t size, int flags)
  565. {
  566. struct kiocb iocb;
  567. struct sock_iocb siocb;
  568. int ret;
  569. init_sync_kiocb(&iocb, NULL);
  570. iocb.private = &siocb;
  571. ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
  572. if (-EIOCBQUEUED == ret)
  573. ret = wait_on_sync_kiocb(&iocb);
  574. return ret;
  575. }
  576. int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
  577. struct kvec *vec, size_t num, size_t size, int flags)
  578. {
  579. mm_segment_t oldfs = get_fs();
  580. int result;
  581. set_fs(KERNEL_DS);
  582. /*
  583. * the following is safe, since for compiler definitions of kvec and
  584. * iovec are identical, yielding the same in-core layout and alignment
  585. */
  586. msg->msg_iov = (struct iovec *)vec, msg->msg_iovlen = num;
  587. result = sock_recvmsg(sock, msg, size, flags);
  588. set_fs(oldfs);
  589. return result;
  590. }
  591. static void sock_aio_dtor(struct kiocb *iocb)
  592. {
  593. kfree(iocb->private);
  594. }
  595. static ssize_t sock_sendpage(struct file *file, struct page *page,
  596. int offset, size_t size, loff_t *ppos, int more)
  597. {
  598. struct socket *sock;
  599. int flags;
  600. sock = file->private_data;
  601. flags = !(file->f_flags & O_NONBLOCK) ? 0 : MSG_DONTWAIT;
  602. if (more)
  603. flags |= MSG_MORE;
  604. return sock->ops->sendpage(sock, page, offset, size, flags);
  605. }
  606. static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
  607. struct pipe_inode_info *pipe, size_t len,
  608. unsigned int flags)
  609. {
  610. struct socket *sock = file->private_data;
  611. if (unlikely(!sock->ops->splice_read))
  612. return -EINVAL;
  613. return sock->ops->splice_read(sock, ppos, pipe, len, flags);
  614. }
  615. static struct sock_iocb *alloc_sock_iocb(struct kiocb *iocb,
  616. struct sock_iocb *siocb)
  617. {
  618. if (!is_sync_kiocb(iocb)) {
  619. siocb = kmalloc(sizeof(*siocb), GFP_KERNEL);
  620. if (!siocb)
  621. return NULL;
  622. iocb->ki_dtor = sock_aio_dtor;
  623. }
  624. siocb->kiocb = iocb;
  625. iocb->private = siocb;
  626. return siocb;
  627. }
  628. static ssize_t do_sock_read(struct msghdr *msg, struct kiocb *iocb,
  629. struct file *file, const struct iovec *iov,
  630. unsigned long nr_segs)
  631. {
  632. struct socket *sock = file->private_data;
  633. size_t size = 0;
  634. int i;
  635. for (i = 0; i < nr_segs; i++)
  636. size += iov[i].iov_len;
  637. msg->msg_name = NULL;
  638. msg->msg_namelen = 0;
  639. msg->msg_control = NULL;
  640. msg->msg_controllen = 0;
  641. msg->msg_iov = (struct iovec *)iov;
  642. msg->msg_iovlen = nr_segs;
  643. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  644. return __sock_recvmsg(iocb, sock, msg, size, msg->msg_flags);
  645. }
  646. static ssize_t sock_aio_read(struct kiocb *iocb, const struct iovec *iov,
  647. unsigned long nr_segs, loff_t pos)
  648. {
  649. struct sock_iocb siocb, *x;
  650. if (pos != 0)
  651. return -ESPIPE;
  652. if (iocb->ki_left == 0) /* Match SYS5 behaviour */
  653. return 0;
  654. x = alloc_sock_iocb(iocb, &siocb);
  655. if (!x)
  656. return -ENOMEM;
  657. return do_sock_read(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  658. }
  659. static ssize_t do_sock_write(struct msghdr *msg, struct kiocb *iocb,
  660. struct file *file, const struct iovec *iov,
  661. unsigned long nr_segs)
  662. {
  663. struct socket *sock = file->private_data;
  664. size_t size = 0;
  665. int i;
  666. for (i = 0; i < nr_segs; i++)
  667. size += iov[i].iov_len;
  668. msg->msg_name = NULL;
  669. msg->msg_namelen = 0;
  670. msg->msg_control = NULL;
  671. msg->msg_controllen = 0;
  672. msg->msg_iov = (struct iovec *)iov;
  673. msg->msg_iovlen = nr_segs;
  674. msg->msg_flags = (file->f_flags & O_NONBLOCK) ? MSG_DONTWAIT : 0;
  675. if (sock->type == SOCK_SEQPACKET)
  676. msg->msg_flags |= MSG_EOR;
  677. return __sock_sendmsg(iocb, sock, msg, size);
  678. }
  679. static ssize_t sock_aio_write(struct kiocb *iocb, const struct iovec *iov,
  680. unsigned long nr_segs, loff_t pos)
  681. {
  682. struct sock_iocb siocb, *x;
  683. if (pos != 0)
  684. return -ESPIPE;
  685. x = alloc_sock_iocb(iocb, &siocb);
  686. if (!x)
  687. return -ENOMEM;
  688. return do_sock_write(&x->async_msg, iocb, iocb->ki_filp, iov, nr_segs);
  689. }
  690. /*
  691. * Atomic setting of ioctl hooks to avoid race
  692. * with module unload.
  693. */
  694. static DEFINE_MUTEX(br_ioctl_mutex);
  695. static int (*br_ioctl_hook) (struct net *, unsigned int cmd, void __user *arg) = NULL;
  696. void brioctl_set(int (*hook) (struct net *, unsigned int, void __user *))
  697. {
  698. mutex_lock(&br_ioctl_mutex);
  699. br_ioctl_hook = hook;
  700. mutex_unlock(&br_ioctl_mutex);
  701. }
  702. EXPORT_SYMBOL(brioctl_set);
  703. static DEFINE_MUTEX(vlan_ioctl_mutex);
  704. static int (*vlan_ioctl_hook) (struct net *, void __user *arg);
  705. void vlan_ioctl_set(int (*hook) (struct net *, void __user *))
  706. {
  707. mutex_lock(&vlan_ioctl_mutex);
  708. vlan_ioctl_hook = hook;
  709. mutex_unlock(&vlan_ioctl_mutex);
  710. }
  711. EXPORT_SYMBOL(vlan_ioctl_set);
  712. static DEFINE_MUTEX(dlci_ioctl_mutex);
  713. static int (*dlci_ioctl_hook) (unsigned int, void __user *);
  714. void dlci_ioctl_set(int (*hook) (unsigned int, void __user *))
  715. {
  716. mutex_lock(&dlci_ioctl_mutex);
  717. dlci_ioctl_hook = hook;
  718. mutex_unlock(&dlci_ioctl_mutex);
  719. }
  720. EXPORT_SYMBOL(dlci_ioctl_set);
  721. /*
  722. * With an ioctl, arg may well be a user mode pointer, but we don't know
  723. * what to do with it - that's up to the protocol still.
  724. */
  725. static long sock_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  726. {
  727. struct socket *sock;
  728. struct sock *sk;
  729. void __user *argp = (void __user *)arg;
  730. int pid, err;
  731. struct net *net;
  732. sock = file->private_data;
  733. sk = sock->sk;
  734. net = sock_net(sk);
  735. if (cmd >= SIOCDEVPRIVATE && cmd <= (SIOCDEVPRIVATE + 15)) {
  736. err = dev_ioctl(net, cmd, argp);
  737. } else
  738. #ifdef CONFIG_WIRELESS_EXT
  739. if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
  740. err = dev_ioctl(net, cmd, argp);
  741. } else
  742. #endif /* CONFIG_WIRELESS_EXT */
  743. switch (cmd) {
  744. case FIOSETOWN:
  745. case SIOCSPGRP:
  746. err = -EFAULT;
  747. if (get_user(pid, (int __user *)argp))
  748. break;
  749. err = f_setown(sock->file, pid, 1);
  750. break;
  751. case FIOGETOWN:
  752. case SIOCGPGRP:
  753. err = put_user(f_getown(sock->file),
  754. (int __user *)argp);
  755. break;
  756. case SIOCGIFBR:
  757. case SIOCSIFBR:
  758. case SIOCBRADDBR:
  759. case SIOCBRDELBR:
  760. err = -ENOPKG;
  761. if (!br_ioctl_hook)
  762. request_module("bridge");
  763. mutex_lock(&br_ioctl_mutex);
  764. if (br_ioctl_hook)
  765. err = br_ioctl_hook(net, cmd, argp);
  766. mutex_unlock(&br_ioctl_mutex);
  767. break;
  768. case SIOCGIFVLAN:
  769. case SIOCSIFVLAN:
  770. err = -ENOPKG;
  771. if (!vlan_ioctl_hook)
  772. request_module("8021q");
  773. mutex_lock(&vlan_ioctl_mutex);
  774. if (vlan_ioctl_hook)
  775. err = vlan_ioctl_hook(net, argp);
  776. mutex_unlock(&vlan_ioctl_mutex);
  777. break;
  778. case SIOCADDDLCI:
  779. case SIOCDELDLCI:
  780. err = -ENOPKG;
  781. if (!dlci_ioctl_hook)
  782. request_module("dlci");
  783. mutex_lock(&dlci_ioctl_mutex);
  784. if (dlci_ioctl_hook)
  785. err = dlci_ioctl_hook(cmd, argp);
  786. mutex_unlock(&dlci_ioctl_mutex);
  787. break;
  788. default:
  789. err = sock->ops->ioctl(sock, cmd, arg);
  790. /*
  791. * If this ioctl is unknown try to hand it down
  792. * to the NIC driver.
  793. */
  794. if (err == -ENOIOCTLCMD)
  795. err = dev_ioctl(net, cmd, argp);
  796. break;
  797. }
  798. return err;
  799. }
  800. int sock_create_lite(int family, int type, int protocol, struct socket **res)
  801. {
  802. int err;
  803. struct socket *sock = NULL;
  804. err = security_socket_create(family, type, protocol, 1);
  805. if (err)
  806. goto out;
  807. sock = sock_alloc();
  808. if (!sock) {
  809. err = -ENOMEM;
  810. goto out;
  811. }
  812. sock->type = type;
  813. err = security_socket_post_create(sock, family, type, protocol, 1);
  814. if (err)
  815. goto out_release;
  816. out:
  817. *res = sock;
  818. return err;
  819. out_release:
  820. sock_release(sock);
  821. sock = NULL;
  822. goto out;
  823. }
  824. /* No kernel lock held - perfect */
  825. static unsigned int sock_poll(struct file *file, poll_table *wait)
  826. {
  827. struct socket *sock;
  828. /*
  829. * We can't return errors to poll, so it's either yes or no.
  830. */
  831. sock = file->private_data;
  832. return sock->ops->poll(file, sock, wait);
  833. }
  834. static int sock_mmap(struct file *file, struct vm_area_struct *vma)
  835. {
  836. struct socket *sock = file->private_data;
  837. return sock->ops->mmap(file, sock, vma);
  838. }
  839. static int sock_close(struct inode *inode, struct file *filp)
  840. {
  841. /*
  842. * It was possible the inode is NULL we were
  843. * closing an unfinished socket.
  844. */
  845. if (!inode) {
  846. printk(KERN_DEBUG "sock_close: NULL inode\n");
  847. return 0;
  848. }
  849. sock_release(SOCKET_I(inode));
  850. return 0;
  851. }
  852. /*
  853. * Update the socket async list
  854. *
  855. * Fasync_list locking strategy.
  856. *
  857. * 1. fasync_list is modified only under process context socket lock
  858. * i.e. under semaphore.
  859. * 2. fasync_list is used under read_lock(&sk->sk_callback_lock)
  860. * or under socket lock.
  861. * 3. fasync_list can be used from softirq context, so that
  862. * modification under socket lock have to be enhanced with
  863. * write_lock_bh(&sk->sk_callback_lock).
  864. * --ANK (990710)
  865. */
  866. static int sock_fasync(int fd, struct file *filp, int on)
  867. {
  868. struct fasync_struct *fa, *fna = NULL, **prev;
  869. struct socket *sock;
  870. struct sock *sk;
  871. if (on) {
  872. fna = kmalloc(sizeof(struct fasync_struct), GFP_KERNEL);
  873. if (fna == NULL)
  874. return -ENOMEM;
  875. }
  876. sock = filp->private_data;
  877. sk = sock->sk;
  878. if (sk == NULL) {
  879. kfree(fna);
  880. return -EINVAL;
  881. }
  882. lock_sock(sk);
  883. prev = &(sock->fasync_list);
  884. for (fa = *prev; fa != NULL; prev = &fa->fa_next, fa = *prev)
  885. if (fa->fa_file == filp)
  886. break;
  887. if (on) {
  888. if (fa != NULL) {
  889. write_lock_bh(&sk->sk_callback_lock);
  890. fa->fa_fd = fd;
  891. write_unlock_bh(&sk->sk_callback_lock);
  892. kfree(fna);
  893. goto out;
  894. }
  895. fna->fa_file = filp;
  896. fna->fa_fd = fd;
  897. fna->magic = FASYNC_MAGIC;
  898. fna->fa_next = sock->fasync_list;
  899. write_lock_bh(&sk->sk_callback_lock);
  900. sock->fasync_list = fna;
  901. write_unlock_bh(&sk->sk_callback_lock);
  902. } else {
  903. if (fa != NULL) {
  904. write_lock_bh(&sk->sk_callback_lock);
  905. *prev = fa->fa_next;
  906. write_unlock_bh(&sk->sk_callback_lock);
  907. kfree(fa);
  908. }
  909. }
  910. out:
  911. release_sock(sock->sk);
  912. return 0;
  913. }
  914. /* This function may be called only under socket lock or callback_lock */
  915. int sock_wake_async(struct socket *sock, int how, int band)
  916. {
  917. if (!sock || !sock->fasync_list)
  918. return -1;
  919. switch (how) {
  920. case SOCK_WAKE_WAITD:
  921. if (test_bit(SOCK_ASYNC_WAITDATA, &sock->flags))
  922. break;
  923. goto call_kill;
  924. case SOCK_WAKE_SPACE:
  925. if (!test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags))
  926. break;
  927. /* fall through */
  928. case SOCK_WAKE_IO:
  929. call_kill:
  930. __kill_fasync(sock->fasync_list, SIGIO, band);
  931. break;
  932. case SOCK_WAKE_URG:
  933. __kill_fasync(sock->fasync_list, SIGURG, band);
  934. }
  935. return 0;
  936. }
  937. static int __sock_create(struct net *net, int family, int type, int protocol,
  938. struct socket **res, int kern)
  939. {
  940. int err;
  941. struct socket *sock;
  942. const struct net_proto_family *pf;
  943. /*
  944. * Check protocol is in range
  945. */
  946. if (family < 0 || family >= NPROTO)
  947. return -EAFNOSUPPORT;
  948. if (type < 0 || type >= SOCK_MAX)
  949. return -EINVAL;
  950. /* Compatibility.
  951. This uglymoron is moved from INET layer to here to avoid
  952. deadlock in module load.
  953. */
  954. if (family == PF_INET && type == SOCK_PACKET) {
  955. static int warned;
  956. if (!warned) {
  957. warned = 1;
  958. printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n",
  959. current->comm);
  960. }
  961. family = PF_PACKET;
  962. }
  963. err = security_socket_create(family, type, protocol, kern);
  964. if (err)
  965. return err;
  966. /*
  967. * Allocate the socket and allow the family to set things up. if
  968. * the protocol is 0, the family is instructed to select an appropriate
  969. * default.
  970. */
  971. sock = sock_alloc();
  972. if (!sock) {
  973. if (net_ratelimit())
  974. printk(KERN_WARNING "socket: no more sockets\n");
  975. return -ENFILE; /* Not exactly a match, but its the
  976. closest posix thing */
  977. }
  978. sock->type = type;
  979. #ifdef CONFIG_MODULES
  980. /* Attempt to load a protocol module if the find failed.
  981. *
  982. * 12/09/1996 Marcin: But! this makes REALLY only sense, if the user
  983. * requested real, full-featured networking support upon configuration.
  984. * Otherwise module support will break!
  985. */
  986. if (net_families[family] == NULL)
  987. request_module("net-pf-%d", family);
  988. #endif
  989. rcu_read_lock();
  990. pf = rcu_dereference(net_families[family]);
  991. err = -EAFNOSUPPORT;
  992. if (!pf)
  993. goto out_release;
  994. /*
  995. * We will call the ->create function, that possibly is in a loadable
  996. * module, so we have to bump that loadable module refcnt first.
  997. */
  998. if (!try_module_get(pf->owner))
  999. goto out_release;
  1000. /* Now protected by module ref count */
  1001. rcu_read_unlock();
  1002. err = pf->create(net, sock, protocol);
  1003. if (err < 0)
  1004. goto out_module_put;
  1005. /*
  1006. * Now to bump the refcnt of the [loadable] module that owns this
  1007. * socket at sock_release time we decrement its refcnt.
  1008. */
  1009. if (!try_module_get(sock->ops->owner))
  1010. goto out_module_busy;
  1011. /*
  1012. * Now that we're done with the ->create function, the [loadable]
  1013. * module can have its refcnt decremented
  1014. */
  1015. module_put(pf->owner);
  1016. err = security_socket_post_create(sock, family, type, protocol, kern);
  1017. if (err)
  1018. goto out_sock_release;
  1019. *res = sock;
  1020. return 0;
  1021. out_module_busy:
  1022. err = -EAFNOSUPPORT;
  1023. out_module_put:
  1024. sock->ops = NULL;
  1025. module_put(pf->owner);
  1026. out_sock_release:
  1027. sock_release(sock);
  1028. return err;
  1029. out_release:
  1030. rcu_read_unlock();
  1031. goto out_sock_release;
  1032. }
  1033. int sock_create(int family, int type, int protocol, struct socket **res)
  1034. {
  1035. return __sock_create(current->nsproxy->net_ns, family, type, protocol, res, 0);
  1036. }
  1037. int sock_create_kern(int family, int type, int protocol, struct socket **res)
  1038. {
  1039. return __sock_create(&init_net, family, type, protocol, res, 1);
  1040. }
  1041. asmlinkage long sys_socket(int family, int type, int protocol)
  1042. {
  1043. int retval;
  1044. struct socket *sock;
  1045. int flags;
  1046. /* Check the SOCK_* constants for consistency. */
  1047. BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
  1048. BUILD_BUG_ON((SOCK_MAX | SOCK_TYPE_MASK) != SOCK_TYPE_MASK);
  1049. BUILD_BUG_ON(SOCK_CLOEXEC & SOCK_TYPE_MASK);
  1050. BUILD_BUG_ON(SOCK_NONBLOCK & SOCK_TYPE_MASK);
  1051. flags = type & ~SOCK_TYPE_MASK;
  1052. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1053. return -EINVAL;
  1054. type &= SOCK_TYPE_MASK;
  1055. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1056. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1057. retval = sock_create(family, type, protocol, &sock);
  1058. if (retval < 0)
  1059. goto out;
  1060. retval = sock_map_fd(sock, flags & (O_CLOEXEC | O_NONBLOCK));
  1061. if (retval < 0)
  1062. goto out_release;
  1063. out:
  1064. /* It may be already another descriptor 8) Not kernel problem. */
  1065. return retval;
  1066. out_release:
  1067. sock_release(sock);
  1068. return retval;
  1069. }
  1070. /*
  1071. * Create a pair of connected sockets.
  1072. */
  1073. asmlinkage long sys_socketpair(int family, int type, int protocol,
  1074. int __user *usockvec)
  1075. {
  1076. struct socket *sock1, *sock2;
  1077. int fd1, fd2, err;
  1078. struct file *newfile1, *newfile2;
  1079. int flags;
  1080. flags = type & ~SOCK_TYPE_MASK;
  1081. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1082. return -EINVAL;
  1083. type &= SOCK_TYPE_MASK;
  1084. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1085. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1086. /*
  1087. * Obtain the first socket and check if the underlying protocol
  1088. * supports the socketpair call.
  1089. */
  1090. err = sock_create(family, type, protocol, &sock1);
  1091. if (err < 0)
  1092. goto out;
  1093. err = sock_create(family, type, protocol, &sock2);
  1094. if (err < 0)
  1095. goto out_release_1;
  1096. err = sock1->ops->socketpair(sock1, sock2);
  1097. if (err < 0)
  1098. goto out_release_both;
  1099. fd1 = sock_alloc_fd(&newfile1, flags & O_CLOEXEC);
  1100. if (unlikely(fd1 < 0)) {
  1101. err = fd1;
  1102. goto out_release_both;
  1103. }
  1104. fd2 = sock_alloc_fd(&newfile2, flags & O_CLOEXEC);
  1105. if (unlikely(fd2 < 0)) {
  1106. err = fd2;
  1107. put_filp(newfile1);
  1108. put_unused_fd(fd1);
  1109. goto out_release_both;
  1110. }
  1111. err = sock_attach_fd(sock1, newfile1, flags & O_NONBLOCK);
  1112. if (unlikely(err < 0)) {
  1113. goto out_fd2;
  1114. }
  1115. err = sock_attach_fd(sock2, newfile2, flags & O_NONBLOCK);
  1116. if (unlikely(err < 0)) {
  1117. fput(newfile1);
  1118. goto out_fd1;
  1119. }
  1120. err = audit_fd_pair(fd1, fd2);
  1121. if (err < 0) {
  1122. fput(newfile1);
  1123. fput(newfile2);
  1124. goto out_fd;
  1125. }
  1126. fd_install(fd1, newfile1);
  1127. fd_install(fd2, newfile2);
  1128. /* fd1 and fd2 may be already another descriptors.
  1129. * Not kernel problem.
  1130. */
  1131. err = put_user(fd1, &usockvec[0]);
  1132. if (!err)
  1133. err = put_user(fd2, &usockvec[1]);
  1134. if (!err)
  1135. return 0;
  1136. sys_close(fd2);
  1137. sys_close(fd1);
  1138. return err;
  1139. out_release_both:
  1140. sock_release(sock2);
  1141. out_release_1:
  1142. sock_release(sock1);
  1143. out:
  1144. return err;
  1145. out_fd2:
  1146. put_filp(newfile1);
  1147. sock_release(sock1);
  1148. out_fd1:
  1149. put_filp(newfile2);
  1150. sock_release(sock2);
  1151. out_fd:
  1152. put_unused_fd(fd1);
  1153. put_unused_fd(fd2);
  1154. goto out;
  1155. }
  1156. /*
  1157. * Bind a name to a socket. Nothing much to do here since it's
  1158. * the protocol's responsibility to handle the local address.
  1159. *
  1160. * We move the socket address to kernel space before we call
  1161. * the protocol layer (having also checked the address is ok).
  1162. */
  1163. asmlinkage long sys_bind(int fd, struct sockaddr __user *umyaddr, int addrlen)
  1164. {
  1165. struct socket *sock;
  1166. struct sockaddr_storage address;
  1167. int err, fput_needed;
  1168. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1169. if (sock) {
  1170. err = move_addr_to_kernel(umyaddr, addrlen, (struct sockaddr *)&address);
  1171. if (err >= 0) {
  1172. err = security_socket_bind(sock,
  1173. (struct sockaddr *)&address,
  1174. addrlen);
  1175. if (!err)
  1176. err = sock->ops->bind(sock,
  1177. (struct sockaddr *)
  1178. &address, addrlen);
  1179. }
  1180. fput_light(sock->file, fput_needed);
  1181. }
  1182. return err;
  1183. }
  1184. /*
  1185. * Perform a listen. Basically, we allow the protocol to do anything
  1186. * necessary for a listen, and if that works, we mark the socket as
  1187. * ready for listening.
  1188. */
  1189. asmlinkage long sys_listen(int fd, int backlog)
  1190. {
  1191. struct socket *sock;
  1192. int err, fput_needed;
  1193. int somaxconn;
  1194. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1195. if (sock) {
  1196. somaxconn = sock_net(sock->sk)->core.sysctl_somaxconn;
  1197. if ((unsigned)backlog > somaxconn)
  1198. backlog = somaxconn;
  1199. err = security_socket_listen(sock, backlog);
  1200. if (!err)
  1201. err = sock->ops->listen(sock, backlog);
  1202. fput_light(sock->file, fput_needed);
  1203. }
  1204. return err;
  1205. }
  1206. /*
  1207. * For accept, we attempt to create a new socket, set up the link
  1208. * with the client, wake up the client, then return the new
  1209. * connected fd. We collect the address of the connector in kernel
  1210. * space and move it to user at the very end. This is unclean because
  1211. * we open the socket then return an error.
  1212. *
  1213. * 1003.1g adds the ability to recvmsg() to query connection pending
  1214. * status to recvmsg. We need to add that support in a way thats
  1215. * clean when we restucture accept also.
  1216. */
  1217. asmlinkage long sys_accept4(int fd, struct sockaddr __user *upeer_sockaddr,
  1218. int __user *upeer_addrlen, int flags)
  1219. {
  1220. struct socket *sock, *newsock;
  1221. struct file *newfile;
  1222. int err, len, newfd, fput_needed;
  1223. struct sockaddr_storage address;
  1224. if (flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
  1225. return -EINVAL;
  1226. if (SOCK_NONBLOCK != O_NONBLOCK && (flags & SOCK_NONBLOCK))
  1227. flags = (flags & ~SOCK_NONBLOCK) | O_NONBLOCK;
  1228. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1229. if (!sock)
  1230. goto out;
  1231. err = -ENFILE;
  1232. if (!(newsock = sock_alloc()))
  1233. goto out_put;
  1234. newsock->type = sock->type;
  1235. newsock->ops = sock->ops;
  1236. /*
  1237. * We don't need try_module_get here, as the listening socket (sock)
  1238. * has the protocol module (sock->ops->owner) held.
  1239. */
  1240. __module_get(newsock->ops->owner);
  1241. newfd = sock_alloc_fd(&newfile, flags & O_CLOEXEC);
  1242. if (unlikely(newfd < 0)) {
  1243. err = newfd;
  1244. sock_release(newsock);
  1245. goto out_put;
  1246. }
  1247. err = sock_attach_fd(newsock, newfile, flags & O_NONBLOCK);
  1248. if (err < 0)
  1249. goto out_fd_simple;
  1250. err = security_socket_accept(sock, newsock);
  1251. if (err)
  1252. goto out_fd;
  1253. err = sock->ops->accept(sock, newsock, sock->file->f_flags);
  1254. if (err < 0)
  1255. goto out_fd;
  1256. if (upeer_sockaddr) {
  1257. if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
  1258. &len, 2) < 0) {
  1259. err = -ECONNABORTED;
  1260. goto out_fd;
  1261. }
  1262. err = move_addr_to_user((struct sockaddr *)&address,
  1263. len, upeer_sockaddr, upeer_addrlen);
  1264. if (err < 0)
  1265. goto out_fd;
  1266. }
  1267. /* File flags are not inherited via accept() unlike another OSes. */
  1268. fd_install(newfd, newfile);
  1269. err = newfd;
  1270. security_socket_post_accept(sock, newsock);
  1271. out_put:
  1272. fput_light(sock->file, fput_needed);
  1273. out:
  1274. return err;
  1275. out_fd_simple:
  1276. sock_release(newsock);
  1277. put_filp(newfile);
  1278. put_unused_fd(newfd);
  1279. goto out_put;
  1280. out_fd:
  1281. fput(newfile);
  1282. put_unused_fd(newfd);
  1283. goto out_put;
  1284. }
  1285. asmlinkage long sys_accept(int fd, struct sockaddr __user *upeer_sockaddr,
  1286. int __user *upeer_addrlen)
  1287. {
  1288. return sys_accept4(fd, upeer_sockaddr, upeer_addrlen, 0);
  1289. }
  1290. /*
  1291. * Attempt to connect to a socket with the server address. The address
  1292. * is in user space so we verify it is OK and move it to kernel space.
  1293. *
  1294. * For 1003.1g we need to add clean support for a bind to AF_UNSPEC to
  1295. * break bindings
  1296. *
  1297. * NOTE: 1003.1g draft 6.3 is broken with respect to AX.25/NetROM and
  1298. * other SEQPACKET protocols that take time to connect() as it doesn't
  1299. * include the -EINPROGRESS status for such sockets.
  1300. */
  1301. asmlinkage long sys_connect(int fd, struct sockaddr __user *uservaddr,
  1302. int addrlen)
  1303. {
  1304. struct socket *sock;
  1305. struct sockaddr_storage address;
  1306. int err, fput_needed;
  1307. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1308. if (!sock)
  1309. goto out;
  1310. err = move_addr_to_kernel(uservaddr, addrlen, (struct sockaddr *)&address);
  1311. if (err < 0)
  1312. goto out_put;
  1313. err =
  1314. security_socket_connect(sock, (struct sockaddr *)&address, addrlen);
  1315. if (err)
  1316. goto out_put;
  1317. err = sock->ops->connect(sock, (struct sockaddr *)&address, addrlen,
  1318. sock->file->f_flags);
  1319. out_put:
  1320. fput_light(sock->file, fput_needed);
  1321. out:
  1322. return err;
  1323. }
  1324. /*
  1325. * Get the local address ('name') of a socket object. Move the obtained
  1326. * name to user space.
  1327. */
  1328. asmlinkage long sys_getsockname(int fd, struct sockaddr __user *usockaddr,
  1329. int __user *usockaddr_len)
  1330. {
  1331. struct socket *sock;
  1332. struct sockaddr_storage address;
  1333. int len, err, fput_needed;
  1334. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1335. if (!sock)
  1336. goto out;
  1337. err = security_socket_getsockname(sock);
  1338. if (err)
  1339. goto out_put;
  1340. err = sock->ops->getname(sock, (struct sockaddr *)&address, &len, 0);
  1341. if (err)
  1342. goto out_put;
  1343. err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr, usockaddr_len);
  1344. out_put:
  1345. fput_light(sock->file, fput_needed);
  1346. out:
  1347. return err;
  1348. }
  1349. /*
  1350. * Get the remote address ('name') of a socket object. Move the obtained
  1351. * name to user space.
  1352. */
  1353. asmlinkage long sys_getpeername(int fd, struct sockaddr __user *usockaddr,
  1354. int __user *usockaddr_len)
  1355. {
  1356. struct socket *sock;
  1357. struct sockaddr_storage address;
  1358. int len, err, fput_needed;
  1359. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1360. if (sock != NULL) {
  1361. err = security_socket_getpeername(sock);
  1362. if (err) {
  1363. fput_light(sock->file, fput_needed);
  1364. return err;
  1365. }
  1366. err =
  1367. sock->ops->getname(sock, (struct sockaddr *)&address, &len,
  1368. 1);
  1369. if (!err)
  1370. err = move_addr_to_user((struct sockaddr *)&address, len, usockaddr,
  1371. usockaddr_len);
  1372. fput_light(sock->file, fput_needed);
  1373. }
  1374. return err;
  1375. }
  1376. /*
  1377. * Send a datagram to a given address. We move the address into kernel
  1378. * space and check the user space data area is readable before invoking
  1379. * the protocol.
  1380. */
  1381. asmlinkage long sys_sendto(int fd, void __user *buff, size_t len,
  1382. unsigned flags, struct sockaddr __user *addr,
  1383. int addr_len)
  1384. {
  1385. struct socket *sock;
  1386. struct sockaddr_storage address;
  1387. int err;
  1388. struct msghdr msg;
  1389. struct iovec iov;
  1390. int fput_needed;
  1391. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1392. if (!sock)
  1393. goto out;
  1394. iov.iov_base = buff;
  1395. iov.iov_len = len;
  1396. msg.msg_name = NULL;
  1397. msg.msg_iov = &iov;
  1398. msg.msg_iovlen = 1;
  1399. msg.msg_control = NULL;
  1400. msg.msg_controllen = 0;
  1401. msg.msg_namelen = 0;
  1402. if (addr) {
  1403. err = move_addr_to_kernel(addr, addr_len, (struct sockaddr *)&address);
  1404. if (err < 0)
  1405. goto out_put;
  1406. msg.msg_name = (struct sockaddr *)&address;
  1407. msg.msg_namelen = addr_len;
  1408. }
  1409. if (sock->file->f_flags & O_NONBLOCK)
  1410. flags |= MSG_DONTWAIT;
  1411. msg.msg_flags = flags;
  1412. err = sock_sendmsg(sock, &msg, len);
  1413. out_put:
  1414. fput_light(sock->file, fput_needed);
  1415. out:
  1416. return err;
  1417. }
  1418. /*
  1419. * Send a datagram down a socket.
  1420. */
  1421. asmlinkage long sys_send(int fd, void __user *buff, size_t len, unsigned flags)
  1422. {
  1423. return sys_sendto(fd, buff, len, flags, NULL, 0);
  1424. }
  1425. /*
  1426. * Receive a frame from the socket and optionally record the address of the
  1427. * sender. We verify the buffers are writable and if needed move the
  1428. * sender address from kernel to user space.
  1429. */
  1430. asmlinkage long sys_recvfrom(int fd, void __user *ubuf, size_t size,
  1431. unsigned flags, struct sockaddr __user *addr,
  1432. int __user *addr_len)
  1433. {
  1434. struct socket *sock;
  1435. struct iovec iov;
  1436. struct msghdr msg;
  1437. struct sockaddr_storage address;
  1438. int err, err2;
  1439. int fput_needed;
  1440. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1441. if (!sock)
  1442. goto out;
  1443. msg.msg_control = NULL;
  1444. msg.msg_controllen = 0;
  1445. msg.msg_iovlen = 1;
  1446. msg.msg_iov = &iov;
  1447. iov.iov_len = size;
  1448. iov.iov_base = ubuf;
  1449. msg.msg_name = (struct sockaddr *)&address;
  1450. msg.msg_namelen = sizeof(address);
  1451. if (sock->file->f_flags & O_NONBLOCK)
  1452. flags |= MSG_DONTWAIT;
  1453. err = sock_recvmsg(sock, &msg, size, flags);
  1454. if (err >= 0 && addr != NULL) {
  1455. err2 = move_addr_to_user((struct sockaddr *)&address,
  1456. msg.msg_namelen, addr, addr_len);
  1457. if (err2 < 0)
  1458. err = err2;
  1459. }
  1460. fput_light(sock->file, fput_needed);
  1461. out:
  1462. return err;
  1463. }
  1464. /*
  1465. * Receive a datagram from a socket.
  1466. */
  1467. asmlinkage long sys_recv(int fd, void __user *ubuf, size_t size,
  1468. unsigned flags)
  1469. {
  1470. return sys_recvfrom(fd, ubuf, size, flags, NULL, NULL);
  1471. }
  1472. /*
  1473. * Set a socket option. Because we don't know the option lengths we have
  1474. * to pass the user mode parameter for the protocols to sort out.
  1475. */
  1476. asmlinkage long sys_setsockopt(int fd, int level, int optname,
  1477. char __user *optval, int optlen)
  1478. {
  1479. int err, fput_needed;
  1480. struct socket *sock;
  1481. if (optlen < 0)
  1482. return -EINVAL;
  1483. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1484. if (sock != NULL) {
  1485. err = security_socket_setsockopt(sock, level, optname);
  1486. if (err)
  1487. goto out_put;
  1488. if (level == SOL_SOCKET)
  1489. err =
  1490. sock_setsockopt(sock, level, optname, optval,
  1491. optlen);
  1492. else
  1493. err =
  1494. sock->ops->setsockopt(sock, level, optname, optval,
  1495. optlen);
  1496. out_put:
  1497. fput_light(sock->file, fput_needed);
  1498. }
  1499. return err;
  1500. }
  1501. /*
  1502. * Get a socket option. Because we don't know the option lengths we have
  1503. * to pass a user mode parameter for the protocols to sort out.
  1504. */
  1505. asmlinkage long sys_getsockopt(int fd, int level, int optname,
  1506. char __user *optval, int __user *optlen)
  1507. {
  1508. int err, fput_needed;
  1509. struct socket *sock;
  1510. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1511. if (sock != NULL) {
  1512. err = security_socket_getsockopt(sock, level, optname);
  1513. if (err)
  1514. goto out_put;
  1515. if (level == SOL_SOCKET)
  1516. err =
  1517. sock_getsockopt(sock, level, optname, optval,
  1518. optlen);
  1519. else
  1520. err =
  1521. sock->ops->getsockopt(sock, level, optname, optval,
  1522. optlen);
  1523. out_put:
  1524. fput_light(sock->file, fput_needed);
  1525. }
  1526. return err;
  1527. }
  1528. /*
  1529. * Shutdown a socket.
  1530. */
  1531. asmlinkage long sys_shutdown(int fd, int how)
  1532. {
  1533. int err, fput_needed;
  1534. struct socket *sock;
  1535. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1536. if (sock != NULL) {
  1537. err = security_socket_shutdown(sock, how);
  1538. if (!err)
  1539. err = sock->ops->shutdown(sock, how);
  1540. fput_light(sock->file, fput_needed);
  1541. }
  1542. return err;
  1543. }
  1544. /* A couple of helpful macros for getting the address of the 32/64 bit
  1545. * fields which are the same type (int / unsigned) on our platforms.
  1546. */
  1547. #define COMPAT_MSG(msg, member) ((MSG_CMSG_COMPAT & flags) ? &msg##_compat->member : &msg->member)
  1548. #define COMPAT_NAMELEN(msg) COMPAT_MSG(msg, msg_namelen)
  1549. #define COMPAT_FLAGS(msg) COMPAT_MSG(msg, msg_flags)
  1550. /*
  1551. * BSD sendmsg interface
  1552. */
  1553. asmlinkage long sys_sendmsg(int fd, struct msghdr __user *msg, unsigned flags)
  1554. {
  1555. struct compat_msghdr __user *msg_compat =
  1556. (struct compat_msghdr __user *)msg;
  1557. struct socket *sock;
  1558. struct sockaddr_storage address;
  1559. struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
  1560. unsigned char ctl[sizeof(struct cmsghdr) + 20]
  1561. __attribute__ ((aligned(sizeof(__kernel_size_t))));
  1562. /* 20 is size of ipv6_pktinfo */
  1563. unsigned char *ctl_buf = ctl;
  1564. struct msghdr msg_sys;
  1565. int err, ctl_len, iov_size, total_len;
  1566. int fput_needed;
  1567. err = -EFAULT;
  1568. if (MSG_CMSG_COMPAT & flags) {
  1569. if (get_compat_msghdr(&msg_sys, msg_compat))
  1570. return -EFAULT;
  1571. }
  1572. else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
  1573. return -EFAULT;
  1574. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1575. if (!sock)
  1576. goto out;
  1577. /* do not move before msg_sys is valid */
  1578. err = -EMSGSIZE;
  1579. if (msg_sys.msg_iovlen > UIO_MAXIOV)
  1580. goto out_put;
  1581. /* Check whether to allocate the iovec area */
  1582. err = -ENOMEM;
  1583. iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
  1584. if (msg_sys.msg_iovlen > UIO_FASTIOV) {
  1585. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1586. if (!iov)
  1587. goto out_put;
  1588. }
  1589. /* This will also move the address data into kernel space */
  1590. if (MSG_CMSG_COMPAT & flags) {
  1591. err = verify_compat_iovec(&msg_sys, iov,
  1592. (struct sockaddr *)&address,
  1593. VERIFY_READ);
  1594. } else
  1595. err = verify_iovec(&msg_sys, iov,
  1596. (struct sockaddr *)&address,
  1597. VERIFY_READ);
  1598. if (err < 0)
  1599. goto out_freeiov;
  1600. total_len = err;
  1601. err = -ENOBUFS;
  1602. if (msg_sys.msg_controllen > INT_MAX)
  1603. goto out_freeiov;
  1604. ctl_len = msg_sys.msg_controllen;
  1605. if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
  1606. err =
  1607. cmsghdr_from_user_compat_to_kern(&msg_sys, sock->sk, ctl,
  1608. sizeof(ctl));
  1609. if (err)
  1610. goto out_freeiov;
  1611. ctl_buf = msg_sys.msg_control;
  1612. ctl_len = msg_sys.msg_controllen;
  1613. } else if (ctl_len) {
  1614. if (ctl_len > sizeof(ctl)) {
  1615. ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
  1616. if (ctl_buf == NULL)
  1617. goto out_freeiov;
  1618. }
  1619. err = -EFAULT;
  1620. /*
  1621. * Careful! Before this, msg_sys.msg_control contains a user pointer.
  1622. * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
  1623. * checking falls down on this.
  1624. */
  1625. if (copy_from_user(ctl_buf, (void __user *)msg_sys.msg_control,
  1626. ctl_len))
  1627. goto out_freectl;
  1628. msg_sys.msg_control = ctl_buf;
  1629. }
  1630. msg_sys.msg_flags = flags;
  1631. if (sock->file->f_flags & O_NONBLOCK)
  1632. msg_sys.msg_flags |= MSG_DONTWAIT;
  1633. err = sock_sendmsg(sock, &msg_sys, total_len);
  1634. out_freectl:
  1635. if (ctl_buf != ctl)
  1636. sock_kfree_s(sock->sk, ctl_buf, ctl_len);
  1637. out_freeiov:
  1638. if (iov != iovstack)
  1639. sock_kfree_s(sock->sk, iov, iov_size);
  1640. out_put:
  1641. fput_light(sock->file, fput_needed);
  1642. out:
  1643. return err;
  1644. }
  1645. /*
  1646. * BSD recvmsg interface
  1647. */
  1648. asmlinkage long sys_recvmsg(int fd, struct msghdr __user *msg,
  1649. unsigned int flags)
  1650. {
  1651. struct compat_msghdr __user *msg_compat =
  1652. (struct compat_msghdr __user *)msg;
  1653. struct socket *sock;
  1654. struct iovec iovstack[UIO_FASTIOV];
  1655. struct iovec *iov = iovstack;
  1656. struct msghdr msg_sys;
  1657. unsigned long cmsg_ptr;
  1658. int err, iov_size, total_len, len;
  1659. int fput_needed;
  1660. /* kernel mode address */
  1661. struct sockaddr_storage addr;
  1662. /* user mode address pointers */
  1663. struct sockaddr __user *uaddr;
  1664. int __user *uaddr_len;
  1665. if (MSG_CMSG_COMPAT & flags) {
  1666. if (get_compat_msghdr(&msg_sys, msg_compat))
  1667. return -EFAULT;
  1668. }
  1669. else if (copy_from_user(&msg_sys, msg, sizeof(struct msghdr)))
  1670. return -EFAULT;
  1671. sock = sockfd_lookup_light(fd, &err, &fput_needed);
  1672. if (!sock)
  1673. goto out;
  1674. err = -EMSGSIZE;
  1675. if (msg_sys.msg_iovlen > UIO_MAXIOV)
  1676. goto out_put;
  1677. /* Check whether to allocate the iovec area */
  1678. err = -ENOMEM;
  1679. iov_size = msg_sys.msg_iovlen * sizeof(struct iovec);
  1680. if (msg_sys.msg_iovlen > UIO_FASTIOV) {
  1681. iov = sock_kmalloc(sock->sk, iov_size, GFP_KERNEL);
  1682. if (!iov)
  1683. goto out_put;
  1684. }
  1685. /*
  1686. * Save the user-mode address (verify_iovec will change the
  1687. * kernel msghdr to use the kernel address space)
  1688. */
  1689. uaddr = (__force void __user *)msg_sys.msg_name;
  1690. uaddr_len = COMPAT_NAMELEN(msg);
  1691. if (MSG_CMSG_COMPAT & flags) {
  1692. err = verify_compat_iovec(&msg_sys, iov,
  1693. (struct sockaddr *)&addr,
  1694. VERIFY_WRITE);
  1695. } else
  1696. err = verify_iovec(&msg_sys, iov,
  1697. (struct sockaddr *)&addr,
  1698. VERIFY_WRITE);
  1699. if (err < 0)
  1700. goto out_freeiov;
  1701. total_len = err;
  1702. cmsg_ptr = (unsigned long)msg_sys.msg_control;
  1703. msg_sys.msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
  1704. if (sock->file->f_flags & O_NONBLOCK)
  1705. flags |= MSG_DONTWAIT;
  1706. err = sock_recvmsg(sock, &msg_sys, total_len, flags);
  1707. if (err < 0)
  1708. goto out_freeiov;
  1709. len = err;
  1710. if (uaddr != NULL) {
  1711. err = move_addr_to_user((struct sockaddr *)&addr,
  1712. msg_sys.msg_namelen, uaddr,
  1713. uaddr_len);
  1714. if (err < 0)
  1715. goto out_freeiov;
  1716. }
  1717. err = __put_user((msg_sys.msg_flags & ~MSG_CMSG_COMPAT),
  1718. COMPAT_FLAGS(msg));
  1719. if (err)
  1720. goto out_freeiov;
  1721. if (MSG_CMSG_COMPAT & flags)
  1722. err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
  1723. &msg_compat->msg_controllen);
  1724. else
  1725. err = __put_user((unsigned long)msg_sys.msg_control - cmsg_ptr,
  1726. &msg->msg_controllen);
  1727. if (err)
  1728. goto out_freeiov;
  1729. err = len;
  1730. out_freeiov:
  1731. if (iov != iovstack)
  1732. sock_kfree_s(sock->sk, iov, iov_size);
  1733. out_put:
  1734. fput_light(sock->file, fput_needed);
  1735. out:
  1736. return err;
  1737. }
  1738. #ifdef __ARCH_WANT_SYS_SOCKETCALL
  1739. /* Argument list sizes for sys_socketcall */
  1740. #define AL(x) ((x) * sizeof(unsigned long))
  1741. static const unsigned char nargs[19]={
  1742. AL(0),AL(3),AL(3),AL(3),AL(2),AL(3),
  1743. AL(3),AL(3),AL(4),AL(4),AL(4),AL(6),
  1744. AL(6),AL(2),AL(5),AL(5),AL(3),AL(3),
  1745. AL(4)
  1746. };
  1747. #undef AL
  1748. /*
  1749. * System call vectors.
  1750. *
  1751. * Argument checking cleaned up. Saved 20% in size.
  1752. * This function doesn't need to set the kernel lock because
  1753. * it is set by the callees.
  1754. */
  1755. asmlinkage long sys_socketcall(int call, unsigned long __user *args)
  1756. {
  1757. unsigned long a[6];
  1758. unsigned long a0, a1;
  1759. int err;
  1760. if (call < 1 || call > SYS_ACCEPT4)
  1761. return -EINVAL;
  1762. /* copy_from_user should be SMP safe. */
  1763. if (copy_from_user(a, args, nargs[call]))
  1764. return -EFAULT;
  1765. err = audit_socketcall(nargs[call] / sizeof(unsigned long), a);
  1766. if (err)
  1767. return err;
  1768. a0 = a[0];
  1769. a1 = a[1];
  1770. switch (call) {
  1771. case SYS_SOCKET:
  1772. err = sys_socket(a0, a1, a[2]);
  1773. break;
  1774. case SYS_BIND:
  1775. err = sys_bind(a0, (struct sockaddr __user *)a1, a[2]);
  1776. break;
  1777. case SYS_CONNECT:
  1778. err = sys_connect(a0, (struct sockaddr __user *)a1, a[2]);
  1779. break;
  1780. case SYS_LISTEN:
  1781. err = sys_listen(a0, a1);
  1782. break;
  1783. case SYS_ACCEPT:
  1784. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  1785. (int __user *)a[2], 0);
  1786. break;
  1787. case SYS_GETSOCKNAME:
  1788. err =
  1789. sys_getsockname(a0, (struct sockaddr __user *)a1,
  1790. (int __user *)a[2]);
  1791. break;
  1792. case SYS_GETPEERNAME:
  1793. err =
  1794. sys_getpeername(a0, (struct sockaddr __user *)a1,
  1795. (int __user *)a[2]);
  1796. break;
  1797. case SYS_SOCKETPAIR:
  1798. err = sys_socketpair(a0, a1, a[2], (int __user *)a[3]);
  1799. break;
  1800. case SYS_SEND:
  1801. err = sys_send(a0, (void __user *)a1, a[2], a[3]);
  1802. break;
  1803. case SYS_SENDTO:
  1804. err = sys_sendto(a0, (void __user *)a1, a[2], a[3],
  1805. (struct sockaddr __user *)a[4], a[5]);
  1806. break;
  1807. case SYS_RECV:
  1808. err = sys_recv(a0, (void __user *)a1, a[2], a[3]);
  1809. break;
  1810. case SYS_RECVFROM:
  1811. err = sys_recvfrom(a0, (void __user *)a1, a[2], a[3],
  1812. (struct sockaddr __user *)a[4],
  1813. (int __user *)a[5]);
  1814. break;
  1815. case SYS_SHUTDOWN:
  1816. err = sys_shutdown(a0, a1);
  1817. break;
  1818. case SYS_SETSOCKOPT:
  1819. err = sys_setsockopt(a0, a1, a[2], (char __user *)a[3], a[4]);
  1820. break;
  1821. case SYS_GETSOCKOPT:
  1822. err =
  1823. sys_getsockopt(a0, a1, a[2], (char __user *)a[3],
  1824. (int __user *)a[4]);
  1825. break;
  1826. case SYS_SENDMSG:
  1827. err = sys_sendmsg(a0, (struct msghdr __user *)a1, a[2]);
  1828. break;
  1829. case SYS_RECVMSG:
  1830. err = sys_recvmsg(a0, (struct msghdr __user *)a1, a[2]);
  1831. break;
  1832. case SYS_ACCEPT4:
  1833. err = sys_accept4(a0, (struct sockaddr __user *)a1,
  1834. (int __user *)a[2], a[3]);
  1835. break;
  1836. default:
  1837. err = -EINVAL;
  1838. break;
  1839. }
  1840. return err;
  1841. }
  1842. #endif /* __ARCH_WANT_SYS_SOCKETCALL */
  1843. /**
  1844. * sock_register - add a socket protocol handler
  1845. * @ops: description of protocol
  1846. *
  1847. * This function is called by a protocol handler that wants to
  1848. * advertise its address family, and have it linked into the
  1849. * socket interface. The value ops->family coresponds to the
  1850. * socket system call protocol family.
  1851. */
  1852. int sock_register(const struct net_proto_family *ops)
  1853. {
  1854. int err;
  1855. if (ops->family >= NPROTO) {
  1856. printk(KERN_CRIT "protocol %d >= NPROTO(%d)\n", ops->family,
  1857. NPROTO);
  1858. return -ENOBUFS;
  1859. }
  1860. spin_lock(&net_family_lock);
  1861. if (net_families[ops->family])
  1862. err = -EEXIST;
  1863. else {
  1864. net_families[ops->family] = ops;
  1865. err = 0;
  1866. }
  1867. spin_unlock(&net_family_lock);
  1868. printk(KERN_INFO "NET: Registered protocol family %d\n", ops->family);
  1869. return err;
  1870. }
  1871. /**
  1872. * sock_unregister - remove a protocol handler
  1873. * @family: protocol family to remove
  1874. *
  1875. * This function is called by a protocol handler that wants to
  1876. * remove its address family, and have it unlinked from the
  1877. * new socket creation.
  1878. *
  1879. * If protocol handler is a module, then it can use module reference
  1880. * counts to protect against new references. If protocol handler is not
  1881. * a module then it needs to provide its own protection in
  1882. * the ops->create routine.
  1883. */
  1884. void sock_unregister(int family)
  1885. {
  1886. BUG_ON(family < 0 || family >= NPROTO);
  1887. spin_lock(&net_family_lock);
  1888. net_families[family] = NULL;
  1889. spin_unlock(&net_family_lock);
  1890. synchronize_rcu();
  1891. printk(KERN_INFO "NET: Unregistered protocol family %d\n", family);
  1892. }
  1893. static int __init sock_init(void)
  1894. {
  1895. /*
  1896. * Initialize sock SLAB cache.
  1897. */
  1898. sk_init();
  1899. /*
  1900. * Initialize skbuff SLAB cache
  1901. */
  1902. skb_init();
  1903. /*
  1904. * Initialize the protocols module.
  1905. */
  1906. init_inodecache();
  1907. register_filesystem(&sock_fs_type);
  1908. sock_mnt = kern_mount(&sock_fs_type);
  1909. /* The real protocol initialization is performed in later initcalls.
  1910. */
  1911. #ifdef CONFIG_NETFILTER
  1912. netfilter_init();
  1913. #endif
  1914. return 0;
  1915. }
  1916. core_initcall(sock_init); /* early initcall */
  1917. #ifdef CONFIG_PROC_FS
  1918. void socket_seq_show(struct seq_file *seq)
  1919. {
  1920. int cpu;
  1921. int counter = 0;
  1922. for_each_possible_cpu(cpu)
  1923. counter += per_cpu(sockets_in_use, cpu);
  1924. /* It can be negative, by the way. 8) */
  1925. if (counter < 0)
  1926. counter = 0;
  1927. seq_printf(seq, "sockets: used %d\n", counter);
  1928. }
  1929. #endif /* CONFIG_PROC_FS */
  1930. #ifdef CONFIG_COMPAT
  1931. static long compat_sock_ioctl(struct file *file, unsigned cmd,
  1932. unsigned long arg)
  1933. {
  1934. struct socket *sock = file->private_data;
  1935. int ret = -ENOIOCTLCMD;
  1936. struct sock *sk;
  1937. struct net *net;
  1938. sk = sock->sk;
  1939. net = sock_net(sk);
  1940. if (sock->ops->compat_ioctl)
  1941. ret = sock->ops->compat_ioctl(sock, cmd, arg);
  1942. if (ret == -ENOIOCTLCMD &&
  1943. (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST))
  1944. ret = compat_wext_handle_ioctl(net, cmd, arg);
  1945. return ret;
  1946. }
  1947. #endif
  1948. int kernel_bind(struct socket *sock, struct sockaddr *addr, int addrlen)
  1949. {
  1950. return sock->ops->bind(sock, addr, addrlen);
  1951. }
  1952. int kernel_listen(struct socket *sock, int backlog)
  1953. {
  1954. return sock->ops->listen(sock, backlog);
  1955. }
  1956. int kernel_accept(struct socket *sock, struct socket **newsock, int flags)
  1957. {
  1958. struct sock *sk = sock->sk;
  1959. int err;
  1960. err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
  1961. newsock);
  1962. if (err < 0)
  1963. goto done;
  1964. err = sock->ops->accept(sock, *newsock, flags);
  1965. if (err < 0) {
  1966. sock_release(*newsock);
  1967. *newsock = NULL;
  1968. goto done;
  1969. }
  1970. (*newsock)->ops = sock->ops;
  1971. __module_get((*newsock)->ops->owner);
  1972. done:
  1973. return err;
  1974. }
  1975. int kernel_connect(struct socket *sock, struct sockaddr *addr, int addrlen,
  1976. int flags)
  1977. {
  1978. return sock->ops->connect(sock, addr, addrlen, flags);
  1979. }
  1980. int kernel_getsockname(struct socket *sock, struct sockaddr *addr,
  1981. int *addrlen)
  1982. {
  1983. return sock->ops->getname(sock, addr, addrlen, 0);
  1984. }
  1985. int kernel_getpeername(struct socket *sock, struct sockaddr *addr,
  1986. int *addrlen)
  1987. {
  1988. return sock->ops->getname(sock, addr, addrlen, 1);
  1989. }
  1990. int kernel_getsockopt(struct socket *sock, int level, int optname,
  1991. char *optval, int *optlen)
  1992. {
  1993. mm_segment_t oldfs = get_fs();
  1994. int err;
  1995. set_fs(KERNEL_DS);
  1996. if (level == SOL_SOCKET)
  1997. err = sock_getsockopt(sock, level, optname, optval, optlen);
  1998. else
  1999. err = sock->ops->getsockopt(sock, level, optname, optval,
  2000. optlen);
  2001. set_fs(oldfs);
  2002. return err;
  2003. }
  2004. int kernel_setsockopt(struct socket *sock, int level, int optname,
  2005. char *optval, int optlen)
  2006. {
  2007. mm_segment_t oldfs = get_fs();
  2008. int err;
  2009. set_fs(KERNEL_DS);
  2010. if (level == SOL_SOCKET)
  2011. err = sock_setsockopt(sock, level, optname, optval, optlen);
  2012. else
  2013. err = sock->ops->setsockopt(sock, level, optname, optval,
  2014. optlen);
  2015. set_fs(oldfs);
  2016. return err;
  2017. }
  2018. int kernel_sendpage(struct socket *sock, struct page *page, int offset,
  2019. size_t size, int flags)
  2020. {
  2021. if (sock->ops->sendpage)
  2022. return sock->ops->sendpage(sock, page, offset, size, flags);
  2023. return sock_no_sendpage(sock, page, offset, size, flags);
  2024. }
  2025. int kernel_sock_ioctl(struct socket *sock, int cmd, unsigned long arg)
  2026. {
  2027. mm_segment_t oldfs = get_fs();
  2028. int err;
  2029. set_fs(KERNEL_DS);
  2030. err = sock->ops->ioctl(sock, cmd, arg);
  2031. set_fs(oldfs);
  2032. return err;
  2033. }
  2034. int kernel_sock_shutdown(struct socket *sock, enum sock_shutdown_cmd how)
  2035. {
  2036. return sock->ops->shutdown(sock, how);
  2037. }
  2038. EXPORT_SYMBOL(sock_create);
  2039. EXPORT_SYMBOL(sock_create_kern);
  2040. EXPORT_SYMBOL(sock_create_lite);
  2041. EXPORT_SYMBOL(sock_map_fd);
  2042. EXPORT_SYMBOL(sock_recvmsg);
  2043. EXPORT_SYMBOL(sock_register);
  2044. EXPORT_SYMBOL(sock_release);
  2045. EXPORT_SYMBOL(sock_sendmsg);
  2046. EXPORT_SYMBOL(sock_unregister);
  2047. EXPORT_SYMBOL(sock_wake_async);
  2048. EXPORT_SYMBOL(sockfd_lookup);
  2049. EXPORT_SYMBOL(kernel_sendmsg);
  2050. EXPORT_SYMBOL(kernel_recvmsg);
  2051. EXPORT_SYMBOL(kernel_bind);
  2052. EXPORT_SYMBOL(kernel_listen);
  2053. EXPORT_SYMBOL(kernel_accept);
  2054. EXPORT_SYMBOL(kernel_connect);
  2055. EXPORT_SYMBOL(kernel_getsockname);
  2056. EXPORT_SYMBOL(kernel_getpeername);
  2057. EXPORT_SYMBOL(kernel_getsockopt);
  2058. EXPORT_SYMBOL(kernel_setsockopt);
  2059. EXPORT_SYMBOL(kernel_sendpage);
  2060. EXPORT_SYMBOL(kernel_sock_ioctl);
  2061. EXPORT_SYMBOL(kernel_sock_shutdown);