af_decnet.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425
  1. /*
  2. * DECnet An implementation of the DECnet protocol suite for the LINUX
  3. * operating system. DECnet is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * DECnet Socket Layer Interface
  7. *
  8. * Authors: Eduardo Marcelo Serrat <emserrat@geocities.com>
  9. * Patrick Caulfield <patrick@pandh.demon.co.uk>
  10. *
  11. * Changes:
  12. * Steve Whitehouse: Copied from Eduardo Serrat and Patrick Caulfield's
  13. * version of the code. Original copyright preserved
  14. * below.
  15. * Steve Whitehouse: Some bug fixes, cleaning up some code to make it
  16. * compatible with my routing layer.
  17. * Steve Whitehouse: Merging changes from Eduardo Serrat and Patrick
  18. * Caulfield.
  19. * Steve Whitehouse: Further bug fixes, checking module code still works
  20. * with new routing layer.
  21. * Steve Whitehouse: Additional set/get_sockopt() calls.
  22. * Steve Whitehouse: Fixed TIOCINQ ioctl to be same as Eduardo's new
  23. * code.
  24. * Steve Whitehouse: recvmsg() changed to try and behave in a POSIX like
  25. * way. Didn't manage it entirely, but its better.
  26. * Steve Whitehouse: ditto for sendmsg().
  27. * Steve Whitehouse: A selection of bug fixes to various things.
  28. * Steve Whitehouse: Added TIOCOUTQ ioctl.
  29. * Steve Whitehouse: Fixes to username2sockaddr & sockaddr2username.
  30. * Steve Whitehouse: Fixes to connect() error returns.
  31. * Patrick Caulfield: Fixes to delayed acceptance logic.
  32. * David S. Miller: New socket locking
  33. * Steve Whitehouse: Socket list hashing/locking
  34. * Arnaldo C. Melo: use capable, not suser
  35. * Steve Whitehouse: Removed unused code. Fix to use sk->allocation
  36. * when required.
  37. * Patrick Caulfield: /proc/net/decnet now has object name/number
  38. * Steve Whitehouse: Fixed local port allocation, hashed sk list
  39. * Matthew Wilcox: Fixes for dn_ioctl()
  40. * Steve Whitehouse: New connect/accept logic to allow timeouts and
  41. * prepare for sendpage etc.
  42. */
  43. /******************************************************************************
  44. (c) 1995-1998 E.M. Serrat emserrat@geocities.com
  45. This program is free software; you can redistribute it and/or modify
  46. it under the terms of the GNU General Public License as published by
  47. the Free Software Foundation; either version 2 of the License, or
  48. any later version.
  49. This program is distributed in the hope that it will be useful,
  50. but WITHOUT ANY WARRANTY; without even the implied warranty of
  51. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  52. GNU General Public License for more details.
  53. HISTORY:
  54. Version Kernel Date Author/Comments
  55. ------- ------ ---- ---------------
  56. Version 0.0.1 2.0.30 01-dic-97 Eduardo Marcelo Serrat
  57. (emserrat@geocities.com)
  58. First Development of DECnet Socket La-
  59. yer for Linux. Only supports outgoing
  60. connections.
  61. Version 0.0.2 2.1.105 20-jun-98 Patrick J. Caulfield
  62. (patrick@pandh.demon.co.uk)
  63. Port to new kernel development version.
  64. Version 0.0.3 2.1.106 25-jun-98 Eduardo Marcelo Serrat
  65. (emserrat@geocities.com)
  66. _
  67. Added support for incoming connections
  68. so we can start developing server apps
  69. on Linux.
  70. -
  71. Module Support
  72. Version 0.0.4 2.1.109 21-jul-98 Eduardo Marcelo Serrat
  73. (emserrat@geocities.com)
  74. _
  75. Added support for X11R6.4. Now we can
  76. use DECnet transport for X on Linux!!!
  77. -
  78. Version 0.0.5 2.1.110 01-aug-98 Eduardo Marcelo Serrat
  79. (emserrat@geocities.com)
  80. Removed bugs on flow control
  81. Removed bugs on incoming accessdata
  82. order
  83. -
  84. Version 0.0.6 2.1.110 07-aug-98 Eduardo Marcelo Serrat
  85. dn_recvmsg fixes
  86. Patrick J. Caulfield
  87. dn_bind fixes
  88. *******************************************************************************/
  89. #include <linux/module.h>
  90. #include <linux/errno.h>
  91. #include <linux/types.h>
  92. #include <linux/slab.h>
  93. #include <linux/socket.h>
  94. #include <linux/in.h>
  95. #include <linux/kernel.h>
  96. #include <linux/sched.h>
  97. #include <linux/timer.h>
  98. #include <linux/string.h>
  99. #include <linux/sockios.h>
  100. #include <linux/net.h>
  101. #include <linux/netdevice.h>
  102. #include <linux/inet.h>
  103. #include <linux/route.h>
  104. #include <linux/netfilter.h>
  105. #include <linux/seq_file.h>
  106. #include <net/sock.h>
  107. #include <net/tcp_states.h>
  108. #include <net/flow.h>
  109. #include <asm/ioctls.h>
  110. #include <linux/capability.h>
  111. #include <linux/mm.h>
  112. #include <linux/interrupt.h>
  113. #include <linux/proc_fs.h>
  114. #include <linux/stat.h>
  115. #include <linux/init.h>
  116. #include <linux/poll.h>
  117. #include <net/net_namespace.h>
  118. #include <net/neighbour.h>
  119. #include <net/dst.h>
  120. #include <net/fib_rules.h>
  121. #include <net/dn.h>
  122. #include <net/dn_nsp.h>
  123. #include <net/dn_dev.h>
  124. #include <net/dn_route.h>
  125. #include <net/dn_fib.h>
  126. #include <net/dn_neigh.h>
  127. struct dn_sock {
  128. struct sock sk;
  129. struct dn_scp scp;
  130. };
  131. static void dn_keepalive(struct sock *sk);
  132. #define DN_SK_HASH_SHIFT 8
  133. #define DN_SK_HASH_SIZE (1 << DN_SK_HASH_SHIFT)
  134. #define DN_SK_HASH_MASK (DN_SK_HASH_SIZE - 1)
  135. static const struct proto_ops dn_proto_ops;
  136. static DEFINE_RWLOCK(dn_hash_lock);
  137. static struct hlist_head dn_sk_hash[DN_SK_HASH_SIZE];
  138. static struct hlist_head dn_wild_sk;
  139. static atomic_long_t decnet_memory_allocated;
  140. static int __dn_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen, int flags);
  141. static int __dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen, int flags);
  142. static struct hlist_head *dn_find_list(struct sock *sk)
  143. {
  144. struct dn_scp *scp = DN_SK(sk);
  145. if (scp->addr.sdn_flags & SDF_WILD)
  146. return hlist_empty(&dn_wild_sk) ? &dn_wild_sk : NULL;
  147. return &dn_sk_hash[le16_to_cpu(scp->addrloc) & DN_SK_HASH_MASK];
  148. }
  149. /*
  150. * Valid ports are those greater than zero and not already in use.
  151. */
  152. static int check_port(__le16 port)
  153. {
  154. struct sock *sk;
  155. struct hlist_node *node;
  156. if (port == 0)
  157. return -1;
  158. sk_for_each(sk, node, &dn_sk_hash[le16_to_cpu(port) & DN_SK_HASH_MASK]) {
  159. struct dn_scp *scp = DN_SK(sk);
  160. if (scp->addrloc == port)
  161. return -1;
  162. }
  163. return 0;
  164. }
  165. static unsigned short port_alloc(struct sock *sk)
  166. {
  167. struct dn_scp *scp = DN_SK(sk);
  168. static unsigned short port = 0x2000;
  169. unsigned short i_port = port;
  170. while(check_port(cpu_to_le16(++port)) != 0) {
  171. if (port == i_port)
  172. return 0;
  173. }
  174. scp->addrloc = cpu_to_le16(port);
  175. return 1;
  176. }
  177. /*
  178. * Since this is only ever called from user
  179. * level, we don't need a write_lock() version
  180. * of this.
  181. */
  182. static int dn_hash_sock(struct sock *sk)
  183. {
  184. struct dn_scp *scp = DN_SK(sk);
  185. struct hlist_head *list;
  186. int rv = -EUSERS;
  187. BUG_ON(sk_hashed(sk));
  188. write_lock_bh(&dn_hash_lock);
  189. if (!scp->addrloc && !port_alloc(sk))
  190. goto out;
  191. rv = -EADDRINUSE;
  192. if ((list = dn_find_list(sk)) == NULL)
  193. goto out;
  194. sk_add_node(sk, list);
  195. rv = 0;
  196. out:
  197. write_unlock_bh(&dn_hash_lock);
  198. return rv;
  199. }
  200. static void dn_unhash_sock(struct sock *sk)
  201. {
  202. write_lock(&dn_hash_lock);
  203. sk_del_node_init(sk);
  204. write_unlock(&dn_hash_lock);
  205. }
  206. static void dn_unhash_sock_bh(struct sock *sk)
  207. {
  208. write_lock_bh(&dn_hash_lock);
  209. sk_del_node_init(sk);
  210. write_unlock_bh(&dn_hash_lock);
  211. }
  212. static struct hlist_head *listen_hash(struct sockaddr_dn *addr)
  213. {
  214. int i;
  215. unsigned hash = addr->sdn_objnum;
  216. if (hash == 0) {
  217. hash = addr->sdn_objnamel;
  218. for(i = 0; i < le16_to_cpu(addr->sdn_objnamel); i++) {
  219. hash ^= addr->sdn_objname[i];
  220. hash ^= (hash << 3);
  221. }
  222. }
  223. return &dn_sk_hash[hash & DN_SK_HASH_MASK];
  224. }
  225. /*
  226. * Called to transform a socket from bound (i.e. with a local address)
  227. * into a listening socket (doesn't need a local port number) and rehashes
  228. * based upon the object name/number.
  229. */
  230. static void dn_rehash_sock(struct sock *sk)
  231. {
  232. struct hlist_head *list;
  233. struct dn_scp *scp = DN_SK(sk);
  234. if (scp->addr.sdn_flags & SDF_WILD)
  235. return;
  236. write_lock_bh(&dn_hash_lock);
  237. sk_del_node_init(sk);
  238. DN_SK(sk)->addrloc = 0;
  239. list = listen_hash(&DN_SK(sk)->addr);
  240. sk_add_node(sk, list);
  241. write_unlock_bh(&dn_hash_lock);
  242. }
  243. int dn_sockaddr2username(struct sockaddr_dn *sdn, unsigned char *buf, unsigned char type)
  244. {
  245. int len = 2;
  246. *buf++ = type;
  247. switch (type) {
  248. case 0:
  249. *buf++ = sdn->sdn_objnum;
  250. break;
  251. case 1:
  252. *buf++ = 0;
  253. *buf++ = le16_to_cpu(sdn->sdn_objnamel);
  254. memcpy(buf, sdn->sdn_objname, le16_to_cpu(sdn->sdn_objnamel));
  255. len = 3 + le16_to_cpu(sdn->sdn_objnamel);
  256. break;
  257. case 2:
  258. memset(buf, 0, 5);
  259. buf += 5;
  260. *buf++ = le16_to_cpu(sdn->sdn_objnamel);
  261. memcpy(buf, sdn->sdn_objname, le16_to_cpu(sdn->sdn_objnamel));
  262. len = 7 + le16_to_cpu(sdn->sdn_objnamel);
  263. break;
  264. }
  265. return len;
  266. }
  267. /*
  268. * On reception of usernames, we handle types 1 and 0 for destination
  269. * addresses only. Types 2 and 4 are used for source addresses, but the
  270. * UIC, GIC are ignored and they are both treated the same way. Type 3
  271. * is never used as I've no idea what its purpose might be or what its
  272. * format is.
  273. */
  274. int dn_username2sockaddr(unsigned char *data, int len, struct sockaddr_dn *sdn, unsigned char *fmt)
  275. {
  276. unsigned char type;
  277. int size = len;
  278. int namel = 12;
  279. sdn->sdn_objnum = 0;
  280. sdn->sdn_objnamel = cpu_to_le16(0);
  281. memset(sdn->sdn_objname, 0, DN_MAXOBJL);
  282. if (len < 2)
  283. return -1;
  284. len -= 2;
  285. *fmt = *data++;
  286. type = *data++;
  287. switch (*fmt) {
  288. case 0:
  289. sdn->sdn_objnum = type;
  290. return 2;
  291. case 1:
  292. namel = 16;
  293. break;
  294. case 2:
  295. len -= 4;
  296. data += 4;
  297. break;
  298. case 4:
  299. len -= 8;
  300. data += 8;
  301. break;
  302. default:
  303. return -1;
  304. }
  305. len -= 1;
  306. if (len < 0)
  307. return -1;
  308. sdn->sdn_objnamel = cpu_to_le16(*data++);
  309. len -= le16_to_cpu(sdn->sdn_objnamel);
  310. if ((len < 0) || (le16_to_cpu(sdn->sdn_objnamel) > namel))
  311. return -1;
  312. memcpy(sdn->sdn_objname, data, le16_to_cpu(sdn->sdn_objnamel));
  313. return size - len;
  314. }
  315. struct sock *dn_sklist_find_listener(struct sockaddr_dn *addr)
  316. {
  317. struct hlist_head *list = listen_hash(addr);
  318. struct hlist_node *node;
  319. struct sock *sk;
  320. read_lock(&dn_hash_lock);
  321. sk_for_each(sk, node, list) {
  322. struct dn_scp *scp = DN_SK(sk);
  323. if (sk->sk_state != TCP_LISTEN)
  324. continue;
  325. if (scp->addr.sdn_objnum) {
  326. if (scp->addr.sdn_objnum != addr->sdn_objnum)
  327. continue;
  328. } else {
  329. if (addr->sdn_objnum)
  330. continue;
  331. if (scp->addr.sdn_objnamel != addr->sdn_objnamel)
  332. continue;
  333. if (memcmp(scp->addr.sdn_objname, addr->sdn_objname, le16_to_cpu(addr->sdn_objnamel)) != 0)
  334. continue;
  335. }
  336. sock_hold(sk);
  337. read_unlock(&dn_hash_lock);
  338. return sk;
  339. }
  340. sk = sk_head(&dn_wild_sk);
  341. if (sk) {
  342. if (sk->sk_state == TCP_LISTEN)
  343. sock_hold(sk);
  344. else
  345. sk = NULL;
  346. }
  347. read_unlock(&dn_hash_lock);
  348. return sk;
  349. }
  350. struct sock *dn_find_by_skb(struct sk_buff *skb)
  351. {
  352. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  353. struct sock *sk;
  354. struct hlist_node *node;
  355. struct dn_scp *scp;
  356. read_lock(&dn_hash_lock);
  357. sk_for_each(sk, node, &dn_sk_hash[le16_to_cpu(cb->dst_port) & DN_SK_HASH_MASK]) {
  358. scp = DN_SK(sk);
  359. if (cb->src != dn_saddr2dn(&scp->peer))
  360. continue;
  361. if (cb->dst_port != scp->addrloc)
  362. continue;
  363. if (scp->addrrem && (cb->src_port != scp->addrrem))
  364. continue;
  365. sock_hold(sk);
  366. goto found;
  367. }
  368. sk = NULL;
  369. found:
  370. read_unlock(&dn_hash_lock);
  371. return sk;
  372. }
  373. static void dn_destruct(struct sock *sk)
  374. {
  375. struct dn_scp *scp = DN_SK(sk);
  376. skb_queue_purge(&scp->data_xmit_queue);
  377. skb_queue_purge(&scp->other_xmit_queue);
  378. skb_queue_purge(&scp->other_receive_queue);
  379. dst_release(rcu_dereference_check(sk->sk_dst_cache, 1));
  380. }
  381. static int dn_memory_pressure;
  382. static void dn_enter_memory_pressure(struct sock *sk)
  383. {
  384. if (!dn_memory_pressure) {
  385. dn_memory_pressure = 1;
  386. }
  387. }
  388. static struct proto dn_proto = {
  389. .name = "NSP",
  390. .owner = THIS_MODULE,
  391. .enter_memory_pressure = dn_enter_memory_pressure,
  392. .memory_pressure = &dn_memory_pressure,
  393. .memory_allocated = &decnet_memory_allocated,
  394. .sysctl_mem = sysctl_decnet_mem,
  395. .sysctl_wmem = sysctl_decnet_wmem,
  396. .sysctl_rmem = sysctl_decnet_rmem,
  397. .max_header = DN_MAX_NSP_DATA_HEADER + 64,
  398. .obj_size = sizeof(struct dn_sock),
  399. };
  400. static struct sock *dn_alloc_sock(struct net *net, struct socket *sock, gfp_t gfp)
  401. {
  402. struct dn_scp *scp;
  403. struct sock *sk = sk_alloc(net, PF_DECnet, gfp, &dn_proto);
  404. if (!sk)
  405. goto out;
  406. if (sock)
  407. sock->ops = &dn_proto_ops;
  408. sock_init_data(sock, sk);
  409. sk->sk_backlog_rcv = dn_nsp_backlog_rcv;
  410. sk->sk_destruct = dn_destruct;
  411. sk->sk_no_check = 1;
  412. sk->sk_family = PF_DECnet;
  413. sk->sk_protocol = 0;
  414. sk->sk_allocation = gfp;
  415. sk->sk_sndbuf = sysctl_decnet_wmem[1];
  416. sk->sk_rcvbuf = sysctl_decnet_rmem[1];
  417. /* Initialization of DECnet Session Control Port */
  418. scp = DN_SK(sk);
  419. scp->state = DN_O; /* Open */
  420. scp->numdat = 1; /* Next data seg to tx */
  421. scp->numoth = 1; /* Next oth data to tx */
  422. scp->ackxmt_dat = 0; /* Last data seg ack'ed */
  423. scp->ackxmt_oth = 0; /* Last oth data ack'ed */
  424. scp->ackrcv_dat = 0; /* Highest data ack recv*/
  425. scp->ackrcv_oth = 0; /* Last oth data ack rec*/
  426. scp->flowrem_sw = DN_SEND;
  427. scp->flowloc_sw = DN_SEND;
  428. scp->flowrem_dat = 0;
  429. scp->flowrem_oth = 1;
  430. scp->flowloc_dat = 0;
  431. scp->flowloc_oth = 1;
  432. scp->services_rem = 0;
  433. scp->services_loc = 1 | NSP_FC_NONE;
  434. scp->info_rem = 0;
  435. scp->info_loc = 0x03; /* NSP version 4.1 */
  436. scp->segsize_rem = 230 - DN_MAX_NSP_DATA_HEADER; /* Default: Updated by remote segsize */
  437. scp->nonagle = 0;
  438. scp->multi_ireq = 1;
  439. scp->accept_mode = ACC_IMMED;
  440. scp->addr.sdn_family = AF_DECnet;
  441. scp->peer.sdn_family = AF_DECnet;
  442. scp->accessdata.acc_accl = 5;
  443. memcpy(scp->accessdata.acc_acc, "LINUX", 5);
  444. scp->max_window = NSP_MAX_WINDOW;
  445. scp->snd_window = NSP_MIN_WINDOW;
  446. scp->nsp_srtt = NSP_INITIAL_SRTT;
  447. scp->nsp_rttvar = NSP_INITIAL_RTTVAR;
  448. scp->nsp_rxtshift = 0;
  449. skb_queue_head_init(&scp->data_xmit_queue);
  450. skb_queue_head_init(&scp->other_xmit_queue);
  451. skb_queue_head_init(&scp->other_receive_queue);
  452. scp->persist = 0;
  453. scp->persist_fxn = NULL;
  454. scp->keepalive = 10 * HZ;
  455. scp->keepalive_fxn = dn_keepalive;
  456. init_timer(&scp->delack_timer);
  457. scp->delack_pending = 0;
  458. scp->delack_fxn = dn_nsp_delayed_ack;
  459. dn_start_slow_timer(sk);
  460. out:
  461. return sk;
  462. }
  463. /*
  464. * Keepalive timer.
  465. * FIXME: Should respond to SO_KEEPALIVE etc.
  466. */
  467. static void dn_keepalive(struct sock *sk)
  468. {
  469. struct dn_scp *scp = DN_SK(sk);
  470. /*
  471. * By checking the other_data transmit queue is empty
  472. * we are double checking that we are not sending too
  473. * many of these keepalive frames.
  474. */
  475. if (skb_queue_empty(&scp->other_xmit_queue))
  476. dn_nsp_send_link(sk, DN_NOCHANGE, 0);
  477. }
  478. /*
  479. * Timer for shutdown/destroyed sockets.
  480. * When socket is dead & no packets have been sent for a
  481. * certain amount of time, they are removed by this
  482. * routine. Also takes care of sending out DI & DC
  483. * frames at correct times.
  484. */
  485. int dn_destroy_timer(struct sock *sk)
  486. {
  487. struct dn_scp *scp = DN_SK(sk);
  488. scp->persist = dn_nsp_persist(sk);
  489. switch (scp->state) {
  490. case DN_DI:
  491. dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC);
  492. if (scp->nsp_rxtshift >= decnet_di_count)
  493. scp->state = DN_CN;
  494. return 0;
  495. case DN_DR:
  496. dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC);
  497. if (scp->nsp_rxtshift >= decnet_dr_count)
  498. scp->state = DN_DRC;
  499. return 0;
  500. case DN_DN:
  501. if (scp->nsp_rxtshift < decnet_dn_count) {
  502. /* printk(KERN_DEBUG "dn_destroy_timer: DN\n"); */
  503. dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC,
  504. GFP_ATOMIC);
  505. return 0;
  506. }
  507. }
  508. scp->persist = (HZ * decnet_time_wait);
  509. if (sk->sk_socket)
  510. return 0;
  511. if ((jiffies - scp->stamp) >= (HZ * decnet_time_wait)) {
  512. dn_unhash_sock(sk);
  513. sock_put(sk);
  514. return 1;
  515. }
  516. return 0;
  517. }
  518. static void dn_destroy_sock(struct sock *sk)
  519. {
  520. struct dn_scp *scp = DN_SK(sk);
  521. scp->nsp_rxtshift = 0; /* reset back off */
  522. if (sk->sk_socket) {
  523. if (sk->sk_socket->state != SS_UNCONNECTED)
  524. sk->sk_socket->state = SS_DISCONNECTING;
  525. }
  526. sk->sk_state = TCP_CLOSE;
  527. switch (scp->state) {
  528. case DN_DN:
  529. dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC,
  530. sk->sk_allocation);
  531. scp->persist_fxn = dn_destroy_timer;
  532. scp->persist = dn_nsp_persist(sk);
  533. break;
  534. case DN_CR:
  535. scp->state = DN_DR;
  536. goto disc_reject;
  537. case DN_RUN:
  538. scp->state = DN_DI;
  539. case DN_DI:
  540. case DN_DR:
  541. disc_reject:
  542. dn_nsp_send_disc(sk, NSP_DISCINIT, 0, sk->sk_allocation);
  543. case DN_NC:
  544. case DN_NR:
  545. case DN_RJ:
  546. case DN_DIC:
  547. case DN_CN:
  548. case DN_DRC:
  549. case DN_CI:
  550. case DN_CD:
  551. scp->persist_fxn = dn_destroy_timer;
  552. scp->persist = dn_nsp_persist(sk);
  553. break;
  554. default:
  555. printk(KERN_DEBUG "DECnet: dn_destroy_sock passed socket in invalid state\n");
  556. case DN_O:
  557. dn_stop_slow_timer(sk);
  558. dn_unhash_sock_bh(sk);
  559. sock_put(sk);
  560. break;
  561. }
  562. }
  563. char *dn_addr2asc(__u16 addr, char *buf)
  564. {
  565. unsigned short node, area;
  566. node = addr & 0x03ff;
  567. area = addr >> 10;
  568. sprintf(buf, "%hd.%hd", area, node);
  569. return buf;
  570. }
  571. static int dn_create(struct net *net, struct socket *sock, int protocol,
  572. int kern)
  573. {
  574. struct sock *sk;
  575. if (protocol < 0 || protocol > SK_PROTOCOL_MAX)
  576. return -EINVAL;
  577. if (!net_eq(net, &init_net))
  578. return -EAFNOSUPPORT;
  579. switch (sock->type) {
  580. case SOCK_SEQPACKET:
  581. if (protocol != DNPROTO_NSP)
  582. return -EPROTONOSUPPORT;
  583. break;
  584. case SOCK_STREAM:
  585. break;
  586. default:
  587. return -ESOCKTNOSUPPORT;
  588. }
  589. if ((sk = dn_alloc_sock(net, sock, GFP_KERNEL)) == NULL)
  590. return -ENOBUFS;
  591. sk->sk_protocol = protocol;
  592. return 0;
  593. }
  594. static int
  595. dn_release(struct socket *sock)
  596. {
  597. struct sock *sk = sock->sk;
  598. if (sk) {
  599. sock_orphan(sk);
  600. sock_hold(sk);
  601. lock_sock(sk);
  602. dn_destroy_sock(sk);
  603. release_sock(sk);
  604. sock_put(sk);
  605. }
  606. return 0;
  607. }
  608. static int dn_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  609. {
  610. struct sock *sk = sock->sk;
  611. struct dn_scp *scp = DN_SK(sk);
  612. struct sockaddr_dn *saddr = (struct sockaddr_dn *)uaddr;
  613. struct net_device *dev, *ldev;
  614. int rv;
  615. if (addr_len != sizeof(struct sockaddr_dn))
  616. return -EINVAL;
  617. if (saddr->sdn_family != AF_DECnet)
  618. return -EINVAL;
  619. if (le16_to_cpu(saddr->sdn_nodeaddrl) && (le16_to_cpu(saddr->sdn_nodeaddrl) != 2))
  620. return -EINVAL;
  621. if (le16_to_cpu(saddr->sdn_objnamel) > DN_MAXOBJL)
  622. return -EINVAL;
  623. if (saddr->sdn_flags & ~SDF_WILD)
  624. return -EINVAL;
  625. if (!capable(CAP_NET_BIND_SERVICE) && (saddr->sdn_objnum ||
  626. (saddr->sdn_flags & SDF_WILD)))
  627. return -EACCES;
  628. if (!(saddr->sdn_flags & SDF_WILD)) {
  629. if (le16_to_cpu(saddr->sdn_nodeaddrl)) {
  630. rcu_read_lock();
  631. ldev = NULL;
  632. for_each_netdev_rcu(&init_net, dev) {
  633. if (!dev->dn_ptr)
  634. continue;
  635. if (dn_dev_islocal(dev, dn_saddr2dn(saddr))) {
  636. ldev = dev;
  637. break;
  638. }
  639. }
  640. rcu_read_unlock();
  641. if (ldev == NULL)
  642. return -EADDRNOTAVAIL;
  643. }
  644. }
  645. rv = -EINVAL;
  646. lock_sock(sk);
  647. if (sock_flag(sk, SOCK_ZAPPED)) {
  648. memcpy(&scp->addr, saddr, addr_len);
  649. sock_reset_flag(sk, SOCK_ZAPPED);
  650. rv = dn_hash_sock(sk);
  651. if (rv)
  652. sock_set_flag(sk, SOCK_ZAPPED);
  653. }
  654. release_sock(sk);
  655. return rv;
  656. }
  657. static int dn_auto_bind(struct socket *sock)
  658. {
  659. struct sock *sk = sock->sk;
  660. struct dn_scp *scp = DN_SK(sk);
  661. int rv;
  662. sock_reset_flag(sk, SOCK_ZAPPED);
  663. scp->addr.sdn_flags = 0;
  664. scp->addr.sdn_objnum = 0;
  665. /*
  666. * This stuff is to keep compatibility with Eduardo's
  667. * patch. I hope I can dispense with it shortly...
  668. */
  669. if ((scp->accessdata.acc_accl != 0) &&
  670. (scp->accessdata.acc_accl <= 12)) {
  671. scp->addr.sdn_objnamel = cpu_to_le16(scp->accessdata.acc_accl);
  672. memcpy(scp->addr.sdn_objname, scp->accessdata.acc_acc, le16_to_cpu(scp->addr.sdn_objnamel));
  673. scp->accessdata.acc_accl = 0;
  674. memset(scp->accessdata.acc_acc, 0, 40);
  675. }
  676. /* End of compatibility stuff */
  677. scp->addr.sdn_add.a_len = cpu_to_le16(2);
  678. rv = dn_dev_bind_default((__le16 *)scp->addr.sdn_add.a_addr);
  679. if (rv == 0) {
  680. rv = dn_hash_sock(sk);
  681. if (rv)
  682. sock_set_flag(sk, SOCK_ZAPPED);
  683. }
  684. return rv;
  685. }
  686. static int dn_confirm_accept(struct sock *sk, long *timeo, gfp_t allocation)
  687. {
  688. struct dn_scp *scp = DN_SK(sk);
  689. DEFINE_WAIT(wait);
  690. int err;
  691. if (scp->state != DN_CR)
  692. return -EINVAL;
  693. scp->state = DN_CC;
  694. scp->segsize_loc = dst_metric_advmss(__sk_dst_get(sk));
  695. dn_send_conn_conf(sk, allocation);
  696. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  697. for(;;) {
  698. release_sock(sk);
  699. if (scp->state == DN_CC)
  700. *timeo = schedule_timeout(*timeo);
  701. lock_sock(sk);
  702. err = 0;
  703. if (scp->state == DN_RUN)
  704. break;
  705. err = sock_error(sk);
  706. if (err)
  707. break;
  708. err = sock_intr_errno(*timeo);
  709. if (signal_pending(current))
  710. break;
  711. err = -EAGAIN;
  712. if (!*timeo)
  713. break;
  714. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  715. }
  716. finish_wait(sk_sleep(sk), &wait);
  717. if (err == 0) {
  718. sk->sk_socket->state = SS_CONNECTED;
  719. } else if (scp->state != DN_CC) {
  720. sk->sk_socket->state = SS_UNCONNECTED;
  721. }
  722. return err;
  723. }
  724. static int dn_wait_run(struct sock *sk, long *timeo)
  725. {
  726. struct dn_scp *scp = DN_SK(sk);
  727. DEFINE_WAIT(wait);
  728. int err = 0;
  729. if (scp->state == DN_RUN)
  730. goto out;
  731. if (!*timeo)
  732. return -EALREADY;
  733. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  734. for(;;) {
  735. release_sock(sk);
  736. if (scp->state == DN_CI || scp->state == DN_CC)
  737. *timeo = schedule_timeout(*timeo);
  738. lock_sock(sk);
  739. err = 0;
  740. if (scp->state == DN_RUN)
  741. break;
  742. err = sock_error(sk);
  743. if (err)
  744. break;
  745. err = sock_intr_errno(*timeo);
  746. if (signal_pending(current))
  747. break;
  748. err = -ETIMEDOUT;
  749. if (!*timeo)
  750. break;
  751. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  752. }
  753. finish_wait(sk_sleep(sk), &wait);
  754. out:
  755. if (err == 0) {
  756. sk->sk_socket->state = SS_CONNECTED;
  757. } else if (scp->state != DN_CI && scp->state != DN_CC) {
  758. sk->sk_socket->state = SS_UNCONNECTED;
  759. }
  760. return err;
  761. }
  762. static int __dn_connect(struct sock *sk, struct sockaddr_dn *addr, int addrlen, long *timeo, int flags)
  763. {
  764. struct socket *sock = sk->sk_socket;
  765. struct dn_scp *scp = DN_SK(sk);
  766. int err = -EISCONN;
  767. struct flowidn fld;
  768. if (sock->state == SS_CONNECTED)
  769. goto out;
  770. if (sock->state == SS_CONNECTING) {
  771. err = 0;
  772. if (scp->state == DN_RUN) {
  773. sock->state = SS_CONNECTED;
  774. goto out;
  775. }
  776. err = -ECONNREFUSED;
  777. if (scp->state != DN_CI && scp->state != DN_CC) {
  778. sock->state = SS_UNCONNECTED;
  779. goto out;
  780. }
  781. return dn_wait_run(sk, timeo);
  782. }
  783. err = -EINVAL;
  784. if (scp->state != DN_O)
  785. goto out;
  786. if (addr == NULL || addrlen != sizeof(struct sockaddr_dn))
  787. goto out;
  788. if (addr->sdn_family != AF_DECnet)
  789. goto out;
  790. if (addr->sdn_flags & SDF_WILD)
  791. goto out;
  792. if (sock_flag(sk, SOCK_ZAPPED)) {
  793. err = dn_auto_bind(sk->sk_socket);
  794. if (err)
  795. goto out;
  796. }
  797. memcpy(&scp->peer, addr, sizeof(struct sockaddr_dn));
  798. err = -EHOSTUNREACH;
  799. memset(&fld, 0, sizeof(fld));
  800. fld.flowidn_oif = sk->sk_bound_dev_if;
  801. fld.daddr = dn_saddr2dn(&scp->peer);
  802. fld.saddr = dn_saddr2dn(&scp->addr);
  803. dn_sk_ports_copy(&fld, scp);
  804. fld.flowidn_proto = DNPROTO_NSP;
  805. if (dn_route_output_sock(&sk->sk_dst_cache, &fld, sk, flags) < 0)
  806. goto out;
  807. sk->sk_route_caps = sk->sk_dst_cache->dev->features;
  808. sock->state = SS_CONNECTING;
  809. scp->state = DN_CI;
  810. scp->segsize_loc = dst_metric_advmss(sk->sk_dst_cache);
  811. dn_nsp_send_conninit(sk, NSP_CI);
  812. err = -EINPROGRESS;
  813. if (*timeo) {
  814. err = dn_wait_run(sk, timeo);
  815. }
  816. out:
  817. return err;
  818. }
  819. static int dn_connect(struct socket *sock, struct sockaddr *uaddr, int addrlen, int flags)
  820. {
  821. struct sockaddr_dn *addr = (struct sockaddr_dn *)uaddr;
  822. struct sock *sk = sock->sk;
  823. int err;
  824. long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  825. lock_sock(sk);
  826. err = __dn_connect(sk, addr, addrlen, &timeo, 0);
  827. release_sock(sk);
  828. return err;
  829. }
  830. static inline int dn_check_state(struct sock *sk, struct sockaddr_dn *addr, int addrlen, long *timeo, int flags)
  831. {
  832. struct dn_scp *scp = DN_SK(sk);
  833. switch (scp->state) {
  834. case DN_RUN:
  835. return 0;
  836. case DN_CR:
  837. return dn_confirm_accept(sk, timeo, sk->sk_allocation);
  838. case DN_CI:
  839. case DN_CC:
  840. return dn_wait_run(sk, timeo);
  841. case DN_O:
  842. return __dn_connect(sk, addr, addrlen, timeo, flags);
  843. }
  844. return -EINVAL;
  845. }
  846. static void dn_access_copy(struct sk_buff *skb, struct accessdata_dn *acc)
  847. {
  848. unsigned char *ptr = skb->data;
  849. acc->acc_userl = *ptr++;
  850. memcpy(&acc->acc_user, ptr, acc->acc_userl);
  851. ptr += acc->acc_userl;
  852. acc->acc_passl = *ptr++;
  853. memcpy(&acc->acc_pass, ptr, acc->acc_passl);
  854. ptr += acc->acc_passl;
  855. acc->acc_accl = *ptr++;
  856. memcpy(&acc->acc_acc, ptr, acc->acc_accl);
  857. skb_pull(skb, acc->acc_accl + acc->acc_passl + acc->acc_userl + 3);
  858. }
  859. static void dn_user_copy(struct sk_buff *skb, struct optdata_dn *opt)
  860. {
  861. unsigned char *ptr = skb->data;
  862. u16 len = *ptr++; /* yes, it's 8bit on the wire */
  863. BUG_ON(len > 16); /* we've checked the contents earlier */
  864. opt->opt_optl = cpu_to_le16(len);
  865. opt->opt_status = 0;
  866. memcpy(opt->opt_data, ptr, len);
  867. skb_pull(skb, len + 1);
  868. }
  869. static struct sk_buff *dn_wait_for_connect(struct sock *sk, long *timeo)
  870. {
  871. DEFINE_WAIT(wait);
  872. struct sk_buff *skb = NULL;
  873. int err = 0;
  874. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  875. for(;;) {
  876. release_sock(sk);
  877. skb = skb_dequeue(&sk->sk_receive_queue);
  878. if (skb == NULL) {
  879. *timeo = schedule_timeout(*timeo);
  880. skb = skb_dequeue(&sk->sk_receive_queue);
  881. }
  882. lock_sock(sk);
  883. if (skb != NULL)
  884. break;
  885. err = -EINVAL;
  886. if (sk->sk_state != TCP_LISTEN)
  887. break;
  888. err = sock_intr_errno(*timeo);
  889. if (signal_pending(current))
  890. break;
  891. err = -EAGAIN;
  892. if (!*timeo)
  893. break;
  894. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  895. }
  896. finish_wait(sk_sleep(sk), &wait);
  897. return skb == NULL ? ERR_PTR(err) : skb;
  898. }
  899. static int dn_accept(struct socket *sock, struct socket *newsock, int flags)
  900. {
  901. struct sock *sk = sock->sk, *newsk;
  902. struct sk_buff *skb = NULL;
  903. struct dn_skb_cb *cb;
  904. unsigned char menuver;
  905. int err = 0;
  906. unsigned char type;
  907. long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  908. struct dst_entry *dst;
  909. lock_sock(sk);
  910. if (sk->sk_state != TCP_LISTEN || DN_SK(sk)->state != DN_O) {
  911. release_sock(sk);
  912. return -EINVAL;
  913. }
  914. skb = skb_dequeue(&sk->sk_receive_queue);
  915. if (skb == NULL) {
  916. skb = dn_wait_for_connect(sk, &timeo);
  917. if (IS_ERR(skb)) {
  918. release_sock(sk);
  919. return PTR_ERR(skb);
  920. }
  921. }
  922. cb = DN_SKB_CB(skb);
  923. sk->sk_ack_backlog--;
  924. newsk = dn_alloc_sock(sock_net(sk), newsock, sk->sk_allocation);
  925. if (newsk == NULL) {
  926. release_sock(sk);
  927. kfree_skb(skb);
  928. return -ENOBUFS;
  929. }
  930. release_sock(sk);
  931. dst = skb_dst(skb);
  932. sk_dst_set(newsk, dst);
  933. skb_dst_set(skb, NULL);
  934. DN_SK(newsk)->state = DN_CR;
  935. DN_SK(newsk)->addrrem = cb->src_port;
  936. DN_SK(newsk)->services_rem = cb->services;
  937. DN_SK(newsk)->info_rem = cb->info;
  938. DN_SK(newsk)->segsize_rem = cb->segsize;
  939. DN_SK(newsk)->accept_mode = DN_SK(sk)->accept_mode;
  940. if (DN_SK(newsk)->segsize_rem < 230)
  941. DN_SK(newsk)->segsize_rem = 230;
  942. if ((DN_SK(newsk)->services_rem & NSP_FC_MASK) == NSP_FC_NONE)
  943. DN_SK(newsk)->max_window = decnet_no_fc_max_cwnd;
  944. newsk->sk_state = TCP_LISTEN;
  945. memcpy(&(DN_SK(newsk)->addr), &(DN_SK(sk)->addr), sizeof(struct sockaddr_dn));
  946. /*
  947. * If we are listening on a wild socket, we don't want
  948. * the newly created socket on the wrong hash queue.
  949. */
  950. DN_SK(newsk)->addr.sdn_flags &= ~SDF_WILD;
  951. skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->addr), &type));
  952. skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->peer), &type));
  953. *(__le16 *)(DN_SK(newsk)->peer.sdn_add.a_addr) = cb->src;
  954. *(__le16 *)(DN_SK(newsk)->addr.sdn_add.a_addr) = cb->dst;
  955. menuver = *skb->data;
  956. skb_pull(skb, 1);
  957. if (menuver & DN_MENUVER_ACC)
  958. dn_access_copy(skb, &(DN_SK(newsk)->accessdata));
  959. if (menuver & DN_MENUVER_USR)
  960. dn_user_copy(skb, &(DN_SK(newsk)->conndata_in));
  961. if (menuver & DN_MENUVER_PRX)
  962. DN_SK(newsk)->peer.sdn_flags |= SDF_PROXY;
  963. if (menuver & DN_MENUVER_UIC)
  964. DN_SK(newsk)->peer.sdn_flags |= SDF_UICPROXY;
  965. kfree_skb(skb);
  966. memcpy(&(DN_SK(newsk)->conndata_out), &(DN_SK(sk)->conndata_out),
  967. sizeof(struct optdata_dn));
  968. memcpy(&(DN_SK(newsk)->discdata_out), &(DN_SK(sk)->discdata_out),
  969. sizeof(struct optdata_dn));
  970. lock_sock(newsk);
  971. err = dn_hash_sock(newsk);
  972. if (err == 0) {
  973. sock_reset_flag(newsk, SOCK_ZAPPED);
  974. dn_send_conn_ack(newsk);
  975. /*
  976. * Here we use sk->sk_allocation since although the conn conf is
  977. * for the newsk, the context is the old socket.
  978. */
  979. if (DN_SK(newsk)->accept_mode == ACC_IMMED)
  980. err = dn_confirm_accept(newsk, &timeo,
  981. sk->sk_allocation);
  982. }
  983. release_sock(newsk);
  984. return err;
  985. }
  986. static int dn_getname(struct socket *sock, struct sockaddr *uaddr,int *uaddr_len,int peer)
  987. {
  988. struct sockaddr_dn *sa = (struct sockaddr_dn *)uaddr;
  989. struct sock *sk = sock->sk;
  990. struct dn_scp *scp = DN_SK(sk);
  991. *uaddr_len = sizeof(struct sockaddr_dn);
  992. lock_sock(sk);
  993. if (peer) {
  994. if ((sock->state != SS_CONNECTED &&
  995. sock->state != SS_CONNECTING) &&
  996. scp->accept_mode == ACC_IMMED) {
  997. release_sock(sk);
  998. return -ENOTCONN;
  999. }
  1000. memcpy(sa, &scp->peer, sizeof(struct sockaddr_dn));
  1001. } else {
  1002. memcpy(sa, &scp->addr, sizeof(struct sockaddr_dn));
  1003. }
  1004. release_sock(sk);
  1005. return 0;
  1006. }
  1007. static unsigned int dn_poll(struct file *file, struct socket *sock, poll_table *wait)
  1008. {
  1009. struct sock *sk = sock->sk;
  1010. struct dn_scp *scp = DN_SK(sk);
  1011. int mask = datagram_poll(file, sock, wait);
  1012. if (!skb_queue_empty(&scp->other_receive_queue))
  1013. mask |= POLLRDBAND;
  1014. return mask;
  1015. }
  1016. static int dn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  1017. {
  1018. struct sock *sk = sock->sk;
  1019. struct dn_scp *scp = DN_SK(sk);
  1020. int err = -EOPNOTSUPP;
  1021. long amount = 0;
  1022. struct sk_buff *skb;
  1023. int val;
  1024. switch(cmd)
  1025. {
  1026. case SIOCGIFADDR:
  1027. case SIOCSIFADDR:
  1028. return dn_dev_ioctl(cmd, (void __user *)arg);
  1029. case SIOCATMARK:
  1030. lock_sock(sk);
  1031. val = !skb_queue_empty(&scp->other_receive_queue);
  1032. if (scp->state != DN_RUN)
  1033. val = -ENOTCONN;
  1034. release_sock(sk);
  1035. return val;
  1036. case TIOCOUTQ:
  1037. amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
  1038. if (amount < 0)
  1039. amount = 0;
  1040. err = put_user(amount, (int __user *)arg);
  1041. break;
  1042. case TIOCINQ:
  1043. lock_sock(sk);
  1044. skb = skb_peek(&scp->other_receive_queue);
  1045. if (skb) {
  1046. amount = skb->len;
  1047. } else {
  1048. skb_queue_walk(&sk->sk_receive_queue, skb)
  1049. amount += skb->len;
  1050. }
  1051. release_sock(sk);
  1052. err = put_user(amount, (int __user *)arg);
  1053. break;
  1054. default:
  1055. err = -ENOIOCTLCMD;
  1056. break;
  1057. }
  1058. return err;
  1059. }
  1060. static int dn_listen(struct socket *sock, int backlog)
  1061. {
  1062. struct sock *sk = sock->sk;
  1063. int err = -EINVAL;
  1064. lock_sock(sk);
  1065. if (sock_flag(sk, SOCK_ZAPPED))
  1066. goto out;
  1067. if ((DN_SK(sk)->state != DN_O) || (sk->sk_state == TCP_LISTEN))
  1068. goto out;
  1069. sk->sk_max_ack_backlog = backlog;
  1070. sk->sk_ack_backlog = 0;
  1071. sk->sk_state = TCP_LISTEN;
  1072. err = 0;
  1073. dn_rehash_sock(sk);
  1074. out:
  1075. release_sock(sk);
  1076. return err;
  1077. }
  1078. static int dn_shutdown(struct socket *sock, int how)
  1079. {
  1080. struct sock *sk = sock->sk;
  1081. struct dn_scp *scp = DN_SK(sk);
  1082. int err = -ENOTCONN;
  1083. lock_sock(sk);
  1084. if (sock->state == SS_UNCONNECTED)
  1085. goto out;
  1086. err = 0;
  1087. if (sock->state == SS_DISCONNECTING)
  1088. goto out;
  1089. err = -EINVAL;
  1090. if (scp->state == DN_O)
  1091. goto out;
  1092. if (how != SHUTDOWN_MASK)
  1093. goto out;
  1094. sk->sk_shutdown = how;
  1095. dn_destroy_sock(sk);
  1096. err = 0;
  1097. out:
  1098. release_sock(sk);
  1099. return err;
  1100. }
  1101. static int dn_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
  1102. {
  1103. struct sock *sk = sock->sk;
  1104. int err;
  1105. lock_sock(sk);
  1106. err = __dn_setsockopt(sock, level, optname, optval, optlen, 0);
  1107. release_sock(sk);
  1108. return err;
  1109. }
  1110. static int __dn_setsockopt(struct socket *sock, int level,int optname, char __user *optval, unsigned int optlen, int flags)
  1111. {
  1112. struct sock *sk = sock->sk;
  1113. struct dn_scp *scp = DN_SK(sk);
  1114. long timeo;
  1115. union {
  1116. struct optdata_dn opt;
  1117. struct accessdata_dn acc;
  1118. int mode;
  1119. unsigned long win;
  1120. int val;
  1121. unsigned char services;
  1122. unsigned char info;
  1123. } u;
  1124. int err;
  1125. if (optlen && !optval)
  1126. return -EINVAL;
  1127. if (optlen > sizeof(u))
  1128. return -EINVAL;
  1129. if (copy_from_user(&u, optval, optlen))
  1130. return -EFAULT;
  1131. switch (optname) {
  1132. case DSO_CONDATA:
  1133. if (sock->state == SS_CONNECTED)
  1134. return -EISCONN;
  1135. if ((scp->state != DN_O) && (scp->state != DN_CR))
  1136. return -EINVAL;
  1137. if (optlen != sizeof(struct optdata_dn))
  1138. return -EINVAL;
  1139. if (le16_to_cpu(u.opt.opt_optl) > 16)
  1140. return -EINVAL;
  1141. memcpy(&scp->conndata_out, &u.opt, optlen);
  1142. break;
  1143. case DSO_DISDATA:
  1144. if (sock->state != SS_CONNECTED &&
  1145. scp->accept_mode == ACC_IMMED)
  1146. return -ENOTCONN;
  1147. if (optlen != sizeof(struct optdata_dn))
  1148. return -EINVAL;
  1149. if (le16_to_cpu(u.opt.opt_optl) > 16)
  1150. return -EINVAL;
  1151. memcpy(&scp->discdata_out, &u.opt, optlen);
  1152. break;
  1153. case DSO_CONACCESS:
  1154. if (sock->state == SS_CONNECTED)
  1155. return -EISCONN;
  1156. if (scp->state != DN_O)
  1157. return -EINVAL;
  1158. if (optlen != sizeof(struct accessdata_dn))
  1159. return -EINVAL;
  1160. if ((u.acc.acc_accl > DN_MAXACCL) ||
  1161. (u.acc.acc_passl > DN_MAXACCL) ||
  1162. (u.acc.acc_userl > DN_MAXACCL))
  1163. return -EINVAL;
  1164. memcpy(&scp->accessdata, &u.acc, optlen);
  1165. break;
  1166. case DSO_ACCEPTMODE:
  1167. if (sock->state == SS_CONNECTED)
  1168. return -EISCONN;
  1169. if (scp->state != DN_O)
  1170. return -EINVAL;
  1171. if (optlen != sizeof(int))
  1172. return -EINVAL;
  1173. if ((u.mode != ACC_IMMED) && (u.mode != ACC_DEFER))
  1174. return -EINVAL;
  1175. scp->accept_mode = (unsigned char)u.mode;
  1176. break;
  1177. case DSO_CONACCEPT:
  1178. if (scp->state != DN_CR)
  1179. return -EINVAL;
  1180. timeo = sock_rcvtimeo(sk, 0);
  1181. err = dn_confirm_accept(sk, &timeo, sk->sk_allocation);
  1182. return err;
  1183. case DSO_CONREJECT:
  1184. if (scp->state != DN_CR)
  1185. return -EINVAL;
  1186. scp->state = DN_DR;
  1187. sk->sk_shutdown = SHUTDOWN_MASK;
  1188. dn_nsp_send_disc(sk, 0x38, 0, sk->sk_allocation);
  1189. break;
  1190. default:
  1191. #ifdef CONFIG_NETFILTER
  1192. return nf_setsockopt(sk, PF_DECnet, optname, optval, optlen);
  1193. #endif
  1194. case DSO_LINKINFO:
  1195. case DSO_STREAM:
  1196. case DSO_SEQPACKET:
  1197. return -ENOPROTOOPT;
  1198. case DSO_MAXWINDOW:
  1199. if (optlen != sizeof(unsigned long))
  1200. return -EINVAL;
  1201. if (u.win > NSP_MAX_WINDOW)
  1202. u.win = NSP_MAX_WINDOW;
  1203. if (u.win == 0)
  1204. return -EINVAL;
  1205. scp->max_window = u.win;
  1206. if (scp->snd_window > u.win)
  1207. scp->snd_window = u.win;
  1208. break;
  1209. case DSO_NODELAY:
  1210. if (optlen != sizeof(int))
  1211. return -EINVAL;
  1212. if (scp->nonagle == 2)
  1213. return -EINVAL;
  1214. scp->nonagle = (u.val == 0) ? 0 : 1;
  1215. /* if (scp->nonagle == 1) { Push pending frames } */
  1216. break;
  1217. case DSO_CORK:
  1218. if (optlen != sizeof(int))
  1219. return -EINVAL;
  1220. if (scp->nonagle == 1)
  1221. return -EINVAL;
  1222. scp->nonagle = (u.val == 0) ? 0 : 2;
  1223. /* if (scp->nonagle == 0) { Push pending frames } */
  1224. break;
  1225. case DSO_SERVICES:
  1226. if (optlen != sizeof(unsigned char))
  1227. return -EINVAL;
  1228. if ((u.services & ~NSP_FC_MASK) != 0x01)
  1229. return -EINVAL;
  1230. if ((u.services & NSP_FC_MASK) == NSP_FC_MASK)
  1231. return -EINVAL;
  1232. scp->services_loc = u.services;
  1233. break;
  1234. case DSO_INFO:
  1235. if (optlen != sizeof(unsigned char))
  1236. return -EINVAL;
  1237. if (u.info & 0xfc)
  1238. return -EINVAL;
  1239. scp->info_loc = u.info;
  1240. break;
  1241. }
  1242. return 0;
  1243. }
  1244. static int dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
  1245. {
  1246. struct sock *sk = sock->sk;
  1247. int err;
  1248. lock_sock(sk);
  1249. err = __dn_getsockopt(sock, level, optname, optval, optlen, 0);
  1250. release_sock(sk);
  1251. return err;
  1252. }
  1253. static int __dn_getsockopt(struct socket *sock, int level,int optname, char __user *optval,int __user *optlen, int flags)
  1254. {
  1255. struct sock *sk = sock->sk;
  1256. struct dn_scp *scp = DN_SK(sk);
  1257. struct linkinfo_dn link;
  1258. unsigned int r_len;
  1259. void *r_data = NULL;
  1260. unsigned int val;
  1261. if(get_user(r_len , optlen))
  1262. return -EFAULT;
  1263. switch (optname) {
  1264. case DSO_CONDATA:
  1265. if (r_len > sizeof(struct optdata_dn))
  1266. r_len = sizeof(struct optdata_dn);
  1267. r_data = &scp->conndata_in;
  1268. break;
  1269. case DSO_DISDATA:
  1270. if (r_len > sizeof(struct optdata_dn))
  1271. r_len = sizeof(struct optdata_dn);
  1272. r_data = &scp->discdata_in;
  1273. break;
  1274. case DSO_CONACCESS:
  1275. if (r_len > sizeof(struct accessdata_dn))
  1276. r_len = sizeof(struct accessdata_dn);
  1277. r_data = &scp->accessdata;
  1278. break;
  1279. case DSO_ACCEPTMODE:
  1280. if (r_len > sizeof(unsigned char))
  1281. r_len = sizeof(unsigned char);
  1282. r_data = &scp->accept_mode;
  1283. break;
  1284. case DSO_LINKINFO:
  1285. if (r_len > sizeof(struct linkinfo_dn))
  1286. r_len = sizeof(struct linkinfo_dn);
  1287. memset(&link, 0, sizeof(link));
  1288. switch (sock->state) {
  1289. case SS_CONNECTING:
  1290. link.idn_linkstate = LL_CONNECTING;
  1291. break;
  1292. case SS_DISCONNECTING:
  1293. link.idn_linkstate = LL_DISCONNECTING;
  1294. break;
  1295. case SS_CONNECTED:
  1296. link.idn_linkstate = LL_RUNNING;
  1297. break;
  1298. default:
  1299. link.idn_linkstate = LL_INACTIVE;
  1300. }
  1301. link.idn_segsize = scp->segsize_rem;
  1302. r_data = &link;
  1303. break;
  1304. default:
  1305. #ifdef CONFIG_NETFILTER
  1306. {
  1307. int ret, len;
  1308. if (get_user(len, optlen))
  1309. return -EFAULT;
  1310. ret = nf_getsockopt(sk, PF_DECnet, optname, optval, &len);
  1311. if (ret >= 0)
  1312. ret = put_user(len, optlen);
  1313. return ret;
  1314. }
  1315. #endif
  1316. case DSO_STREAM:
  1317. case DSO_SEQPACKET:
  1318. case DSO_CONACCEPT:
  1319. case DSO_CONREJECT:
  1320. return -ENOPROTOOPT;
  1321. case DSO_MAXWINDOW:
  1322. if (r_len > sizeof(unsigned long))
  1323. r_len = sizeof(unsigned long);
  1324. r_data = &scp->max_window;
  1325. break;
  1326. case DSO_NODELAY:
  1327. if (r_len > sizeof(int))
  1328. r_len = sizeof(int);
  1329. val = (scp->nonagle == 1);
  1330. r_data = &val;
  1331. break;
  1332. case DSO_CORK:
  1333. if (r_len > sizeof(int))
  1334. r_len = sizeof(int);
  1335. val = (scp->nonagle == 2);
  1336. r_data = &val;
  1337. break;
  1338. case DSO_SERVICES:
  1339. if (r_len > sizeof(unsigned char))
  1340. r_len = sizeof(unsigned char);
  1341. r_data = &scp->services_rem;
  1342. break;
  1343. case DSO_INFO:
  1344. if (r_len > sizeof(unsigned char))
  1345. r_len = sizeof(unsigned char);
  1346. r_data = &scp->info_rem;
  1347. break;
  1348. }
  1349. if (r_data) {
  1350. if (copy_to_user(optval, r_data, r_len))
  1351. return -EFAULT;
  1352. if (put_user(r_len, optlen))
  1353. return -EFAULT;
  1354. }
  1355. return 0;
  1356. }
  1357. static int dn_data_ready(struct sock *sk, struct sk_buff_head *q, int flags, int target)
  1358. {
  1359. struct sk_buff *skb;
  1360. int len = 0;
  1361. if (flags & MSG_OOB)
  1362. return !skb_queue_empty(q) ? 1 : 0;
  1363. skb_queue_walk(q, skb) {
  1364. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  1365. len += skb->len;
  1366. if (cb->nsp_flags & 0x40) {
  1367. /* SOCK_SEQPACKET reads to EOM */
  1368. if (sk->sk_type == SOCK_SEQPACKET)
  1369. return 1;
  1370. /* so does SOCK_STREAM unless WAITALL is specified */
  1371. if (!(flags & MSG_WAITALL))
  1372. return 1;
  1373. }
  1374. /* minimum data length for read exceeded */
  1375. if (len >= target)
  1376. return 1;
  1377. }
  1378. return 0;
  1379. }
  1380. static int dn_recvmsg(struct kiocb *iocb, struct socket *sock,
  1381. struct msghdr *msg, size_t size, int flags)
  1382. {
  1383. struct sock *sk = sock->sk;
  1384. struct dn_scp *scp = DN_SK(sk);
  1385. struct sk_buff_head *queue = &sk->sk_receive_queue;
  1386. size_t target = size > 1 ? 1 : 0;
  1387. size_t copied = 0;
  1388. int rv = 0;
  1389. struct sk_buff *skb, *n;
  1390. struct dn_skb_cb *cb = NULL;
  1391. unsigned char eor = 0;
  1392. long timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1393. lock_sock(sk);
  1394. if (sock_flag(sk, SOCK_ZAPPED)) {
  1395. rv = -EADDRNOTAVAIL;
  1396. goto out;
  1397. }
  1398. if (sk->sk_shutdown & RCV_SHUTDOWN) {
  1399. rv = 0;
  1400. goto out;
  1401. }
  1402. rv = dn_check_state(sk, NULL, 0, &timeo, flags);
  1403. if (rv)
  1404. goto out;
  1405. if (flags & ~(MSG_CMSG_COMPAT|MSG_PEEK|MSG_OOB|MSG_WAITALL|MSG_DONTWAIT|MSG_NOSIGNAL)) {
  1406. rv = -EOPNOTSUPP;
  1407. goto out;
  1408. }
  1409. if (flags & MSG_OOB)
  1410. queue = &scp->other_receive_queue;
  1411. if (flags & MSG_WAITALL)
  1412. target = size;
  1413. /*
  1414. * See if there is data ready to read, sleep if there isn't
  1415. */
  1416. for(;;) {
  1417. DEFINE_WAIT(wait);
  1418. if (sk->sk_err)
  1419. goto out;
  1420. if (!skb_queue_empty(&scp->other_receive_queue)) {
  1421. if (!(flags & MSG_OOB)) {
  1422. msg->msg_flags |= MSG_OOB;
  1423. if (!scp->other_report) {
  1424. scp->other_report = 1;
  1425. goto out;
  1426. }
  1427. }
  1428. }
  1429. if (scp->state != DN_RUN)
  1430. goto out;
  1431. if (signal_pending(current)) {
  1432. rv = sock_intr_errno(timeo);
  1433. goto out;
  1434. }
  1435. if (dn_data_ready(sk, queue, flags, target))
  1436. break;
  1437. if (flags & MSG_DONTWAIT) {
  1438. rv = -EWOULDBLOCK;
  1439. goto out;
  1440. }
  1441. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1442. set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  1443. sk_wait_event(sk, &timeo, dn_data_ready(sk, queue, flags, target));
  1444. clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  1445. finish_wait(sk_sleep(sk), &wait);
  1446. }
  1447. skb_queue_walk_safe(queue, skb, n) {
  1448. unsigned int chunk = skb->len;
  1449. cb = DN_SKB_CB(skb);
  1450. if ((chunk + copied) > size)
  1451. chunk = size - copied;
  1452. if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
  1453. rv = -EFAULT;
  1454. break;
  1455. }
  1456. copied += chunk;
  1457. if (!(flags & MSG_PEEK))
  1458. skb_pull(skb, chunk);
  1459. eor = cb->nsp_flags & 0x40;
  1460. if (skb->len == 0) {
  1461. skb_unlink(skb, queue);
  1462. kfree_skb(skb);
  1463. /*
  1464. * N.B. Don't refer to skb or cb after this point
  1465. * in loop.
  1466. */
  1467. if ((scp->flowloc_sw == DN_DONTSEND) && !dn_congested(sk)) {
  1468. scp->flowloc_sw = DN_SEND;
  1469. dn_nsp_send_link(sk, DN_SEND, 0);
  1470. }
  1471. }
  1472. if (eor) {
  1473. if (sk->sk_type == SOCK_SEQPACKET)
  1474. break;
  1475. if (!(flags & MSG_WAITALL))
  1476. break;
  1477. }
  1478. if (flags & MSG_OOB)
  1479. break;
  1480. if (copied >= target)
  1481. break;
  1482. }
  1483. rv = copied;
  1484. if (eor && (sk->sk_type == SOCK_SEQPACKET))
  1485. msg->msg_flags |= MSG_EOR;
  1486. out:
  1487. if (rv == 0)
  1488. rv = (flags & MSG_PEEK) ? -sk->sk_err : sock_error(sk);
  1489. if ((rv >= 0) && msg->msg_name) {
  1490. memcpy(msg->msg_name, &scp->peer, sizeof(struct sockaddr_dn));
  1491. msg->msg_namelen = sizeof(struct sockaddr_dn);
  1492. }
  1493. release_sock(sk);
  1494. return rv;
  1495. }
  1496. static inline int dn_queue_too_long(struct dn_scp *scp, struct sk_buff_head *queue, int flags)
  1497. {
  1498. unsigned char fctype = scp->services_rem & NSP_FC_MASK;
  1499. if (skb_queue_len(queue) >= scp->snd_window)
  1500. return 1;
  1501. if (fctype != NSP_FC_NONE) {
  1502. if (flags & MSG_OOB) {
  1503. if (scp->flowrem_oth == 0)
  1504. return 1;
  1505. } else {
  1506. if (scp->flowrem_dat == 0)
  1507. return 1;
  1508. }
  1509. }
  1510. return 0;
  1511. }
  1512. /*
  1513. * The DECnet spec requires that the "routing layer" accepts packets which
  1514. * are at least 230 bytes in size. This excludes any headers which the NSP
  1515. * layer might add, so we always assume that we'll be using the maximal
  1516. * length header on data packets. The variation in length is due to the
  1517. * inclusion (or not) of the two 16 bit acknowledgement fields so it doesn't
  1518. * make much practical difference.
  1519. */
  1520. unsigned dn_mss_from_pmtu(struct net_device *dev, int mtu)
  1521. {
  1522. unsigned mss = 230 - DN_MAX_NSP_DATA_HEADER;
  1523. if (dev) {
  1524. struct dn_dev *dn_db = rcu_dereference_raw(dev->dn_ptr);
  1525. mtu -= LL_RESERVED_SPACE(dev);
  1526. if (dn_db->use_long)
  1527. mtu -= 21;
  1528. else
  1529. mtu -= 6;
  1530. mtu -= DN_MAX_NSP_DATA_HEADER;
  1531. } else {
  1532. /*
  1533. * 21 = long header, 16 = guess at MAC header length
  1534. */
  1535. mtu -= (21 + DN_MAX_NSP_DATA_HEADER + 16);
  1536. }
  1537. if (mtu > mss)
  1538. mss = mtu;
  1539. return mss;
  1540. }
  1541. static inline unsigned int dn_current_mss(struct sock *sk, int flags)
  1542. {
  1543. struct dst_entry *dst = __sk_dst_get(sk);
  1544. struct dn_scp *scp = DN_SK(sk);
  1545. int mss_now = min_t(int, scp->segsize_loc, scp->segsize_rem);
  1546. /* Other data messages are limited to 16 bytes per packet */
  1547. if (flags & MSG_OOB)
  1548. return 16;
  1549. /* This works out the maximum size of segment we can send out */
  1550. if (dst) {
  1551. u32 mtu = dst_mtu(dst);
  1552. mss_now = min_t(int, dn_mss_from_pmtu(dst->dev, mtu), mss_now);
  1553. }
  1554. return mss_now;
  1555. }
  1556. /*
  1557. * N.B. We get the timeout wrong here, but then we always did get it
  1558. * wrong before and this is another step along the road to correcting
  1559. * it. It ought to get updated each time we pass through the routine,
  1560. * but in practise it probably doesn't matter too much for now.
  1561. */
  1562. static inline struct sk_buff *dn_alloc_send_pskb(struct sock *sk,
  1563. unsigned long datalen, int noblock,
  1564. int *errcode)
  1565. {
  1566. struct sk_buff *skb = sock_alloc_send_skb(sk, datalen,
  1567. noblock, errcode);
  1568. if (skb) {
  1569. skb->protocol = htons(ETH_P_DNA_RT);
  1570. skb->pkt_type = PACKET_OUTGOING;
  1571. }
  1572. return skb;
  1573. }
  1574. static int dn_sendmsg(struct kiocb *iocb, struct socket *sock,
  1575. struct msghdr *msg, size_t size)
  1576. {
  1577. struct sock *sk = sock->sk;
  1578. struct dn_scp *scp = DN_SK(sk);
  1579. size_t mss;
  1580. struct sk_buff_head *queue = &scp->data_xmit_queue;
  1581. int flags = msg->msg_flags;
  1582. int err = 0;
  1583. size_t sent = 0;
  1584. int addr_len = msg->msg_namelen;
  1585. struct sockaddr_dn *addr = (struct sockaddr_dn *)msg->msg_name;
  1586. struct sk_buff *skb = NULL;
  1587. struct dn_skb_cb *cb;
  1588. size_t len;
  1589. unsigned char fctype;
  1590. long timeo;
  1591. if (flags & ~(MSG_TRYHARD|MSG_OOB|MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|MSG_MORE|MSG_CMSG_COMPAT))
  1592. return -EOPNOTSUPP;
  1593. if (addr_len && (addr_len != sizeof(struct sockaddr_dn)))
  1594. return -EINVAL;
  1595. lock_sock(sk);
  1596. timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
  1597. /*
  1598. * The only difference between stream sockets and sequenced packet
  1599. * sockets is that the stream sockets always behave as if MSG_EOR
  1600. * has been set.
  1601. */
  1602. if (sock->type == SOCK_STREAM) {
  1603. if (flags & MSG_EOR) {
  1604. err = -EINVAL;
  1605. goto out;
  1606. }
  1607. flags |= MSG_EOR;
  1608. }
  1609. err = dn_check_state(sk, addr, addr_len, &timeo, flags);
  1610. if (err)
  1611. goto out_err;
  1612. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  1613. err = -EPIPE;
  1614. if (!(flags & MSG_NOSIGNAL))
  1615. send_sig(SIGPIPE, current, 0);
  1616. goto out_err;
  1617. }
  1618. if ((flags & MSG_TRYHARD) && sk->sk_dst_cache)
  1619. dst_negative_advice(sk);
  1620. mss = scp->segsize_rem;
  1621. fctype = scp->services_rem & NSP_FC_MASK;
  1622. mss = dn_current_mss(sk, flags);
  1623. if (flags & MSG_OOB) {
  1624. queue = &scp->other_xmit_queue;
  1625. if (size > mss) {
  1626. err = -EMSGSIZE;
  1627. goto out;
  1628. }
  1629. }
  1630. scp->persist_fxn = dn_nsp_xmit_timeout;
  1631. while(sent < size) {
  1632. err = sock_error(sk);
  1633. if (err)
  1634. goto out;
  1635. if (signal_pending(current)) {
  1636. err = sock_intr_errno(timeo);
  1637. goto out;
  1638. }
  1639. /*
  1640. * Calculate size that we wish to send.
  1641. */
  1642. len = size - sent;
  1643. if (len > mss)
  1644. len = mss;
  1645. /*
  1646. * Wait for queue size to go down below the window
  1647. * size.
  1648. */
  1649. if (dn_queue_too_long(scp, queue, flags)) {
  1650. DEFINE_WAIT(wait);
  1651. if (flags & MSG_DONTWAIT) {
  1652. err = -EWOULDBLOCK;
  1653. goto out;
  1654. }
  1655. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1656. set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  1657. sk_wait_event(sk, &timeo,
  1658. !dn_queue_too_long(scp, queue, flags));
  1659. clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  1660. finish_wait(sk_sleep(sk), &wait);
  1661. continue;
  1662. }
  1663. /*
  1664. * Get a suitably sized skb.
  1665. * 64 is a bit of a hack really, but its larger than any
  1666. * link-layer headers and has served us well as a good
  1667. * guess as to their real length.
  1668. */
  1669. skb = dn_alloc_send_pskb(sk, len + 64 + DN_MAX_NSP_DATA_HEADER,
  1670. flags & MSG_DONTWAIT, &err);
  1671. if (err)
  1672. break;
  1673. if (!skb)
  1674. continue;
  1675. cb = DN_SKB_CB(skb);
  1676. skb_reserve(skb, 64 + DN_MAX_NSP_DATA_HEADER);
  1677. if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
  1678. err = -EFAULT;
  1679. goto out;
  1680. }
  1681. if (flags & MSG_OOB) {
  1682. cb->nsp_flags = 0x30;
  1683. if (fctype != NSP_FC_NONE)
  1684. scp->flowrem_oth--;
  1685. } else {
  1686. cb->nsp_flags = 0x00;
  1687. if (scp->seg_total == 0)
  1688. cb->nsp_flags |= 0x20;
  1689. scp->seg_total += len;
  1690. if (((sent + len) == size) && (flags & MSG_EOR)) {
  1691. cb->nsp_flags |= 0x40;
  1692. scp->seg_total = 0;
  1693. if (fctype == NSP_FC_SCMC)
  1694. scp->flowrem_dat--;
  1695. }
  1696. if (fctype == NSP_FC_SRC)
  1697. scp->flowrem_dat--;
  1698. }
  1699. sent += len;
  1700. dn_nsp_queue_xmit(sk, skb, sk->sk_allocation, flags & MSG_OOB);
  1701. skb = NULL;
  1702. scp->persist = dn_nsp_persist(sk);
  1703. }
  1704. out:
  1705. kfree_skb(skb);
  1706. release_sock(sk);
  1707. return sent ? sent : err;
  1708. out_err:
  1709. err = sk_stream_error(sk, flags, err);
  1710. release_sock(sk);
  1711. return err;
  1712. }
  1713. static int dn_device_event(struct notifier_block *this, unsigned long event,
  1714. void *ptr)
  1715. {
  1716. struct net_device *dev = (struct net_device *)ptr;
  1717. if (!net_eq(dev_net(dev), &init_net))
  1718. return NOTIFY_DONE;
  1719. switch (event) {
  1720. case NETDEV_UP:
  1721. dn_dev_up(dev);
  1722. break;
  1723. case NETDEV_DOWN:
  1724. dn_dev_down(dev);
  1725. break;
  1726. default:
  1727. break;
  1728. }
  1729. return NOTIFY_DONE;
  1730. }
  1731. static struct notifier_block dn_dev_notifier = {
  1732. .notifier_call = dn_device_event,
  1733. };
  1734. extern int dn_route_rcv(struct sk_buff *, struct net_device *, struct packet_type *, struct net_device *);
  1735. static struct packet_type dn_dix_packet_type __read_mostly = {
  1736. .type = cpu_to_be16(ETH_P_DNA_RT),
  1737. .func = dn_route_rcv,
  1738. };
  1739. #ifdef CONFIG_PROC_FS
  1740. struct dn_iter_state {
  1741. int bucket;
  1742. };
  1743. static struct sock *dn_socket_get_first(struct seq_file *seq)
  1744. {
  1745. struct dn_iter_state *state = seq->private;
  1746. struct sock *n = NULL;
  1747. for(state->bucket = 0;
  1748. state->bucket < DN_SK_HASH_SIZE;
  1749. ++state->bucket) {
  1750. n = sk_head(&dn_sk_hash[state->bucket]);
  1751. if (n)
  1752. break;
  1753. }
  1754. return n;
  1755. }
  1756. static struct sock *dn_socket_get_next(struct seq_file *seq,
  1757. struct sock *n)
  1758. {
  1759. struct dn_iter_state *state = seq->private;
  1760. n = sk_next(n);
  1761. try_again:
  1762. if (n)
  1763. goto out;
  1764. if (++state->bucket >= DN_SK_HASH_SIZE)
  1765. goto out;
  1766. n = sk_head(&dn_sk_hash[state->bucket]);
  1767. goto try_again;
  1768. out:
  1769. return n;
  1770. }
  1771. static struct sock *socket_get_idx(struct seq_file *seq, loff_t *pos)
  1772. {
  1773. struct sock *sk = dn_socket_get_first(seq);
  1774. if (sk) {
  1775. while(*pos && (sk = dn_socket_get_next(seq, sk)))
  1776. --*pos;
  1777. }
  1778. return *pos ? NULL : sk;
  1779. }
  1780. static void *dn_socket_get_idx(struct seq_file *seq, loff_t pos)
  1781. {
  1782. void *rc;
  1783. read_lock_bh(&dn_hash_lock);
  1784. rc = socket_get_idx(seq, &pos);
  1785. if (!rc) {
  1786. read_unlock_bh(&dn_hash_lock);
  1787. }
  1788. return rc;
  1789. }
  1790. static void *dn_socket_seq_start(struct seq_file *seq, loff_t *pos)
  1791. {
  1792. return *pos ? dn_socket_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1793. }
  1794. static void *dn_socket_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1795. {
  1796. void *rc;
  1797. if (v == SEQ_START_TOKEN) {
  1798. rc = dn_socket_get_idx(seq, 0);
  1799. goto out;
  1800. }
  1801. rc = dn_socket_get_next(seq, v);
  1802. if (rc)
  1803. goto out;
  1804. read_unlock_bh(&dn_hash_lock);
  1805. out:
  1806. ++*pos;
  1807. return rc;
  1808. }
  1809. static void dn_socket_seq_stop(struct seq_file *seq, void *v)
  1810. {
  1811. if (v && v != SEQ_START_TOKEN)
  1812. read_unlock_bh(&dn_hash_lock);
  1813. }
  1814. #define IS_NOT_PRINTABLE(x) ((x) < 32 || (x) > 126)
  1815. static void dn_printable_object(struct sockaddr_dn *dn, unsigned char *buf)
  1816. {
  1817. int i;
  1818. switch (le16_to_cpu(dn->sdn_objnamel)) {
  1819. case 0:
  1820. sprintf(buf, "%d", dn->sdn_objnum);
  1821. break;
  1822. default:
  1823. for (i = 0; i < le16_to_cpu(dn->sdn_objnamel); i++) {
  1824. buf[i] = dn->sdn_objname[i];
  1825. if (IS_NOT_PRINTABLE(buf[i]))
  1826. buf[i] = '.';
  1827. }
  1828. buf[i] = 0;
  1829. }
  1830. }
  1831. static char *dn_state2asc(unsigned char state)
  1832. {
  1833. switch (state) {
  1834. case DN_O:
  1835. return "OPEN";
  1836. case DN_CR:
  1837. return " CR";
  1838. case DN_DR:
  1839. return " DR";
  1840. case DN_DRC:
  1841. return " DRC";
  1842. case DN_CC:
  1843. return " CC";
  1844. case DN_CI:
  1845. return " CI";
  1846. case DN_NR:
  1847. return " NR";
  1848. case DN_NC:
  1849. return " NC";
  1850. case DN_CD:
  1851. return " CD";
  1852. case DN_RJ:
  1853. return " RJ";
  1854. case DN_RUN:
  1855. return " RUN";
  1856. case DN_DI:
  1857. return " DI";
  1858. case DN_DIC:
  1859. return " DIC";
  1860. case DN_DN:
  1861. return " DN";
  1862. case DN_CL:
  1863. return " CL";
  1864. case DN_CN:
  1865. return " CN";
  1866. }
  1867. return "????";
  1868. }
  1869. static inline void dn_socket_format_entry(struct seq_file *seq, struct sock *sk)
  1870. {
  1871. struct dn_scp *scp = DN_SK(sk);
  1872. char buf1[DN_ASCBUF_LEN];
  1873. char buf2[DN_ASCBUF_LEN];
  1874. char local_object[DN_MAXOBJL+3];
  1875. char remote_object[DN_MAXOBJL+3];
  1876. dn_printable_object(&scp->addr, local_object);
  1877. dn_printable_object(&scp->peer, remote_object);
  1878. seq_printf(seq,
  1879. "%6s/%04X %04d:%04d %04d:%04d %01d %-16s "
  1880. "%6s/%04X %04d:%04d %04d:%04d %01d %-16s %4s %s\n",
  1881. dn_addr2asc(le16_to_cpu(dn_saddr2dn(&scp->addr)), buf1),
  1882. scp->addrloc,
  1883. scp->numdat,
  1884. scp->numoth,
  1885. scp->ackxmt_dat,
  1886. scp->ackxmt_oth,
  1887. scp->flowloc_sw,
  1888. local_object,
  1889. dn_addr2asc(le16_to_cpu(dn_saddr2dn(&scp->peer)), buf2),
  1890. scp->addrrem,
  1891. scp->numdat_rcv,
  1892. scp->numoth_rcv,
  1893. scp->ackrcv_dat,
  1894. scp->ackrcv_oth,
  1895. scp->flowrem_sw,
  1896. remote_object,
  1897. dn_state2asc(scp->state),
  1898. ((scp->accept_mode == ACC_IMMED) ? "IMMED" : "DEFER"));
  1899. }
  1900. static int dn_socket_seq_show(struct seq_file *seq, void *v)
  1901. {
  1902. if (v == SEQ_START_TOKEN) {
  1903. seq_puts(seq, "Local Remote\n");
  1904. } else {
  1905. dn_socket_format_entry(seq, v);
  1906. }
  1907. return 0;
  1908. }
  1909. static const struct seq_operations dn_socket_seq_ops = {
  1910. .start = dn_socket_seq_start,
  1911. .next = dn_socket_seq_next,
  1912. .stop = dn_socket_seq_stop,
  1913. .show = dn_socket_seq_show,
  1914. };
  1915. static int dn_socket_seq_open(struct inode *inode, struct file *file)
  1916. {
  1917. return seq_open_private(file, &dn_socket_seq_ops,
  1918. sizeof(struct dn_iter_state));
  1919. }
  1920. static const struct file_operations dn_socket_seq_fops = {
  1921. .owner = THIS_MODULE,
  1922. .open = dn_socket_seq_open,
  1923. .read = seq_read,
  1924. .llseek = seq_lseek,
  1925. .release = seq_release_private,
  1926. };
  1927. #endif
  1928. static const struct net_proto_family dn_family_ops = {
  1929. .family = AF_DECnet,
  1930. .create = dn_create,
  1931. .owner = THIS_MODULE,
  1932. };
  1933. static const struct proto_ops dn_proto_ops = {
  1934. .family = AF_DECnet,
  1935. .owner = THIS_MODULE,
  1936. .release = dn_release,
  1937. .bind = dn_bind,
  1938. .connect = dn_connect,
  1939. .socketpair = sock_no_socketpair,
  1940. .accept = dn_accept,
  1941. .getname = dn_getname,
  1942. .poll = dn_poll,
  1943. .ioctl = dn_ioctl,
  1944. .listen = dn_listen,
  1945. .shutdown = dn_shutdown,
  1946. .setsockopt = dn_setsockopt,
  1947. .getsockopt = dn_getsockopt,
  1948. .sendmsg = dn_sendmsg,
  1949. .recvmsg = dn_recvmsg,
  1950. .mmap = sock_no_mmap,
  1951. .sendpage = sock_no_sendpage,
  1952. };
  1953. void dn_register_sysctl(void);
  1954. void dn_unregister_sysctl(void);
  1955. MODULE_DESCRIPTION("The Linux DECnet Network Protocol");
  1956. MODULE_AUTHOR("Linux DECnet Project Team");
  1957. MODULE_LICENSE("GPL");
  1958. MODULE_ALIAS_NETPROTO(PF_DECnet);
  1959. static char banner[] __initdata = KERN_INFO "NET4: DECnet for Linux: V.2.5.68s (C) 1995-2003 Linux DECnet Project Team\n";
  1960. static int __init decnet_init(void)
  1961. {
  1962. int rc;
  1963. printk(banner);
  1964. rc = proto_register(&dn_proto, 1);
  1965. if (rc != 0)
  1966. goto out;
  1967. dn_neigh_init();
  1968. dn_dev_init();
  1969. dn_route_init();
  1970. dn_fib_init();
  1971. sock_register(&dn_family_ops);
  1972. dev_add_pack(&dn_dix_packet_type);
  1973. register_netdevice_notifier(&dn_dev_notifier);
  1974. proc_net_fops_create(&init_net, "decnet", S_IRUGO, &dn_socket_seq_fops);
  1975. dn_register_sysctl();
  1976. out:
  1977. return rc;
  1978. }
  1979. module_init(decnet_init);
  1980. /*
  1981. * Prevent DECnet module unloading until its fixed properly.
  1982. * Requires an audit of the code to check for memory leaks and
  1983. * initialisation problems etc.
  1984. */
  1985. #if 0
  1986. static void __exit decnet_exit(void)
  1987. {
  1988. sock_unregister(AF_DECnet);
  1989. rtnl_unregister_all(PF_DECnet);
  1990. dev_remove_pack(&dn_dix_packet_type);
  1991. dn_unregister_sysctl();
  1992. unregister_netdevice_notifier(&dn_dev_notifier);
  1993. dn_route_cleanup();
  1994. dn_dev_cleanup();
  1995. dn_neigh_cleanup();
  1996. dn_fib_cleanup();
  1997. proc_net_remove(&init_net, "decnet");
  1998. proto_unregister(&dn_proto);
  1999. rcu_barrier_bh(); /* Wait for completion of call_rcu_bh()'s */
  2000. }
  2001. module_exit(decnet_exit);
  2002. #endif