socket.c 55 KB

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