socket.c 51 KB

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