socket.c 49 KB

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