af_decnet.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423
  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 <linux/jiffies.h>
  118. #include <net/net_namespace.h>
  119. #include <net/neighbour.h>
  120. #include <net/dst.h>
  121. #include <net/fib_rules.h>
  122. #include <net/dn.h>
  123. #include <net/dn_nsp.h>
  124. #include <net/dn_dev.h>
  125. #include <net/dn_route.h>
  126. #include <net/dn_fib.h>
  127. #include <net/dn_neigh.h>
  128. struct dn_sock {
  129. struct sock sk;
  130. struct dn_scp scp;
  131. };
  132. static void dn_keepalive(struct sock *sk);
  133. #define DN_SK_HASH_SHIFT 8
  134. #define DN_SK_HASH_SIZE (1 << DN_SK_HASH_SHIFT)
  135. #define DN_SK_HASH_MASK (DN_SK_HASH_SIZE - 1)
  136. static const struct proto_ops dn_proto_ops;
  137. static DEFINE_RWLOCK(dn_hash_lock);
  138. static struct hlist_head dn_sk_hash[DN_SK_HASH_SIZE];
  139. static struct hlist_head dn_wild_sk;
  140. static atomic_long_t decnet_memory_allocated;
  141. static int __dn_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen, int flags);
  142. static int __dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen, int flags);
  143. static struct hlist_head *dn_find_list(struct sock *sk)
  144. {
  145. struct dn_scp *scp = DN_SK(sk);
  146. if (scp->addr.sdn_flags & SDF_WILD)
  147. return hlist_empty(&dn_wild_sk) ? &dn_wild_sk : NULL;
  148. return &dn_sk_hash[le16_to_cpu(scp->addrloc) & DN_SK_HASH_MASK];
  149. }
  150. /*
  151. * Valid ports are those greater than zero and not already in use.
  152. */
  153. static int check_port(__le16 port)
  154. {
  155. struct sock *sk;
  156. if (port == 0)
  157. return -1;
  158. sk_for_each(sk, &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 int 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 sock *sk;
  319. read_lock(&dn_hash_lock);
  320. sk_for_each(sk, list) {
  321. struct dn_scp *scp = DN_SK(sk);
  322. if (sk->sk_state != TCP_LISTEN)
  323. continue;
  324. if (scp->addr.sdn_objnum) {
  325. if (scp->addr.sdn_objnum != addr->sdn_objnum)
  326. continue;
  327. } else {
  328. if (addr->sdn_objnum)
  329. continue;
  330. if (scp->addr.sdn_objnamel != addr->sdn_objnamel)
  331. continue;
  332. if (memcmp(scp->addr.sdn_objname, addr->sdn_objname, le16_to_cpu(addr->sdn_objnamel)) != 0)
  333. continue;
  334. }
  335. sock_hold(sk);
  336. read_unlock(&dn_hash_lock);
  337. return sk;
  338. }
  339. sk = sk_head(&dn_wild_sk);
  340. if (sk) {
  341. if (sk->sk_state == TCP_LISTEN)
  342. sock_hold(sk);
  343. else
  344. sk = NULL;
  345. }
  346. read_unlock(&dn_hash_lock);
  347. return sk;
  348. }
  349. struct sock *dn_find_by_skb(struct sk_buff *skb)
  350. {
  351. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  352. struct sock *sk;
  353. struct dn_scp *scp;
  354. read_lock(&dn_hash_lock);
  355. sk_for_each(sk, &dn_sk_hash[le16_to_cpu(cb->dst_port) & DN_SK_HASH_MASK]) {
  356. scp = DN_SK(sk);
  357. if (cb->src != dn_saddr2dn(&scp->peer))
  358. continue;
  359. if (cb->dst_port != scp->addrloc)
  360. continue;
  361. if (scp->addrrem && (cb->src_port != scp->addrrem))
  362. continue;
  363. sock_hold(sk);
  364. goto found;
  365. }
  366. sk = NULL;
  367. found:
  368. read_unlock(&dn_hash_lock);
  369. return sk;
  370. }
  371. static void dn_destruct(struct sock *sk)
  372. {
  373. struct dn_scp *scp = DN_SK(sk);
  374. skb_queue_purge(&scp->data_xmit_queue);
  375. skb_queue_purge(&scp->other_xmit_queue);
  376. skb_queue_purge(&scp->other_receive_queue);
  377. dst_release(rcu_dereference_check(sk->sk_dst_cache, 1));
  378. }
  379. static int dn_memory_pressure;
  380. static void dn_enter_memory_pressure(struct sock *sk)
  381. {
  382. if (!dn_memory_pressure) {
  383. dn_memory_pressure = 1;
  384. }
  385. }
  386. static struct proto dn_proto = {
  387. .name = "NSP",
  388. .owner = THIS_MODULE,
  389. .enter_memory_pressure = dn_enter_memory_pressure,
  390. .memory_pressure = &dn_memory_pressure,
  391. .memory_allocated = &decnet_memory_allocated,
  392. .sysctl_mem = sysctl_decnet_mem,
  393. .sysctl_wmem = sysctl_decnet_wmem,
  394. .sysctl_rmem = sysctl_decnet_rmem,
  395. .max_header = DN_MAX_NSP_DATA_HEADER + 64,
  396. .obj_size = sizeof(struct dn_sock),
  397. };
  398. static struct sock *dn_alloc_sock(struct net *net, struct socket *sock, gfp_t gfp, int kern)
  399. {
  400. struct dn_scp *scp;
  401. struct sock *sk = sk_alloc(net, PF_DECnet, gfp, &dn_proto, kern);
  402. if (!sk)
  403. goto out;
  404. if (sock)
  405. sock->ops = &dn_proto_ops;
  406. sock_init_data(sock, sk);
  407. sk->sk_backlog_rcv = dn_nsp_backlog_rcv;
  408. sk->sk_destruct = dn_destruct;
  409. sk->sk_no_check_tx = 1;
  410. sk->sk_family = PF_DECnet;
  411. sk->sk_protocol = 0;
  412. sk->sk_allocation = gfp;
  413. sk->sk_sndbuf = sysctl_decnet_wmem[1];
  414. sk->sk_rcvbuf = sysctl_decnet_rmem[1];
  415. /* Initialization of DECnet Session Control Port */
  416. scp = DN_SK(sk);
  417. scp->state = DN_O; /* Open */
  418. scp->numdat = 1; /* Next data seg to tx */
  419. scp->numoth = 1; /* Next oth data to tx */
  420. scp->ackxmt_dat = 0; /* Last data seg ack'ed */
  421. scp->ackxmt_oth = 0; /* Last oth data ack'ed */
  422. scp->ackrcv_dat = 0; /* Highest data ack recv*/
  423. scp->ackrcv_oth = 0; /* Last oth data ack rec*/
  424. scp->flowrem_sw = DN_SEND;
  425. scp->flowloc_sw = DN_SEND;
  426. scp->flowrem_dat = 0;
  427. scp->flowrem_oth = 1;
  428. scp->flowloc_dat = 0;
  429. scp->flowloc_oth = 1;
  430. scp->services_rem = 0;
  431. scp->services_loc = 1 | NSP_FC_NONE;
  432. scp->info_rem = 0;
  433. scp->info_loc = 0x03; /* NSP version 4.1 */
  434. scp->segsize_rem = 230 - DN_MAX_NSP_DATA_HEADER; /* Default: Updated by remote segsize */
  435. scp->nonagle = 0;
  436. scp->multi_ireq = 1;
  437. scp->accept_mode = ACC_IMMED;
  438. scp->addr.sdn_family = AF_DECnet;
  439. scp->peer.sdn_family = AF_DECnet;
  440. scp->accessdata.acc_accl = 5;
  441. memcpy(scp->accessdata.acc_acc, "LINUX", 5);
  442. scp->max_window = NSP_MAX_WINDOW;
  443. scp->snd_window = NSP_MIN_WINDOW;
  444. scp->nsp_srtt = NSP_INITIAL_SRTT;
  445. scp->nsp_rttvar = NSP_INITIAL_RTTVAR;
  446. scp->nsp_rxtshift = 0;
  447. skb_queue_head_init(&scp->data_xmit_queue);
  448. skb_queue_head_init(&scp->other_xmit_queue);
  449. skb_queue_head_init(&scp->other_receive_queue);
  450. scp->persist = 0;
  451. scp->persist_fxn = NULL;
  452. scp->keepalive = 10 * HZ;
  453. scp->keepalive_fxn = dn_keepalive;
  454. init_timer(&scp->delack_timer);
  455. scp->delack_pending = 0;
  456. scp->delack_fxn = dn_nsp_delayed_ack;
  457. dn_start_slow_timer(sk);
  458. out:
  459. return sk;
  460. }
  461. /*
  462. * Keepalive timer.
  463. * FIXME: Should respond to SO_KEEPALIVE etc.
  464. */
  465. static void dn_keepalive(struct sock *sk)
  466. {
  467. struct dn_scp *scp = DN_SK(sk);
  468. /*
  469. * By checking the other_data transmit queue is empty
  470. * we are double checking that we are not sending too
  471. * many of these keepalive frames.
  472. */
  473. if (skb_queue_empty(&scp->other_xmit_queue))
  474. dn_nsp_send_link(sk, DN_NOCHANGE, 0);
  475. }
  476. /*
  477. * Timer for shutdown/destroyed sockets.
  478. * When socket is dead & no packets have been sent for a
  479. * certain amount of time, they are removed by this
  480. * routine. Also takes care of sending out DI & DC
  481. * frames at correct times.
  482. */
  483. int dn_destroy_timer(struct sock *sk)
  484. {
  485. struct dn_scp *scp = DN_SK(sk);
  486. scp->persist = dn_nsp_persist(sk);
  487. switch (scp->state) {
  488. case DN_DI:
  489. dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC);
  490. if (scp->nsp_rxtshift >= decnet_di_count)
  491. scp->state = DN_CN;
  492. return 0;
  493. case DN_DR:
  494. dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC);
  495. if (scp->nsp_rxtshift >= decnet_dr_count)
  496. scp->state = DN_DRC;
  497. return 0;
  498. case DN_DN:
  499. if (scp->nsp_rxtshift < decnet_dn_count) {
  500. /* printk(KERN_DEBUG "dn_destroy_timer: DN\n"); */
  501. dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC,
  502. GFP_ATOMIC);
  503. return 0;
  504. }
  505. }
  506. scp->persist = (HZ * decnet_time_wait);
  507. if (sk->sk_socket)
  508. return 0;
  509. if (time_after_eq(jiffies, scp->stamp + HZ * decnet_time_wait)) {
  510. dn_unhash_sock(sk);
  511. sock_put(sk);
  512. return 1;
  513. }
  514. return 0;
  515. }
  516. static void dn_destroy_sock(struct sock *sk)
  517. {
  518. struct dn_scp *scp = DN_SK(sk);
  519. scp->nsp_rxtshift = 0; /* reset back off */
  520. if (sk->sk_socket) {
  521. if (sk->sk_socket->state != SS_UNCONNECTED)
  522. sk->sk_socket->state = SS_DISCONNECTING;
  523. }
  524. sk->sk_state = TCP_CLOSE;
  525. switch (scp->state) {
  526. case DN_DN:
  527. dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC,
  528. sk->sk_allocation);
  529. scp->persist_fxn = dn_destroy_timer;
  530. scp->persist = dn_nsp_persist(sk);
  531. break;
  532. case DN_CR:
  533. scp->state = DN_DR;
  534. goto disc_reject;
  535. case DN_RUN:
  536. scp->state = DN_DI;
  537. case DN_DI:
  538. case DN_DR:
  539. disc_reject:
  540. dn_nsp_send_disc(sk, NSP_DISCINIT, 0, sk->sk_allocation);
  541. case DN_NC:
  542. case DN_NR:
  543. case DN_RJ:
  544. case DN_DIC:
  545. case DN_CN:
  546. case DN_DRC:
  547. case DN_CI:
  548. case DN_CD:
  549. scp->persist_fxn = dn_destroy_timer;
  550. scp->persist = dn_nsp_persist(sk);
  551. break;
  552. default:
  553. printk(KERN_DEBUG "DECnet: dn_destroy_sock passed socket in invalid state\n");
  554. case DN_O:
  555. dn_stop_slow_timer(sk);
  556. dn_unhash_sock_bh(sk);
  557. sock_put(sk);
  558. break;
  559. }
  560. }
  561. char *dn_addr2asc(__u16 addr, char *buf)
  562. {
  563. unsigned short node, area;
  564. node = addr & 0x03ff;
  565. area = addr >> 10;
  566. sprintf(buf, "%hd.%hd", area, node);
  567. return buf;
  568. }
  569. static int dn_create(struct net *net, struct socket *sock, int protocol,
  570. int kern)
  571. {
  572. struct sock *sk;
  573. if (protocol < 0 || protocol > SK_PROTOCOL_MAX)
  574. return -EINVAL;
  575. if (!net_eq(net, &init_net))
  576. return -EAFNOSUPPORT;
  577. switch (sock->type) {
  578. case SOCK_SEQPACKET:
  579. if (protocol != DNPROTO_NSP)
  580. return -EPROTONOSUPPORT;
  581. break;
  582. case SOCK_STREAM:
  583. break;
  584. default:
  585. return -ESOCKTNOSUPPORT;
  586. }
  587. if ((sk = dn_alloc_sock(net, sock, GFP_KERNEL, kern)) == NULL)
  588. return -ENOBUFS;
  589. sk->sk_protocol = protocol;
  590. return 0;
  591. }
  592. static int
  593. dn_release(struct socket *sock)
  594. {
  595. struct sock *sk = sock->sk;
  596. if (sk) {
  597. sock_orphan(sk);
  598. sock_hold(sk);
  599. lock_sock(sk);
  600. dn_destroy_sock(sk);
  601. release_sock(sk);
  602. sock_put(sk);
  603. }
  604. return 0;
  605. }
  606. static int dn_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  607. {
  608. struct sock *sk = sock->sk;
  609. struct dn_scp *scp = DN_SK(sk);
  610. struct sockaddr_dn *saddr = (struct sockaddr_dn *)uaddr;
  611. struct net_device *dev, *ldev;
  612. int rv;
  613. if (addr_len != sizeof(struct sockaddr_dn))
  614. return -EINVAL;
  615. if (saddr->sdn_family != AF_DECnet)
  616. return -EINVAL;
  617. if (le16_to_cpu(saddr->sdn_nodeaddrl) && (le16_to_cpu(saddr->sdn_nodeaddrl) != 2))
  618. return -EINVAL;
  619. if (le16_to_cpu(saddr->sdn_objnamel) > DN_MAXOBJL)
  620. return -EINVAL;
  621. if (saddr->sdn_flags & ~SDF_WILD)
  622. return -EINVAL;
  623. if (!capable(CAP_NET_BIND_SERVICE) && (saddr->sdn_objnum ||
  624. (saddr->sdn_flags & SDF_WILD)))
  625. return -EACCES;
  626. if (!(saddr->sdn_flags & SDF_WILD)) {
  627. if (le16_to_cpu(saddr->sdn_nodeaddrl)) {
  628. rcu_read_lock();
  629. ldev = NULL;
  630. for_each_netdev_rcu(&init_net, dev) {
  631. if (!dev->dn_ptr)
  632. continue;
  633. if (dn_dev_islocal(dev, dn_saddr2dn(saddr))) {
  634. ldev = dev;
  635. break;
  636. }
  637. }
  638. rcu_read_unlock();
  639. if (ldev == NULL)
  640. return -EADDRNOTAVAIL;
  641. }
  642. }
  643. rv = -EINVAL;
  644. lock_sock(sk);
  645. if (sock_flag(sk, SOCK_ZAPPED)) {
  646. memcpy(&scp->addr, saddr, addr_len);
  647. sock_reset_flag(sk, SOCK_ZAPPED);
  648. rv = dn_hash_sock(sk);
  649. if (rv)
  650. sock_set_flag(sk, SOCK_ZAPPED);
  651. }
  652. release_sock(sk);
  653. return rv;
  654. }
  655. static int dn_auto_bind(struct socket *sock)
  656. {
  657. struct sock *sk = sock->sk;
  658. struct dn_scp *scp = DN_SK(sk);
  659. int rv;
  660. sock_reset_flag(sk, SOCK_ZAPPED);
  661. scp->addr.sdn_flags = 0;
  662. scp->addr.sdn_objnum = 0;
  663. /*
  664. * This stuff is to keep compatibility with Eduardo's
  665. * patch. I hope I can dispense with it shortly...
  666. */
  667. if ((scp->accessdata.acc_accl != 0) &&
  668. (scp->accessdata.acc_accl <= 12)) {
  669. scp->addr.sdn_objnamel = cpu_to_le16(scp->accessdata.acc_accl);
  670. memcpy(scp->addr.sdn_objname, scp->accessdata.acc_acc, le16_to_cpu(scp->addr.sdn_objnamel));
  671. scp->accessdata.acc_accl = 0;
  672. memset(scp->accessdata.acc_acc, 0, 40);
  673. }
  674. /* End of compatibility stuff */
  675. scp->addr.sdn_add.a_len = cpu_to_le16(2);
  676. rv = dn_dev_bind_default((__le16 *)scp->addr.sdn_add.a_addr);
  677. if (rv == 0) {
  678. rv = dn_hash_sock(sk);
  679. if (rv)
  680. sock_set_flag(sk, SOCK_ZAPPED);
  681. }
  682. return rv;
  683. }
  684. static int dn_confirm_accept(struct sock *sk, long *timeo, gfp_t allocation)
  685. {
  686. struct dn_scp *scp = DN_SK(sk);
  687. DEFINE_WAIT(wait);
  688. int err;
  689. if (scp->state != DN_CR)
  690. return -EINVAL;
  691. scp->state = DN_CC;
  692. scp->segsize_loc = dst_metric_advmss(__sk_dst_get(sk));
  693. dn_send_conn_conf(sk, allocation);
  694. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  695. for(;;) {
  696. release_sock(sk);
  697. if (scp->state == DN_CC)
  698. *timeo = schedule_timeout(*timeo);
  699. lock_sock(sk);
  700. err = 0;
  701. if (scp->state == DN_RUN)
  702. break;
  703. err = sock_error(sk);
  704. if (err)
  705. break;
  706. err = sock_intr_errno(*timeo);
  707. if (signal_pending(current))
  708. break;
  709. err = -EAGAIN;
  710. if (!*timeo)
  711. break;
  712. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  713. }
  714. finish_wait(sk_sleep(sk), &wait);
  715. if (err == 0) {
  716. sk->sk_socket->state = SS_CONNECTED;
  717. } else if (scp->state != DN_CC) {
  718. sk->sk_socket->state = SS_UNCONNECTED;
  719. }
  720. return err;
  721. }
  722. static int dn_wait_run(struct sock *sk, long *timeo)
  723. {
  724. struct dn_scp *scp = DN_SK(sk);
  725. DEFINE_WAIT(wait);
  726. int err = 0;
  727. if (scp->state == DN_RUN)
  728. goto out;
  729. if (!*timeo)
  730. return -EALREADY;
  731. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  732. for(;;) {
  733. release_sock(sk);
  734. if (scp->state == DN_CI || scp->state == DN_CC)
  735. *timeo = schedule_timeout(*timeo);
  736. lock_sock(sk);
  737. err = 0;
  738. if (scp->state == DN_RUN)
  739. break;
  740. err = sock_error(sk);
  741. if (err)
  742. break;
  743. err = sock_intr_errno(*timeo);
  744. if (signal_pending(current))
  745. break;
  746. err = -ETIMEDOUT;
  747. if (!*timeo)
  748. break;
  749. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  750. }
  751. finish_wait(sk_sleep(sk), &wait);
  752. out:
  753. if (err == 0) {
  754. sk->sk_socket->state = SS_CONNECTED;
  755. } else if (scp->state != DN_CI && scp->state != DN_CC) {
  756. sk->sk_socket->state = SS_UNCONNECTED;
  757. }
  758. return err;
  759. }
  760. static int __dn_connect(struct sock *sk, struct sockaddr_dn *addr, int addrlen, long *timeo, int flags)
  761. {
  762. struct socket *sock = sk->sk_socket;
  763. struct dn_scp *scp = DN_SK(sk);
  764. int err = -EISCONN;
  765. struct flowidn fld;
  766. struct dst_entry *dst;
  767. if (sock->state == SS_CONNECTED)
  768. goto out;
  769. if (sock->state == SS_CONNECTING) {
  770. err = 0;
  771. if (scp->state == DN_RUN) {
  772. sock->state = SS_CONNECTED;
  773. goto out;
  774. }
  775. err = -ECONNREFUSED;
  776. if (scp->state != DN_CI && scp->state != DN_CC) {
  777. sock->state = SS_UNCONNECTED;
  778. goto out;
  779. }
  780. return dn_wait_run(sk, timeo);
  781. }
  782. err = -EINVAL;
  783. if (scp->state != DN_O)
  784. goto out;
  785. if (addr == NULL || addrlen != sizeof(struct sockaddr_dn))
  786. goto out;
  787. if (addr->sdn_family != AF_DECnet)
  788. goto out;
  789. if (addr->sdn_flags & SDF_WILD)
  790. goto out;
  791. if (sock_flag(sk, SOCK_ZAPPED)) {
  792. err = dn_auto_bind(sk->sk_socket);
  793. if (err)
  794. goto out;
  795. }
  796. memcpy(&scp->peer, addr, sizeof(struct sockaddr_dn));
  797. err = -EHOSTUNREACH;
  798. memset(&fld, 0, sizeof(fld));
  799. fld.flowidn_oif = sk->sk_bound_dev_if;
  800. fld.daddr = dn_saddr2dn(&scp->peer);
  801. fld.saddr = dn_saddr2dn(&scp->addr);
  802. dn_sk_ports_copy(&fld, scp);
  803. fld.flowidn_proto = DNPROTO_NSP;
  804. if (dn_route_output_sock(&sk->sk_dst_cache, &fld, sk, flags) < 0)
  805. goto out;
  806. dst = __sk_dst_get(sk);
  807. sk->sk_route_caps = dst->dev->features;
  808. sock->state = SS_CONNECTING;
  809. scp->state = DN_CI;
  810. scp->segsize_loc = dst_metric_advmss(dst);
  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, 0);
  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 != SHUT_RDWR)
  1093. goto out;
  1094. sk->sk_shutdown = SHUTDOWN_MASK;
  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. #ifdef CONFIG_NETFILTER
  1109. /* we need to exclude all possible ENOPROTOOPTs except default case */
  1110. if (err == -ENOPROTOOPT && optname != DSO_LINKINFO &&
  1111. optname != DSO_STREAM && optname != DSO_SEQPACKET)
  1112. err = nf_setsockopt(sk, PF_DECnet, optname, optval, optlen);
  1113. #endif
  1114. return err;
  1115. }
  1116. static int __dn_setsockopt(struct socket *sock, int level,int optname, char __user *optval, unsigned int optlen, int flags)
  1117. {
  1118. struct sock *sk = sock->sk;
  1119. struct dn_scp *scp = DN_SK(sk);
  1120. long timeo;
  1121. union {
  1122. struct optdata_dn opt;
  1123. struct accessdata_dn acc;
  1124. int mode;
  1125. unsigned long win;
  1126. int val;
  1127. unsigned char services;
  1128. unsigned char info;
  1129. } u;
  1130. int err;
  1131. if (optlen && !optval)
  1132. return -EINVAL;
  1133. if (optlen > sizeof(u))
  1134. return -EINVAL;
  1135. if (copy_from_user(&u, optval, optlen))
  1136. return -EFAULT;
  1137. switch (optname) {
  1138. case DSO_CONDATA:
  1139. if (sock->state == SS_CONNECTED)
  1140. return -EISCONN;
  1141. if ((scp->state != DN_O) && (scp->state != DN_CR))
  1142. return -EINVAL;
  1143. if (optlen != sizeof(struct optdata_dn))
  1144. return -EINVAL;
  1145. if (le16_to_cpu(u.opt.opt_optl) > 16)
  1146. return -EINVAL;
  1147. memcpy(&scp->conndata_out, &u.opt, optlen);
  1148. break;
  1149. case DSO_DISDATA:
  1150. if (sock->state != SS_CONNECTED &&
  1151. scp->accept_mode == ACC_IMMED)
  1152. return -ENOTCONN;
  1153. if (optlen != sizeof(struct optdata_dn))
  1154. return -EINVAL;
  1155. if (le16_to_cpu(u.opt.opt_optl) > 16)
  1156. return -EINVAL;
  1157. memcpy(&scp->discdata_out, &u.opt, optlen);
  1158. break;
  1159. case DSO_CONACCESS:
  1160. if (sock->state == SS_CONNECTED)
  1161. return -EISCONN;
  1162. if (scp->state != DN_O)
  1163. return -EINVAL;
  1164. if (optlen != sizeof(struct accessdata_dn))
  1165. return -EINVAL;
  1166. if ((u.acc.acc_accl > DN_MAXACCL) ||
  1167. (u.acc.acc_passl > DN_MAXACCL) ||
  1168. (u.acc.acc_userl > DN_MAXACCL))
  1169. return -EINVAL;
  1170. memcpy(&scp->accessdata, &u.acc, optlen);
  1171. break;
  1172. case DSO_ACCEPTMODE:
  1173. if (sock->state == SS_CONNECTED)
  1174. return -EISCONN;
  1175. if (scp->state != DN_O)
  1176. return -EINVAL;
  1177. if (optlen != sizeof(int))
  1178. return -EINVAL;
  1179. if ((u.mode != ACC_IMMED) && (u.mode != ACC_DEFER))
  1180. return -EINVAL;
  1181. scp->accept_mode = (unsigned char)u.mode;
  1182. break;
  1183. case DSO_CONACCEPT:
  1184. if (scp->state != DN_CR)
  1185. return -EINVAL;
  1186. timeo = sock_rcvtimeo(sk, 0);
  1187. err = dn_confirm_accept(sk, &timeo, sk->sk_allocation);
  1188. return err;
  1189. case DSO_CONREJECT:
  1190. if (scp->state != DN_CR)
  1191. return -EINVAL;
  1192. scp->state = DN_DR;
  1193. sk->sk_shutdown = SHUTDOWN_MASK;
  1194. dn_nsp_send_disc(sk, 0x38, 0, sk->sk_allocation);
  1195. break;
  1196. case DSO_MAXWINDOW:
  1197. if (optlen != sizeof(unsigned long))
  1198. return -EINVAL;
  1199. if (u.win > NSP_MAX_WINDOW)
  1200. u.win = NSP_MAX_WINDOW;
  1201. if (u.win == 0)
  1202. return -EINVAL;
  1203. scp->max_window = u.win;
  1204. if (scp->snd_window > u.win)
  1205. scp->snd_window = u.win;
  1206. break;
  1207. case DSO_NODELAY:
  1208. if (optlen != sizeof(int))
  1209. return -EINVAL;
  1210. if (scp->nonagle == 2)
  1211. return -EINVAL;
  1212. scp->nonagle = (u.val == 0) ? 0 : 1;
  1213. /* if (scp->nonagle == 1) { Push pending frames } */
  1214. break;
  1215. case DSO_CORK:
  1216. if (optlen != sizeof(int))
  1217. return -EINVAL;
  1218. if (scp->nonagle == 1)
  1219. return -EINVAL;
  1220. scp->nonagle = (u.val == 0) ? 0 : 2;
  1221. /* if (scp->nonagle == 0) { Push pending frames } */
  1222. break;
  1223. case DSO_SERVICES:
  1224. if (optlen != sizeof(unsigned char))
  1225. return -EINVAL;
  1226. if ((u.services & ~NSP_FC_MASK) != 0x01)
  1227. return -EINVAL;
  1228. if ((u.services & NSP_FC_MASK) == NSP_FC_MASK)
  1229. return -EINVAL;
  1230. scp->services_loc = u.services;
  1231. break;
  1232. case DSO_INFO:
  1233. if (optlen != sizeof(unsigned char))
  1234. return -EINVAL;
  1235. if (u.info & 0xfc)
  1236. return -EINVAL;
  1237. scp->info_loc = u.info;
  1238. break;
  1239. case DSO_LINKINFO:
  1240. case DSO_STREAM:
  1241. case DSO_SEQPACKET:
  1242. default:
  1243. return -ENOPROTOOPT;
  1244. }
  1245. return 0;
  1246. }
  1247. static int dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
  1248. {
  1249. struct sock *sk = sock->sk;
  1250. int err;
  1251. lock_sock(sk);
  1252. err = __dn_getsockopt(sock, level, optname, optval, optlen, 0);
  1253. release_sock(sk);
  1254. #ifdef CONFIG_NETFILTER
  1255. if (err == -ENOPROTOOPT && optname != DSO_STREAM &&
  1256. optname != DSO_SEQPACKET && optname != DSO_CONACCEPT &&
  1257. optname != DSO_CONREJECT) {
  1258. int len;
  1259. if (get_user(len, optlen))
  1260. return -EFAULT;
  1261. err = nf_getsockopt(sk, PF_DECnet, optname, optval, &len);
  1262. if (err >= 0)
  1263. err = put_user(len, optlen);
  1264. }
  1265. #endif
  1266. return err;
  1267. }
  1268. static int __dn_getsockopt(struct socket *sock, int level,int optname, char __user *optval,int __user *optlen, int flags)
  1269. {
  1270. struct sock *sk = sock->sk;
  1271. struct dn_scp *scp = DN_SK(sk);
  1272. struct linkinfo_dn link;
  1273. unsigned int r_len;
  1274. void *r_data = NULL;
  1275. unsigned int val;
  1276. if(get_user(r_len , optlen))
  1277. return -EFAULT;
  1278. switch (optname) {
  1279. case DSO_CONDATA:
  1280. if (r_len > sizeof(struct optdata_dn))
  1281. r_len = sizeof(struct optdata_dn);
  1282. r_data = &scp->conndata_in;
  1283. break;
  1284. case DSO_DISDATA:
  1285. if (r_len > sizeof(struct optdata_dn))
  1286. r_len = sizeof(struct optdata_dn);
  1287. r_data = &scp->discdata_in;
  1288. break;
  1289. case DSO_CONACCESS:
  1290. if (r_len > sizeof(struct accessdata_dn))
  1291. r_len = sizeof(struct accessdata_dn);
  1292. r_data = &scp->accessdata;
  1293. break;
  1294. case DSO_ACCEPTMODE:
  1295. if (r_len > sizeof(unsigned char))
  1296. r_len = sizeof(unsigned char);
  1297. r_data = &scp->accept_mode;
  1298. break;
  1299. case DSO_LINKINFO:
  1300. if (r_len > sizeof(struct linkinfo_dn))
  1301. r_len = sizeof(struct linkinfo_dn);
  1302. memset(&link, 0, sizeof(link));
  1303. switch (sock->state) {
  1304. case SS_CONNECTING:
  1305. link.idn_linkstate = LL_CONNECTING;
  1306. break;
  1307. case SS_DISCONNECTING:
  1308. link.idn_linkstate = LL_DISCONNECTING;
  1309. break;
  1310. case SS_CONNECTED:
  1311. link.idn_linkstate = LL_RUNNING;
  1312. break;
  1313. default:
  1314. link.idn_linkstate = LL_INACTIVE;
  1315. }
  1316. link.idn_segsize = scp->segsize_rem;
  1317. r_data = &link;
  1318. break;
  1319. case DSO_MAXWINDOW:
  1320. if (r_len > sizeof(unsigned long))
  1321. r_len = sizeof(unsigned long);
  1322. r_data = &scp->max_window;
  1323. break;
  1324. case DSO_NODELAY:
  1325. if (r_len > sizeof(int))
  1326. r_len = sizeof(int);
  1327. val = (scp->nonagle == 1);
  1328. r_data = &val;
  1329. break;
  1330. case DSO_CORK:
  1331. if (r_len > sizeof(int))
  1332. r_len = sizeof(int);
  1333. val = (scp->nonagle == 2);
  1334. r_data = &val;
  1335. break;
  1336. case DSO_SERVICES:
  1337. if (r_len > sizeof(unsigned char))
  1338. r_len = sizeof(unsigned char);
  1339. r_data = &scp->services_rem;
  1340. break;
  1341. case DSO_INFO:
  1342. if (r_len > sizeof(unsigned char))
  1343. r_len = sizeof(unsigned char);
  1344. r_data = &scp->info_rem;
  1345. break;
  1346. case DSO_STREAM:
  1347. case DSO_SEQPACKET:
  1348. case DSO_CONACCEPT:
  1349. case DSO_CONREJECT:
  1350. default:
  1351. return -ENOPROTOOPT;
  1352. }
  1353. if (r_data) {
  1354. if (copy_to_user(optval, r_data, r_len))
  1355. return -EFAULT;
  1356. if (put_user(r_len, optlen))
  1357. return -EFAULT;
  1358. }
  1359. return 0;
  1360. }
  1361. static int dn_data_ready(struct sock *sk, struct sk_buff_head *q, int flags, int target)
  1362. {
  1363. struct sk_buff *skb;
  1364. int len = 0;
  1365. if (flags & MSG_OOB)
  1366. return !skb_queue_empty(q) ? 1 : 0;
  1367. skb_queue_walk(q, skb) {
  1368. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  1369. len += skb->len;
  1370. if (cb->nsp_flags & 0x40) {
  1371. /* SOCK_SEQPACKET reads to EOM */
  1372. if (sk->sk_type == SOCK_SEQPACKET)
  1373. return 1;
  1374. /* so does SOCK_STREAM unless WAITALL is specified */
  1375. if (!(flags & MSG_WAITALL))
  1376. return 1;
  1377. }
  1378. /* minimum data length for read exceeded */
  1379. if (len >= target)
  1380. return 1;
  1381. }
  1382. return 0;
  1383. }
  1384. static int dn_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
  1385. int flags)
  1386. {
  1387. struct sock *sk = sock->sk;
  1388. struct dn_scp *scp = DN_SK(sk);
  1389. struct sk_buff_head *queue = &sk->sk_receive_queue;
  1390. size_t target = size > 1 ? 1 : 0;
  1391. size_t copied = 0;
  1392. int rv = 0;
  1393. struct sk_buff *skb, *n;
  1394. struct dn_skb_cb *cb = NULL;
  1395. unsigned char eor = 0;
  1396. long timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1397. lock_sock(sk);
  1398. if (sock_flag(sk, SOCK_ZAPPED)) {
  1399. rv = -EADDRNOTAVAIL;
  1400. goto out;
  1401. }
  1402. if (sk->sk_shutdown & RCV_SHUTDOWN) {
  1403. rv = 0;
  1404. goto out;
  1405. }
  1406. rv = dn_check_state(sk, NULL, 0, &timeo, flags);
  1407. if (rv)
  1408. goto out;
  1409. if (flags & ~(MSG_CMSG_COMPAT|MSG_PEEK|MSG_OOB|MSG_WAITALL|MSG_DONTWAIT|MSG_NOSIGNAL)) {
  1410. rv = -EOPNOTSUPP;
  1411. goto out;
  1412. }
  1413. if (flags & MSG_OOB)
  1414. queue = &scp->other_receive_queue;
  1415. if (flags & MSG_WAITALL)
  1416. target = size;
  1417. /*
  1418. * See if there is data ready to read, sleep if there isn't
  1419. */
  1420. for(;;) {
  1421. DEFINE_WAIT(wait);
  1422. if (sk->sk_err)
  1423. goto out;
  1424. if (!skb_queue_empty(&scp->other_receive_queue)) {
  1425. if (!(flags & MSG_OOB)) {
  1426. msg->msg_flags |= MSG_OOB;
  1427. if (!scp->other_report) {
  1428. scp->other_report = 1;
  1429. goto out;
  1430. }
  1431. }
  1432. }
  1433. if (scp->state != DN_RUN)
  1434. goto out;
  1435. if (signal_pending(current)) {
  1436. rv = sock_intr_errno(timeo);
  1437. goto out;
  1438. }
  1439. if (dn_data_ready(sk, queue, flags, target))
  1440. break;
  1441. if (flags & MSG_DONTWAIT) {
  1442. rv = -EWOULDBLOCK;
  1443. goto out;
  1444. }
  1445. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1446. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  1447. sk_wait_event(sk, &timeo, dn_data_ready(sk, queue, flags, target));
  1448. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  1449. finish_wait(sk_sleep(sk), &wait);
  1450. }
  1451. skb_queue_walk_safe(queue, skb, n) {
  1452. unsigned int chunk = skb->len;
  1453. cb = DN_SKB_CB(skb);
  1454. if ((chunk + copied) > size)
  1455. chunk = size - copied;
  1456. if (memcpy_to_msg(msg, skb->data, chunk)) {
  1457. rv = -EFAULT;
  1458. break;
  1459. }
  1460. copied += chunk;
  1461. if (!(flags & MSG_PEEK))
  1462. skb_pull(skb, chunk);
  1463. eor = cb->nsp_flags & 0x40;
  1464. if (skb->len == 0) {
  1465. skb_unlink(skb, queue);
  1466. kfree_skb(skb);
  1467. /*
  1468. * N.B. Don't refer to skb or cb after this point
  1469. * in loop.
  1470. */
  1471. if ((scp->flowloc_sw == DN_DONTSEND) && !dn_congested(sk)) {
  1472. scp->flowloc_sw = DN_SEND;
  1473. dn_nsp_send_link(sk, DN_SEND, 0);
  1474. }
  1475. }
  1476. if (eor) {
  1477. if (sk->sk_type == SOCK_SEQPACKET)
  1478. break;
  1479. if (!(flags & MSG_WAITALL))
  1480. break;
  1481. }
  1482. if (flags & MSG_OOB)
  1483. break;
  1484. if (copied >= target)
  1485. break;
  1486. }
  1487. rv = copied;
  1488. if (eor && (sk->sk_type == SOCK_SEQPACKET))
  1489. msg->msg_flags |= MSG_EOR;
  1490. out:
  1491. if (rv == 0)
  1492. rv = (flags & MSG_PEEK) ? -sk->sk_err : sock_error(sk);
  1493. if ((rv >= 0) && msg->msg_name) {
  1494. __sockaddr_check_size(sizeof(struct sockaddr_dn));
  1495. memcpy(msg->msg_name, &scp->peer, sizeof(struct sockaddr_dn));
  1496. msg->msg_namelen = sizeof(struct sockaddr_dn);
  1497. }
  1498. release_sock(sk);
  1499. return rv;
  1500. }
  1501. static inline int dn_queue_too_long(struct dn_scp *scp, struct sk_buff_head *queue, int flags)
  1502. {
  1503. unsigned char fctype = scp->services_rem & NSP_FC_MASK;
  1504. if (skb_queue_len(queue) >= scp->snd_window)
  1505. return 1;
  1506. if (fctype != NSP_FC_NONE) {
  1507. if (flags & MSG_OOB) {
  1508. if (scp->flowrem_oth == 0)
  1509. return 1;
  1510. } else {
  1511. if (scp->flowrem_dat == 0)
  1512. return 1;
  1513. }
  1514. }
  1515. return 0;
  1516. }
  1517. /*
  1518. * The DECnet spec requires that the "routing layer" accepts packets which
  1519. * are at least 230 bytes in size. This excludes any headers which the NSP
  1520. * layer might add, so we always assume that we'll be using the maximal
  1521. * length header on data packets. The variation in length is due to the
  1522. * inclusion (or not) of the two 16 bit acknowledgement fields so it doesn't
  1523. * make much practical difference.
  1524. */
  1525. unsigned int dn_mss_from_pmtu(struct net_device *dev, int mtu)
  1526. {
  1527. unsigned int mss = 230 - DN_MAX_NSP_DATA_HEADER;
  1528. if (dev) {
  1529. struct dn_dev *dn_db = rcu_dereference_raw(dev->dn_ptr);
  1530. mtu -= LL_RESERVED_SPACE(dev);
  1531. if (dn_db->use_long)
  1532. mtu -= 21;
  1533. else
  1534. mtu -= 6;
  1535. mtu -= DN_MAX_NSP_DATA_HEADER;
  1536. } else {
  1537. /*
  1538. * 21 = long header, 16 = guess at MAC header length
  1539. */
  1540. mtu -= (21 + DN_MAX_NSP_DATA_HEADER + 16);
  1541. }
  1542. if (mtu > mss)
  1543. mss = mtu;
  1544. return mss;
  1545. }
  1546. static inline unsigned int dn_current_mss(struct sock *sk, int flags)
  1547. {
  1548. struct dst_entry *dst = __sk_dst_get(sk);
  1549. struct dn_scp *scp = DN_SK(sk);
  1550. int mss_now = min_t(int, scp->segsize_loc, scp->segsize_rem);
  1551. /* Other data messages are limited to 16 bytes per packet */
  1552. if (flags & MSG_OOB)
  1553. return 16;
  1554. /* This works out the maximum size of segment we can send out */
  1555. if (dst) {
  1556. u32 mtu = dst_mtu(dst);
  1557. mss_now = min_t(int, dn_mss_from_pmtu(dst->dev, mtu), mss_now);
  1558. }
  1559. return mss_now;
  1560. }
  1561. /*
  1562. * N.B. We get the timeout wrong here, but then we always did get it
  1563. * wrong before and this is another step along the road to correcting
  1564. * it. It ought to get updated each time we pass through the routine,
  1565. * but in practise it probably doesn't matter too much for now.
  1566. */
  1567. static inline struct sk_buff *dn_alloc_send_pskb(struct sock *sk,
  1568. unsigned long datalen, int noblock,
  1569. int *errcode)
  1570. {
  1571. struct sk_buff *skb = sock_alloc_send_skb(sk, datalen,
  1572. noblock, errcode);
  1573. if (skb) {
  1574. skb->protocol = htons(ETH_P_DNA_RT);
  1575. skb->pkt_type = PACKET_OUTGOING;
  1576. }
  1577. return skb;
  1578. }
  1579. static int dn_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
  1580. {
  1581. struct sock *sk = sock->sk;
  1582. struct dn_scp *scp = DN_SK(sk);
  1583. size_t mss;
  1584. struct sk_buff_head *queue = &scp->data_xmit_queue;
  1585. int flags = msg->msg_flags;
  1586. int err = 0;
  1587. size_t sent = 0;
  1588. int addr_len = msg->msg_namelen;
  1589. DECLARE_SOCKADDR(struct sockaddr_dn *, addr, msg->msg_name);
  1590. struct sk_buff *skb = NULL;
  1591. struct dn_skb_cb *cb;
  1592. size_t len;
  1593. unsigned char fctype;
  1594. long timeo;
  1595. if (flags & ~(MSG_TRYHARD|MSG_OOB|MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|MSG_MORE|MSG_CMSG_COMPAT))
  1596. return -EOPNOTSUPP;
  1597. if (addr_len && (addr_len != sizeof(struct sockaddr_dn)))
  1598. return -EINVAL;
  1599. lock_sock(sk);
  1600. timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
  1601. /*
  1602. * The only difference between stream sockets and sequenced packet
  1603. * sockets is that the stream sockets always behave as if MSG_EOR
  1604. * has been set.
  1605. */
  1606. if (sock->type == SOCK_STREAM) {
  1607. if (flags & MSG_EOR) {
  1608. err = -EINVAL;
  1609. goto out;
  1610. }
  1611. flags |= MSG_EOR;
  1612. }
  1613. err = dn_check_state(sk, addr, addr_len, &timeo, flags);
  1614. if (err)
  1615. goto out_err;
  1616. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  1617. err = -EPIPE;
  1618. if (!(flags & MSG_NOSIGNAL))
  1619. send_sig(SIGPIPE, current, 0);
  1620. goto out_err;
  1621. }
  1622. if ((flags & MSG_TRYHARD) && sk->sk_dst_cache)
  1623. dst_negative_advice(sk);
  1624. mss = scp->segsize_rem;
  1625. fctype = scp->services_rem & NSP_FC_MASK;
  1626. mss = dn_current_mss(sk, flags);
  1627. if (flags & MSG_OOB) {
  1628. queue = &scp->other_xmit_queue;
  1629. if (size > mss) {
  1630. err = -EMSGSIZE;
  1631. goto out;
  1632. }
  1633. }
  1634. scp->persist_fxn = dn_nsp_xmit_timeout;
  1635. while(sent < size) {
  1636. err = sock_error(sk);
  1637. if (err)
  1638. goto out;
  1639. if (signal_pending(current)) {
  1640. err = sock_intr_errno(timeo);
  1641. goto out;
  1642. }
  1643. /*
  1644. * Calculate size that we wish to send.
  1645. */
  1646. len = size - sent;
  1647. if (len > mss)
  1648. len = mss;
  1649. /*
  1650. * Wait for queue size to go down below the window
  1651. * size.
  1652. */
  1653. if (dn_queue_too_long(scp, queue, flags)) {
  1654. DEFINE_WAIT(wait);
  1655. if (flags & MSG_DONTWAIT) {
  1656. err = -EWOULDBLOCK;
  1657. goto out;
  1658. }
  1659. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1660. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  1661. sk_wait_event(sk, &timeo,
  1662. !dn_queue_too_long(scp, queue, flags));
  1663. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  1664. finish_wait(sk_sleep(sk), &wait);
  1665. continue;
  1666. }
  1667. /*
  1668. * Get a suitably sized skb.
  1669. * 64 is a bit of a hack really, but its larger than any
  1670. * link-layer headers and has served us well as a good
  1671. * guess as to their real length.
  1672. */
  1673. skb = dn_alloc_send_pskb(sk, len + 64 + DN_MAX_NSP_DATA_HEADER,
  1674. flags & MSG_DONTWAIT, &err);
  1675. if (err)
  1676. break;
  1677. if (!skb)
  1678. continue;
  1679. cb = DN_SKB_CB(skb);
  1680. skb_reserve(skb, 64 + DN_MAX_NSP_DATA_HEADER);
  1681. if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
  1682. err = -EFAULT;
  1683. goto out;
  1684. }
  1685. if (flags & MSG_OOB) {
  1686. cb->nsp_flags = 0x30;
  1687. if (fctype != NSP_FC_NONE)
  1688. scp->flowrem_oth--;
  1689. } else {
  1690. cb->nsp_flags = 0x00;
  1691. if (scp->seg_total == 0)
  1692. cb->nsp_flags |= 0x20;
  1693. scp->seg_total += len;
  1694. if (((sent + len) == size) && (flags & MSG_EOR)) {
  1695. cb->nsp_flags |= 0x40;
  1696. scp->seg_total = 0;
  1697. if (fctype == NSP_FC_SCMC)
  1698. scp->flowrem_dat--;
  1699. }
  1700. if (fctype == NSP_FC_SRC)
  1701. scp->flowrem_dat--;
  1702. }
  1703. sent += len;
  1704. dn_nsp_queue_xmit(sk, skb, sk->sk_allocation, flags & MSG_OOB);
  1705. skb = NULL;
  1706. scp->persist = dn_nsp_persist(sk);
  1707. }
  1708. out:
  1709. kfree_skb(skb);
  1710. release_sock(sk);
  1711. return sent ? sent : err;
  1712. out_err:
  1713. err = sk_stream_error(sk, flags, err);
  1714. release_sock(sk);
  1715. return err;
  1716. }
  1717. static int dn_device_event(struct notifier_block *this, unsigned long event,
  1718. void *ptr)
  1719. {
  1720. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1721. if (!net_eq(dev_net(dev), &init_net))
  1722. return NOTIFY_DONE;
  1723. switch (event) {
  1724. case NETDEV_UP:
  1725. dn_dev_up(dev);
  1726. break;
  1727. case NETDEV_DOWN:
  1728. dn_dev_down(dev);
  1729. break;
  1730. default:
  1731. break;
  1732. }
  1733. return NOTIFY_DONE;
  1734. }
  1735. static struct notifier_block dn_dev_notifier = {
  1736. .notifier_call = dn_device_event,
  1737. };
  1738. static struct packet_type dn_dix_packet_type __read_mostly = {
  1739. .type = cpu_to_be16(ETH_P_DNA_RT),
  1740. .func = dn_route_rcv,
  1741. };
  1742. #ifdef CONFIG_PROC_FS
  1743. struct dn_iter_state {
  1744. int bucket;
  1745. };
  1746. static struct sock *dn_socket_get_first(struct seq_file *seq)
  1747. {
  1748. struct dn_iter_state *state = seq->private;
  1749. struct sock *n = NULL;
  1750. for(state->bucket = 0;
  1751. state->bucket < DN_SK_HASH_SIZE;
  1752. ++state->bucket) {
  1753. n = sk_head(&dn_sk_hash[state->bucket]);
  1754. if (n)
  1755. break;
  1756. }
  1757. return n;
  1758. }
  1759. static struct sock *dn_socket_get_next(struct seq_file *seq,
  1760. struct sock *n)
  1761. {
  1762. struct dn_iter_state *state = seq->private;
  1763. n = sk_next(n);
  1764. try_again:
  1765. if (n)
  1766. goto out;
  1767. if (++state->bucket >= DN_SK_HASH_SIZE)
  1768. goto out;
  1769. n = sk_head(&dn_sk_hash[state->bucket]);
  1770. goto try_again;
  1771. out:
  1772. return n;
  1773. }
  1774. static struct sock *socket_get_idx(struct seq_file *seq, loff_t *pos)
  1775. {
  1776. struct sock *sk = dn_socket_get_first(seq);
  1777. if (sk) {
  1778. while(*pos && (sk = dn_socket_get_next(seq, sk)))
  1779. --*pos;
  1780. }
  1781. return *pos ? NULL : sk;
  1782. }
  1783. static void *dn_socket_get_idx(struct seq_file *seq, loff_t pos)
  1784. {
  1785. void *rc;
  1786. read_lock_bh(&dn_hash_lock);
  1787. rc = socket_get_idx(seq, &pos);
  1788. if (!rc) {
  1789. read_unlock_bh(&dn_hash_lock);
  1790. }
  1791. return rc;
  1792. }
  1793. static void *dn_socket_seq_start(struct seq_file *seq, loff_t *pos)
  1794. {
  1795. return *pos ? dn_socket_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1796. }
  1797. static void *dn_socket_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1798. {
  1799. void *rc;
  1800. if (v == SEQ_START_TOKEN) {
  1801. rc = dn_socket_get_idx(seq, 0);
  1802. goto out;
  1803. }
  1804. rc = dn_socket_get_next(seq, v);
  1805. if (rc)
  1806. goto out;
  1807. read_unlock_bh(&dn_hash_lock);
  1808. out:
  1809. ++*pos;
  1810. return rc;
  1811. }
  1812. static void dn_socket_seq_stop(struct seq_file *seq, void *v)
  1813. {
  1814. if (v && v != SEQ_START_TOKEN)
  1815. read_unlock_bh(&dn_hash_lock);
  1816. }
  1817. #define IS_NOT_PRINTABLE(x) ((x) < 32 || (x) > 126)
  1818. static void dn_printable_object(struct sockaddr_dn *dn, unsigned char *buf)
  1819. {
  1820. int i;
  1821. switch (le16_to_cpu(dn->sdn_objnamel)) {
  1822. case 0:
  1823. sprintf(buf, "%d", dn->sdn_objnum);
  1824. break;
  1825. default:
  1826. for (i = 0; i < le16_to_cpu(dn->sdn_objnamel); i++) {
  1827. buf[i] = dn->sdn_objname[i];
  1828. if (IS_NOT_PRINTABLE(buf[i]))
  1829. buf[i] = '.';
  1830. }
  1831. buf[i] = 0;
  1832. }
  1833. }
  1834. static char *dn_state2asc(unsigned char state)
  1835. {
  1836. switch (state) {
  1837. case DN_O:
  1838. return "OPEN";
  1839. case DN_CR:
  1840. return " CR";
  1841. case DN_DR:
  1842. return " DR";
  1843. case DN_DRC:
  1844. return " DRC";
  1845. case DN_CC:
  1846. return " CC";
  1847. case DN_CI:
  1848. return " CI";
  1849. case DN_NR:
  1850. return " NR";
  1851. case DN_NC:
  1852. return " NC";
  1853. case DN_CD:
  1854. return " CD";
  1855. case DN_RJ:
  1856. return " RJ";
  1857. case DN_RUN:
  1858. return " RUN";
  1859. case DN_DI:
  1860. return " DI";
  1861. case DN_DIC:
  1862. return " DIC";
  1863. case DN_DN:
  1864. return " DN";
  1865. case DN_CL:
  1866. return " CL";
  1867. case DN_CN:
  1868. return " CN";
  1869. }
  1870. return "????";
  1871. }
  1872. static inline void dn_socket_format_entry(struct seq_file *seq, struct sock *sk)
  1873. {
  1874. struct dn_scp *scp = DN_SK(sk);
  1875. char buf1[DN_ASCBUF_LEN];
  1876. char buf2[DN_ASCBUF_LEN];
  1877. char local_object[DN_MAXOBJL+3];
  1878. char remote_object[DN_MAXOBJL+3];
  1879. dn_printable_object(&scp->addr, local_object);
  1880. dn_printable_object(&scp->peer, remote_object);
  1881. seq_printf(seq,
  1882. "%6s/%04X %04d:%04d %04d:%04d %01d %-16s "
  1883. "%6s/%04X %04d:%04d %04d:%04d %01d %-16s %4s %s\n",
  1884. dn_addr2asc(le16_to_cpu(dn_saddr2dn(&scp->addr)), buf1),
  1885. scp->addrloc,
  1886. scp->numdat,
  1887. scp->numoth,
  1888. scp->ackxmt_dat,
  1889. scp->ackxmt_oth,
  1890. scp->flowloc_sw,
  1891. local_object,
  1892. dn_addr2asc(le16_to_cpu(dn_saddr2dn(&scp->peer)), buf2),
  1893. scp->addrrem,
  1894. scp->numdat_rcv,
  1895. scp->numoth_rcv,
  1896. scp->ackrcv_dat,
  1897. scp->ackrcv_oth,
  1898. scp->flowrem_sw,
  1899. remote_object,
  1900. dn_state2asc(scp->state),
  1901. ((scp->accept_mode == ACC_IMMED) ? "IMMED" : "DEFER"));
  1902. }
  1903. static int dn_socket_seq_show(struct seq_file *seq, void *v)
  1904. {
  1905. if (v == SEQ_START_TOKEN) {
  1906. seq_puts(seq, "Local Remote\n");
  1907. } else {
  1908. dn_socket_format_entry(seq, v);
  1909. }
  1910. return 0;
  1911. }
  1912. static const struct seq_operations dn_socket_seq_ops = {
  1913. .start = dn_socket_seq_start,
  1914. .next = dn_socket_seq_next,
  1915. .stop = dn_socket_seq_stop,
  1916. .show = dn_socket_seq_show,
  1917. };
  1918. static int dn_socket_seq_open(struct inode *inode, struct file *file)
  1919. {
  1920. return seq_open_private(file, &dn_socket_seq_ops,
  1921. sizeof(struct dn_iter_state));
  1922. }
  1923. static const struct file_operations dn_socket_seq_fops = {
  1924. .owner = THIS_MODULE,
  1925. .open = dn_socket_seq_open,
  1926. .read = seq_read,
  1927. .llseek = seq_lseek,
  1928. .release = seq_release_private,
  1929. };
  1930. #endif
  1931. static const struct net_proto_family dn_family_ops = {
  1932. .family = AF_DECnet,
  1933. .create = dn_create,
  1934. .owner = THIS_MODULE,
  1935. };
  1936. static const struct proto_ops dn_proto_ops = {
  1937. .family = AF_DECnet,
  1938. .owner = THIS_MODULE,
  1939. .release = dn_release,
  1940. .bind = dn_bind,
  1941. .connect = dn_connect,
  1942. .socketpair = sock_no_socketpair,
  1943. .accept = dn_accept,
  1944. .getname = dn_getname,
  1945. .poll = dn_poll,
  1946. .ioctl = dn_ioctl,
  1947. .listen = dn_listen,
  1948. .shutdown = dn_shutdown,
  1949. .setsockopt = dn_setsockopt,
  1950. .getsockopt = dn_getsockopt,
  1951. .sendmsg = dn_sendmsg,
  1952. .recvmsg = dn_recvmsg,
  1953. .mmap = sock_no_mmap,
  1954. .sendpage = sock_no_sendpage,
  1955. };
  1956. MODULE_DESCRIPTION("The Linux DECnet Network Protocol");
  1957. MODULE_AUTHOR("Linux DECnet Project Team");
  1958. MODULE_LICENSE("GPL");
  1959. MODULE_ALIAS_NETPROTO(PF_DECnet);
  1960. static char banner[] __initdata = KERN_INFO "NET4: DECnet for Linux: V.2.5.68s (C) 1995-2003 Linux DECnet Project Team\n";
  1961. static int __init decnet_init(void)
  1962. {
  1963. int rc;
  1964. printk(banner);
  1965. rc = proto_register(&dn_proto, 1);
  1966. if (rc != 0)
  1967. goto out;
  1968. dn_neigh_init();
  1969. dn_dev_init();
  1970. dn_route_init();
  1971. dn_fib_init();
  1972. sock_register(&dn_family_ops);
  1973. dev_add_pack(&dn_dix_packet_type);
  1974. register_netdevice_notifier(&dn_dev_notifier);
  1975. proc_create("decnet", S_IRUGO, init_net.proc_net, &dn_socket_seq_fops);
  1976. dn_register_sysctl();
  1977. out:
  1978. return rc;
  1979. }
  1980. module_init(decnet_init);
  1981. /*
  1982. * Prevent DECnet module unloading until its fixed properly.
  1983. * Requires an audit of the code to check for memory leaks and
  1984. * initialisation problems etc.
  1985. */
  1986. #if 0
  1987. static void __exit decnet_exit(void)
  1988. {
  1989. sock_unregister(AF_DECnet);
  1990. rtnl_unregister_all(PF_DECnet);
  1991. dev_remove_pack(&dn_dix_packet_type);
  1992. dn_unregister_sysctl();
  1993. unregister_netdevice_notifier(&dn_dev_notifier);
  1994. dn_route_cleanup();
  1995. dn_dev_cleanup();
  1996. dn_neigh_cleanup();
  1997. dn_fib_cleanup();
  1998. remove_proc_entry("decnet", init_net.proc_net);
  1999. proto_unregister(&dn_proto);
  2000. rcu_barrier_bh(); /* Wait for completion of call_rcu_bh()'s */
  2001. }
  2002. module_exit(decnet_exit);
  2003. #endif