socket.c 51 KB

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