socket.c 55 KB

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