socket.c 49 KB

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