af_decnet.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424
  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/system.h>
  110. #include <asm/ioctls.h>
  111. #include <linux/capability.h>
  112. #include <linux/mm.h>
  113. #include <linux/interrupt.h>
  114. #include <linux/proc_fs.h>
  115. #include <linux/stat.h>
  116. #include <linux/init.h>
  117. #include <linux/poll.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. struct hlist_node *node;
  157. if (port == 0)
  158. return -1;
  159. sk_for_each(sk, node, &dn_sk_hash[le16_to_cpu(port) & DN_SK_HASH_MASK]) {
  160. struct dn_scp *scp = DN_SK(sk);
  161. if (scp->addrloc == port)
  162. return -1;
  163. }
  164. return 0;
  165. }
  166. static unsigned short port_alloc(struct sock *sk)
  167. {
  168. struct dn_scp *scp = DN_SK(sk);
  169. static unsigned short port = 0x2000;
  170. unsigned short i_port = port;
  171. while(check_port(cpu_to_le16(++port)) != 0) {
  172. if (port == i_port)
  173. return 0;
  174. }
  175. scp->addrloc = cpu_to_le16(port);
  176. return 1;
  177. }
  178. /*
  179. * Since this is only ever called from user
  180. * level, we don't need a write_lock() version
  181. * of this.
  182. */
  183. static int dn_hash_sock(struct sock *sk)
  184. {
  185. struct dn_scp *scp = DN_SK(sk);
  186. struct hlist_head *list;
  187. int rv = -EUSERS;
  188. BUG_ON(sk_hashed(sk));
  189. write_lock_bh(&dn_hash_lock);
  190. if (!scp->addrloc && !port_alloc(sk))
  191. goto out;
  192. rv = -EADDRINUSE;
  193. if ((list = dn_find_list(sk)) == NULL)
  194. goto out;
  195. sk_add_node(sk, list);
  196. rv = 0;
  197. out:
  198. write_unlock_bh(&dn_hash_lock);
  199. return rv;
  200. }
  201. static void dn_unhash_sock(struct sock *sk)
  202. {
  203. write_lock(&dn_hash_lock);
  204. sk_del_node_init(sk);
  205. write_unlock(&dn_hash_lock);
  206. }
  207. static void dn_unhash_sock_bh(struct sock *sk)
  208. {
  209. write_lock_bh(&dn_hash_lock);
  210. sk_del_node_init(sk);
  211. write_unlock_bh(&dn_hash_lock);
  212. }
  213. static struct hlist_head *listen_hash(struct sockaddr_dn *addr)
  214. {
  215. int i;
  216. unsigned hash = addr->sdn_objnum;
  217. if (hash == 0) {
  218. hash = addr->sdn_objnamel;
  219. for(i = 0; i < le16_to_cpu(addr->sdn_objnamel); i++) {
  220. hash ^= addr->sdn_objname[i];
  221. hash ^= (hash << 3);
  222. }
  223. }
  224. return &dn_sk_hash[hash & DN_SK_HASH_MASK];
  225. }
  226. /*
  227. * Called to transform a socket from bound (i.e. with a local address)
  228. * into a listening socket (doesn't need a local port number) and rehashes
  229. * based upon the object name/number.
  230. */
  231. static void dn_rehash_sock(struct sock *sk)
  232. {
  233. struct hlist_head *list;
  234. struct dn_scp *scp = DN_SK(sk);
  235. if (scp->addr.sdn_flags & SDF_WILD)
  236. return;
  237. write_lock_bh(&dn_hash_lock);
  238. sk_del_node_init(sk);
  239. DN_SK(sk)->addrloc = 0;
  240. list = listen_hash(&DN_SK(sk)->addr);
  241. sk_add_node(sk, list);
  242. write_unlock_bh(&dn_hash_lock);
  243. }
  244. int dn_sockaddr2username(struct sockaddr_dn *sdn, unsigned char *buf, unsigned char type)
  245. {
  246. int len = 2;
  247. *buf++ = type;
  248. switch(type) {
  249. case 0:
  250. *buf++ = sdn->sdn_objnum;
  251. break;
  252. case 1:
  253. *buf++ = 0;
  254. *buf++ = le16_to_cpu(sdn->sdn_objnamel);
  255. memcpy(buf, sdn->sdn_objname, le16_to_cpu(sdn->sdn_objnamel));
  256. len = 3 + le16_to_cpu(sdn->sdn_objnamel);
  257. break;
  258. case 2:
  259. memset(buf, 0, 5);
  260. buf += 5;
  261. *buf++ = le16_to_cpu(sdn->sdn_objnamel);
  262. memcpy(buf, sdn->sdn_objname, le16_to_cpu(sdn->sdn_objnamel));
  263. len = 7 + le16_to_cpu(sdn->sdn_objnamel);
  264. break;
  265. }
  266. return len;
  267. }
  268. /*
  269. * On reception of usernames, we handle types 1 and 0 for destination
  270. * addresses only. Types 2 and 4 are used for source addresses, but the
  271. * UIC, GIC are ignored and they are both treated the same way. Type 3
  272. * is never used as I've no idea what its purpose might be or what its
  273. * format is.
  274. */
  275. int dn_username2sockaddr(unsigned char *data, int len, struct sockaddr_dn *sdn, unsigned char *fmt)
  276. {
  277. unsigned char type;
  278. int size = len;
  279. int namel = 12;
  280. sdn->sdn_objnum = 0;
  281. sdn->sdn_objnamel = cpu_to_le16(0);
  282. memset(sdn->sdn_objname, 0, DN_MAXOBJL);
  283. if (len < 2)
  284. return -1;
  285. len -= 2;
  286. *fmt = *data++;
  287. type = *data++;
  288. switch(*fmt) {
  289. case 0:
  290. sdn->sdn_objnum = type;
  291. return 2;
  292. case 1:
  293. namel = 16;
  294. break;
  295. case 2:
  296. len -= 4;
  297. data += 4;
  298. break;
  299. case 4:
  300. len -= 8;
  301. data += 8;
  302. break;
  303. default:
  304. return -1;
  305. }
  306. len -= 1;
  307. if (len < 0)
  308. return -1;
  309. sdn->sdn_objnamel = cpu_to_le16(*data++);
  310. len -= le16_to_cpu(sdn->sdn_objnamel);
  311. if ((len < 0) || (le16_to_cpu(sdn->sdn_objnamel) > namel))
  312. return -1;
  313. memcpy(sdn->sdn_objname, data, le16_to_cpu(sdn->sdn_objnamel));
  314. return size - len;
  315. }
  316. struct sock *dn_sklist_find_listener(struct sockaddr_dn *addr)
  317. {
  318. struct hlist_head *list = listen_hash(addr);
  319. struct hlist_node *node;
  320. struct sock *sk;
  321. read_lock(&dn_hash_lock);
  322. sk_for_each(sk, node, list) {
  323. struct dn_scp *scp = DN_SK(sk);
  324. if (sk->sk_state != TCP_LISTEN)
  325. continue;
  326. if (scp->addr.sdn_objnum) {
  327. if (scp->addr.sdn_objnum != addr->sdn_objnum)
  328. continue;
  329. } else {
  330. if (addr->sdn_objnum)
  331. continue;
  332. if (scp->addr.sdn_objnamel != addr->sdn_objnamel)
  333. continue;
  334. if (memcmp(scp->addr.sdn_objname, addr->sdn_objname, le16_to_cpu(addr->sdn_objnamel)) != 0)
  335. continue;
  336. }
  337. sock_hold(sk);
  338. read_unlock(&dn_hash_lock);
  339. return sk;
  340. }
  341. sk = sk_head(&dn_wild_sk);
  342. if (sk) {
  343. if (sk->sk_state == TCP_LISTEN)
  344. sock_hold(sk);
  345. else
  346. sk = NULL;
  347. }
  348. read_unlock(&dn_hash_lock);
  349. return sk;
  350. }
  351. struct sock *dn_find_by_skb(struct sk_buff *skb)
  352. {
  353. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  354. struct sock *sk;
  355. struct hlist_node *node;
  356. struct dn_scp *scp;
  357. read_lock(&dn_hash_lock);
  358. sk_for_each(sk, node, &dn_sk_hash[le16_to_cpu(cb->dst_port) & DN_SK_HASH_MASK]) {
  359. scp = DN_SK(sk);
  360. if (cb->src != dn_saddr2dn(&scp->peer))
  361. continue;
  362. if (cb->dst_port != scp->addrloc)
  363. continue;
  364. if (scp->addrrem && (cb->src_port != scp->addrrem))
  365. continue;
  366. sock_hold(sk);
  367. goto found;
  368. }
  369. sk = NULL;
  370. found:
  371. read_unlock(&dn_hash_lock);
  372. return sk;
  373. }
  374. static void dn_destruct(struct sock *sk)
  375. {
  376. struct dn_scp *scp = DN_SK(sk);
  377. skb_queue_purge(&scp->data_xmit_queue);
  378. skb_queue_purge(&scp->other_xmit_queue);
  379. skb_queue_purge(&scp->other_receive_queue);
  380. dst_release(rcu_dereference_check(sk->sk_dst_cache, 1));
  381. }
  382. static int dn_memory_pressure;
  383. static void dn_enter_memory_pressure(struct sock *sk)
  384. {
  385. if (!dn_memory_pressure) {
  386. dn_memory_pressure = 1;
  387. }
  388. }
  389. static struct proto dn_proto = {
  390. .name = "NSP",
  391. .owner = THIS_MODULE,
  392. .enter_memory_pressure = dn_enter_memory_pressure,
  393. .memory_pressure = &dn_memory_pressure,
  394. .memory_allocated = &decnet_memory_allocated,
  395. .sysctl_mem = sysctl_decnet_mem,
  396. .sysctl_wmem = sysctl_decnet_wmem,
  397. .sysctl_rmem = sysctl_decnet_rmem,
  398. .max_header = DN_MAX_NSP_DATA_HEADER + 64,
  399. .obj_size = sizeof(struct dn_sock),
  400. };
  401. static struct sock *dn_alloc_sock(struct net *net, struct socket *sock, gfp_t gfp)
  402. {
  403. struct dn_scp *scp;
  404. struct sock *sk = sk_alloc(net, PF_DECnet, gfp, &dn_proto);
  405. if (!sk)
  406. goto out;
  407. if (sock)
  408. sock->ops = &dn_proto_ops;
  409. sock_init_data(sock, sk);
  410. sk->sk_backlog_rcv = dn_nsp_backlog_rcv;
  411. sk->sk_destruct = dn_destruct;
  412. sk->sk_no_check = 1;
  413. sk->sk_family = PF_DECnet;
  414. sk->sk_protocol = 0;
  415. sk->sk_allocation = gfp;
  416. sk->sk_sndbuf = sysctl_decnet_wmem[1];
  417. sk->sk_rcvbuf = sysctl_decnet_rmem[1];
  418. /* Initialization of DECnet Session Control Port */
  419. scp = DN_SK(sk);
  420. scp->state = DN_O; /* Open */
  421. scp->numdat = 1; /* Next data seg to tx */
  422. scp->numoth = 1; /* Next oth data to tx */
  423. scp->ackxmt_dat = 0; /* Last data seg ack'ed */
  424. scp->ackxmt_oth = 0; /* Last oth data ack'ed */
  425. scp->ackrcv_dat = 0; /* Highest data ack recv*/
  426. scp->ackrcv_oth = 0; /* Last oth data ack rec*/
  427. scp->flowrem_sw = DN_SEND;
  428. scp->flowloc_sw = DN_SEND;
  429. scp->flowrem_dat = 0;
  430. scp->flowrem_oth = 1;
  431. scp->flowloc_dat = 0;
  432. scp->flowloc_oth = 1;
  433. scp->services_rem = 0;
  434. scp->services_loc = 1 | NSP_FC_NONE;
  435. scp->info_rem = 0;
  436. scp->info_loc = 0x03; /* NSP version 4.1 */
  437. scp->segsize_rem = 230 - DN_MAX_NSP_DATA_HEADER; /* Default: Updated by remote segsize */
  438. scp->nonagle = 0;
  439. scp->multi_ireq = 1;
  440. scp->accept_mode = ACC_IMMED;
  441. scp->addr.sdn_family = AF_DECnet;
  442. scp->peer.sdn_family = AF_DECnet;
  443. scp->accessdata.acc_accl = 5;
  444. memcpy(scp->accessdata.acc_acc, "LINUX", 5);
  445. scp->max_window = NSP_MAX_WINDOW;
  446. scp->snd_window = NSP_MIN_WINDOW;
  447. scp->nsp_srtt = NSP_INITIAL_SRTT;
  448. scp->nsp_rttvar = NSP_INITIAL_RTTVAR;
  449. scp->nsp_rxtshift = 0;
  450. skb_queue_head_init(&scp->data_xmit_queue);
  451. skb_queue_head_init(&scp->other_xmit_queue);
  452. skb_queue_head_init(&scp->other_receive_queue);
  453. scp->persist = 0;
  454. scp->persist_fxn = NULL;
  455. scp->keepalive = 10 * HZ;
  456. scp->keepalive_fxn = dn_keepalive;
  457. init_timer(&scp->delack_timer);
  458. scp->delack_pending = 0;
  459. scp->delack_fxn = dn_nsp_delayed_ack;
  460. dn_start_slow_timer(sk);
  461. out:
  462. return sk;
  463. }
  464. /*
  465. * Keepalive timer.
  466. * FIXME: Should respond to SO_KEEPALIVE etc.
  467. */
  468. static void dn_keepalive(struct sock *sk)
  469. {
  470. struct dn_scp *scp = DN_SK(sk);
  471. /*
  472. * By checking the other_data transmit queue is empty
  473. * we are double checking that we are not sending too
  474. * many of these keepalive frames.
  475. */
  476. if (skb_queue_empty(&scp->other_xmit_queue))
  477. dn_nsp_send_link(sk, DN_NOCHANGE, 0);
  478. }
  479. /*
  480. * Timer for shutdown/destroyed sockets.
  481. * When socket is dead & no packets have been sent for a
  482. * certain amount of time, they are removed by this
  483. * routine. Also takes care of sending out DI & DC
  484. * frames at correct times.
  485. */
  486. int dn_destroy_timer(struct sock *sk)
  487. {
  488. struct dn_scp *scp = DN_SK(sk);
  489. scp->persist = dn_nsp_persist(sk);
  490. switch(scp->state) {
  491. case DN_DI:
  492. dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC);
  493. if (scp->nsp_rxtshift >= decnet_di_count)
  494. scp->state = DN_CN;
  495. return 0;
  496. case DN_DR:
  497. dn_nsp_send_disc(sk, NSP_DISCINIT, 0, GFP_ATOMIC);
  498. if (scp->nsp_rxtshift >= decnet_dr_count)
  499. scp->state = DN_DRC;
  500. return 0;
  501. case DN_DN:
  502. if (scp->nsp_rxtshift < decnet_dn_count) {
  503. /* printk(KERN_DEBUG "dn_destroy_timer: DN\n"); */
  504. dn_nsp_send_disc(sk, NSP_DISCCONF, NSP_REASON_DC, 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 (!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)) == 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. if (sock->state == SS_CONNECTED)
  767. goto out;
  768. if (sock->state == SS_CONNECTING) {
  769. err = 0;
  770. if (scp->state == DN_RUN) {
  771. sock->state = SS_CONNECTED;
  772. goto out;
  773. }
  774. err = -ECONNREFUSED;
  775. if (scp->state != DN_CI && scp->state != DN_CC) {
  776. sock->state = SS_UNCONNECTED;
  777. goto out;
  778. }
  779. return dn_wait_run(sk, timeo);
  780. }
  781. err = -EINVAL;
  782. if (scp->state != DN_O)
  783. goto out;
  784. if (addr == NULL || addrlen != sizeof(struct sockaddr_dn))
  785. goto out;
  786. if (addr->sdn_family != AF_DECnet)
  787. goto out;
  788. if (addr->sdn_flags & SDF_WILD)
  789. goto out;
  790. if (sock_flag(sk, SOCK_ZAPPED)) {
  791. err = dn_auto_bind(sk->sk_socket);
  792. if (err)
  793. goto out;
  794. }
  795. memcpy(&scp->peer, addr, sizeof(struct sockaddr_dn));
  796. err = -EHOSTUNREACH;
  797. memset(&fld, 0, sizeof(fld));
  798. fld.flowidn_oif = sk->sk_bound_dev_if;
  799. fld.daddr = dn_saddr2dn(&scp->peer);
  800. fld.saddr = dn_saddr2dn(&scp->addr);
  801. dn_sk_ports_copy(&fld, scp);
  802. fld.flowidn_proto = DNPROTO_NSP;
  803. if (dn_route_output_sock(&sk->sk_dst_cache, &fld, sk, flags) < 0)
  804. goto out;
  805. sk->sk_route_caps = sk->sk_dst_cache->dev->features;
  806. sock->state = SS_CONNECTING;
  807. scp->state = DN_CI;
  808. scp->segsize_loc = dst_metric_advmss(sk->sk_dst_cache);
  809. dn_nsp_send_conninit(sk, NSP_CI);
  810. err = -EINPROGRESS;
  811. if (*timeo) {
  812. err = dn_wait_run(sk, timeo);
  813. }
  814. out:
  815. return err;
  816. }
  817. static int dn_connect(struct socket *sock, struct sockaddr *uaddr, int addrlen, int flags)
  818. {
  819. struct sockaddr_dn *addr = (struct sockaddr_dn *)uaddr;
  820. struct sock *sk = sock->sk;
  821. int err;
  822. long timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  823. lock_sock(sk);
  824. err = __dn_connect(sk, addr, addrlen, &timeo, 0);
  825. release_sock(sk);
  826. return err;
  827. }
  828. static inline int dn_check_state(struct sock *sk, struct sockaddr_dn *addr, int addrlen, long *timeo, int flags)
  829. {
  830. struct dn_scp *scp = DN_SK(sk);
  831. switch(scp->state) {
  832. case DN_RUN:
  833. return 0;
  834. case DN_CR:
  835. return dn_confirm_accept(sk, timeo, sk->sk_allocation);
  836. case DN_CI:
  837. case DN_CC:
  838. return dn_wait_run(sk, timeo);
  839. case DN_O:
  840. return __dn_connect(sk, addr, addrlen, timeo, flags);
  841. }
  842. return -EINVAL;
  843. }
  844. static void dn_access_copy(struct sk_buff *skb, struct accessdata_dn *acc)
  845. {
  846. unsigned char *ptr = skb->data;
  847. acc->acc_userl = *ptr++;
  848. memcpy(&acc->acc_user, ptr, acc->acc_userl);
  849. ptr += acc->acc_userl;
  850. acc->acc_passl = *ptr++;
  851. memcpy(&acc->acc_pass, ptr, acc->acc_passl);
  852. ptr += acc->acc_passl;
  853. acc->acc_accl = *ptr++;
  854. memcpy(&acc->acc_acc, ptr, acc->acc_accl);
  855. skb_pull(skb, acc->acc_accl + acc->acc_passl + acc->acc_userl + 3);
  856. }
  857. static void dn_user_copy(struct sk_buff *skb, struct optdata_dn *opt)
  858. {
  859. unsigned char *ptr = skb->data;
  860. u16 len = *ptr++; /* yes, it's 8bit on the wire */
  861. BUG_ON(len > 16); /* we've checked the contents earlier */
  862. opt->opt_optl = cpu_to_le16(len);
  863. opt->opt_status = 0;
  864. memcpy(opt->opt_data, ptr, len);
  865. skb_pull(skb, len + 1);
  866. }
  867. static struct sk_buff *dn_wait_for_connect(struct sock *sk, long *timeo)
  868. {
  869. DEFINE_WAIT(wait);
  870. struct sk_buff *skb = NULL;
  871. int err = 0;
  872. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  873. for(;;) {
  874. release_sock(sk);
  875. skb = skb_dequeue(&sk->sk_receive_queue);
  876. if (skb == NULL) {
  877. *timeo = schedule_timeout(*timeo);
  878. skb = skb_dequeue(&sk->sk_receive_queue);
  879. }
  880. lock_sock(sk);
  881. if (skb != NULL)
  882. break;
  883. err = -EINVAL;
  884. if (sk->sk_state != TCP_LISTEN)
  885. break;
  886. err = sock_intr_errno(*timeo);
  887. if (signal_pending(current))
  888. break;
  889. err = -EAGAIN;
  890. if (!*timeo)
  891. break;
  892. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  893. }
  894. finish_wait(sk_sleep(sk), &wait);
  895. return skb == NULL ? ERR_PTR(err) : skb;
  896. }
  897. static int dn_accept(struct socket *sock, struct socket *newsock, int flags)
  898. {
  899. struct sock *sk = sock->sk, *newsk;
  900. struct sk_buff *skb = NULL;
  901. struct dn_skb_cb *cb;
  902. unsigned char menuver;
  903. int err = 0;
  904. unsigned char type;
  905. long timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
  906. struct dst_entry *dst;
  907. lock_sock(sk);
  908. if (sk->sk_state != TCP_LISTEN || DN_SK(sk)->state != DN_O) {
  909. release_sock(sk);
  910. return -EINVAL;
  911. }
  912. skb = skb_dequeue(&sk->sk_receive_queue);
  913. if (skb == NULL) {
  914. skb = dn_wait_for_connect(sk, &timeo);
  915. if (IS_ERR(skb)) {
  916. release_sock(sk);
  917. return PTR_ERR(skb);
  918. }
  919. }
  920. cb = DN_SKB_CB(skb);
  921. sk->sk_ack_backlog--;
  922. newsk = dn_alloc_sock(sock_net(sk), newsock, sk->sk_allocation);
  923. if (newsk == NULL) {
  924. release_sock(sk);
  925. kfree_skb(skb);
  926. return -ENOBUFS;
  927. }
  928. release_sock(sk);
  929. dst = skb_dst(skb);
  930. sk_dst_set(newsk, dst);
  931. skb_dst_set(skb, NULL);
  932. DN_SK(newsk)->state = DN_CR;
  933. DN_SK(newsk)->addrrem = cb->src_port;
  934. DN_SK(newsk)->services_rem = cb->services;
  935. DN_SK(newsk)->info_rem = cb->info;
  936. DN_SK(newsk)->segsize_rem = cb->segsize;
  937. DN_SK(newsk)->accept_mode = DN_SK(sk)->accept_mode;
  938. if (DN_SK(newsk)->segsize_rem < 230)
  939. DN_SK(newsk)->segsize_rem = 230;
  940. if ((DN_SK(newsk)->services_rem & NSP_FC_MASK) == NSP_FC_NONE)
  941. DN_SK(newsk)->max_window = decnet_no_fc_max_cwnd;
  942. newsk->sk_state = TCP_LISTEN;
  943. memcpy(&(DN_SK(newsk)->addr), &(DN_SK(sk)->addr), sizeof(struct sockaddr_dn));
  944. /*
  945. * If we are listening on a wild socket, we don't want
  946. * the newly created socket on the wrong hash queue.
  947. */
  948. DN_SK(newsk)->addr.sdn_flags &= ~SDF_WILD;
  949. skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->addr), &type));
  950. skb_pull(skb, dn_username2sockaddr(skb->data, skb->len, &(DN_SK(newsk)->peer), &type));
  951. *(__le16 *)(DN_SK(newsk)->peer.sdn_add.a_addr) = cb->src;
  952. *(__le16 *)(DN_SK(newsk)->addr.sdn_add.a_addr) = cb->dst;
  953. menuver = *skb->data;
  954. skb_pull(skb, 1);
  955. if (menuver & DN_MENUVER_ACC)
  956. dn_access_copy(skb, &(DN_SK(newsk)->accessdata));
  957. if (menuver & DN_MENUVER_USR)
  958. dn_user_copy(skb, &(DN_SK(newsk)->conndata_in));
  959. if (menuver & DN_MENUVER_PRX)
  960. DN_SK(newsk)->peer.sdn_flags |= SDF_PROXY;
  961. if (menuver & DN_MENUVER_UIC)
  962. DN_SK(newsk)->peer.sdn_flags |= SDF_UICPROXY;
  963. kfree_skb(skb);
  964. memcpy(&(DN_SK(newsk)->conndata_out), &(DN_SK(sk)->conndata_out),
  965. sizeof(struct optdata_dn));
  966. memcpy(&(DN_SK(newsk)->discdata_out), &(DN_SK(sk)->discdata_out),
  967. sizeof(struct optdata_dn));
  968. lock_sock(newsk);
  969. err = dn_hash_sock(newsk);
  970. if (err == 0) {
  971. sock_reset_flag(newsk, SOCK_ZAPPED);
  972. dn_send_conn_ack(newsk);
  973. /*
  974. * Here we use sk->sk_allocation since although the conn conf is
  975. * for the newsk, the context is the old socket.
  976. */
  977. if (DN_SK(newsk)->accept_mode == ACC_IMMED)
  978. err = dn_confirm_accept(newsk, &timeo,
  979. sk->sk_allocation);
  980. }
  981. release_sock(newsk);
  982. return err;
  983. }
  984. static int dn_getname(struct socket *sock, struct sockaddr *uaddr,int *uaddr_len,int peer)
  985. {
  986. struct sockaddr_dn *sa = (struct sockaddr_dn *)uaddr;
  987. struct sock *sk = sock->sk;
  988. struct dn_scp *scp = DN_SK(sk);
  989. *uaddr_len = sizeof(struct sockaddr_dn);
  990. lock_sock(sk);
  991. if (peer) {
  992. if ((sock->state != SS_CONNECTED &&
  993. sock->state != SS_CONNECTING) &&
  994. scp->accept_mode == ACC_IMMED) {
  995. release_sock(sk);
  996. return -ENOTCONN;
  997. }
  998. memcpy(sa, &scp->peer, sizeof(struct sockaddr_dn));
  999. } else {
  1000. memcpy(sa, &scp->addr, sizeof(struct sockaddr_dn));
  1001. }
  1002. release_sock(sk);
  1003. return 0;
  1004. }
  1005. static unsigned int dn_poll(struct file *file, struct socket *sock, poll_table *wait)
  1006. {
  1007. struct sock *sk = sock->sk;
  1008. struct dn_scp *scp = DN_SK(sk);
  1009. int mask = datagram_poll(file, sock, wait);
  1010. if (!skb_queue_empty(&scp->other_receive_queue))
  1011. mask |= POLLRDBAND;
  1012. return mask;
  1013. }
  1014. static int dn_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  1015. {
  1016. struct sock *sk = sock->sk;
  1017. struct dn_scp *scp = DN_SK(sk);
  1018. int err = -EOPNOTSUPP;
  1019. long amount = 0;
  1020. struct sk_buff *skb;
  1021. int val;
  1022. switch(cmd)
  1023. {
  1024. case SIOCGIFADDR:
  1025. case SIOCSIFADDR:
  1026. return dn_dev_ioctl(cmd, (void __user *)arg);
  1027. case SIOCATMARK:
  1028. lock_sock(sk);
  1029. val = !skb_queue_empty(&scp->other_receive_queue);
  1030. if (scp->state != DN_RUN)
  1031. val = -ENOTCONN;
  1032. release_sock(sk);
  1033. return val;
  1034. case TIOCOUTQ:
  1035. amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
  1036. if (amount < 0)
  1037. amount = 0;
  1038. err = put_user(amount, (int __user *)arg);
  1039. break;
  1040. case TIOCINQ:
  1041. lock_sock(sk);
  1042. skb = skb_peek(&scp->other_receive_queue);
  1043. if (skb) {
  1044. amount = skb->len;
  1045. } else {
  1046. skb_queue_walk(&sk->sk_receive_queue, skb)
  1047. amount += skb->len;
  1048. }
  1049. release_sock(sk);
  1050. err = put_user(amount, (int __user *)arg);
  1051. break;
  1052. default:
  1053. err = -ENOIOCTLCMD;
  1054. break;
  1055. }
  1056. return err;
  1057. }
  1058. static int dn_listen(struct socket *sock, int backlog)
  1059. {
  1060. struct sock *sk = sock->sk;
  1061. int err = -EINVAL;
  1062. lock_sock(sk);
  1063. if (sock_flag(sk, SOCK_ZAPPED))
  1064. goto out;
  1065. if ((DN_SK(sk)->state != DN_O) || (sk->sk_state == TCP_LISTEN))
  1066. goto out;
  1067. sk->sk_max_ack_backlog = backlog;
  1068. sk->sk_ack_backlog = 0;
  1069. sk->sk_state = TCP_LISTEN;
  1070. err = 0;
  1071. dn_rehash_sock(sk);
  1072. out:
  1073. release_sock(sk);
  1074. return err;
  1075. }
  1076. static int dn_shutdown(struct socket *sock, int how)
  1077. {
  1078. struct sock *sk = sock->sk;
  1079. struct dn_scp *scp = DN_SK(sk);
  1080. int err = -ENOTCONN;
  1081. lock_sock(sk);
  1082. if (sock->state == SS_UNCONNECTED)
  1083. goto out;
  1084. err = 0;
  1085. if (sock->state == SS_DISCONNECTING)
  1086. goto out;
  1087. err = -EINVAL;
  1088. if (scp->state == DN_O)
  1089. goto out;
  1090. if (how != SHUTDOWN_MASK)
  1091. goto out;
  1092. sk->sk_shutdown = how;
  1093. dn_destroy_sock(sk);
  1094. err = 0;
  1095. out:
  1096. release_sock(sk);
  1097. return err;
  1098. }
  1099. static int dn_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
  1100. {
  1101. struct sock *sk = sock->sk;
  1102. int err;
  1103. lock_sock(sk);
  1104. err = __dn_setsockopt(sock, level, optname, optval, optlen, 0);
  1105. release_sock(sk);
  1106. return err;
  1107. }
  1108. static int __dn_setsockopt(struct socket *sock, int level,int optname, char __user *optval, unsigned int optlen, int flags)
  1109. {
  1110. struct sock *sk = sock->sk;
  1111. struct dn_scp *scp = DN_SK(sk);
  1112. long timeo;
  1113. union {
  1114. struct optdata_dn opt;
  1115. struct accessdata_dn acc;
  1116. int mode;
  1117. unsigned long win;
  1118. int val;
  1119. unsigned char services;
  1120. unsigned char info;
  1121. } u;
  1122. int err;
  1123. if (optlen && !optval)
  1124. return -EINVAL;
  1125. if (optlen > sizeof(u))
  1126. return -EINVAL;
  1127. if (copy_from_user(&u, optval, optlen))
  1128. return -EFAULT;
  1129. switch(optname) {
  1130. case DSO_CONDATA:
  1131. if (sock->state == SS_CONNECTED)
  1132. return -EISCONN;
  1133. if ((scp->state != DN_O) && (scp->state != DN_CR))
  1134. return -EINVAL;
  1135. if (optlen != sizeof(struct optdata_dn))
  1136. return -EINVAL;
  1137. if (le16_to_cpu(u.opt.opt_optl) > 16)
  1138. return -EINVAL;
  1139. memcpy(&scp->conndata_out, &u.opt, optlen);
  1140. break;
  1141. case DSO_DISDATA:
  1142. if (sock->state != SS_CONNECTED && scp->accept_mode == ACC_IMMED)
  1143. return -ENOTCONN;
  1144. if (optlen != sizeof(struct optdata_dn))
  1145. return -EINVAL;
  1146. if (le16_to_cpu(u.opt.opt_optl) > 16)
  1147. return -EINVAL;
  1148. memcpy(&scp->discdata_out, &u.opt, optlen);
  1149. break;
  1150. case DSO_CONACCESS:
  1151. if (sock->state == SS_CONNECTED)
  1152. return -EISCONN;
  1153. if (scp->state != DN_O)
  1154. return -EINVAL;
  1155. if (optlen != sizeof(struct accessdata_dn))
  1156. return -EINVAL;
  1157. if ((u.acc.acc_accl > DN_MAXACCL) ||
  1158. (u.acc.acc_passl > DN_MAXACCL) ||
  1159. (u.acc.acc_userl > DN_MAXACCL))
  1160. return -EINVAL;
  1161. memcpy(&scp->accessdata, &u.acc, optlen);
  1162. break;
  1163. case DSO_ACCEPTMODE:
  1164. if (sock->state == SS_CONNECTED)
  1165. return -EISCONN;
  1166. if (scp->state != DN_O)
  1167. return -EINVAL;
  1168. if (optlen != sizeof(int))
  1169. return -EINVAL;
  1170. if ((u.mode != ACC_IMMED) && (u.mode != ACC_DEFER))
  1171. return -EINVAL;
  1172. scp->accept_mode = (unsigned char)u.mode;
  1173. break;
  1174. case DSO_CONACCEPT:
  1175. if (scp->state != DN_CR)
  1176. return -EINVAL;
  1177. timeo = sock_rcvtimeo(sk, 0);
  1178. err = dn_confirm_accept(sk, &timeo, sk->sk_allocation);
  1179. return err;
  1180. case DSO_CONREJECT:
  1181. if (scp->state != DN_CR)
  1182. return -EINVAL;
  1183. scp->state = DN_DR;
  1184. sk->sk_shutdown = SHUTDOWN_MASK;
  1185. dn_nsp_send_disc(sk, 0x38, 0, sk->sk_allocation);
  1186. break;
  1187. default:
  1188. #ifdef CONFIG_NETFILTER
  1189. return nf_setsockopt(sk, PF_DECnet, optname, optval, optlen);
  1190. #endif
  1191. case DSO_LINKINFO:
  1192. case DSO_STREAM:
  1193. case DSO_SEQPACKET:
  1194. return -ENOPROTOOPT;
  1195. case DSO_MAXWINDOW:
  1196. if (optlen != sizeof(unsigned long))
  1197. return -EINVAL;
  1198. if (u.win > NSP_MAX_WINDOW)
  1199. u.win = NSP_MAX_WINDOW;
  1200. if (u.win == 0)
  1201. return -EINVAL;
  1202. scp->max_window = u.win;
  1203. if (scp->snd_window > u.win)
  1204. scp->snd_window = u.win;
  1205. break;
  1206. case DSO_NODELAY:
  1207. if (optlen != sizeof(int))
  1208. return -EINVAL;
  1209. if (scp->nonagle == 2)
  1210. return -EINVAL;
  1211. scp->nonagle = (u.val == 0) ? 0 : 1;
  1212. /* if (scp->nonagle == 1) { Push pending frames } */
  1213. break;
  1214. case DSO_CORK:
  1215. if (optlen != sizeof(int))
  1216. return -EINVAL;
  1217. if (scp->nonagle == 1)
  1218. return -EINVAL;
  1219. scp->nonagle = (u.val == 0) ? 0 : 2;
  1220. /* if (scp->nonagle == 0) { Push pending frames } */
  1221. break;
  1222. case DSO_SERVICES:
  1223. if (optlen != sizeof(unsigned char))
  1224. return -EINVAL;
  1225. if ((u.services & ~NSP_FC_MASK) != 0x01)
  1226. return -EINVAL;
  1227. if ((u.services & NSP_FC_MASK) == NSP_FC_MASK)
  1228. return -EINVAL;
  1229. scp->services_loc = u.services;
  1230. break;
  1231. case DSO_INFO:
  1232. if (optlen != sizeof(unsigned char))
  1233. return -EINVAL;
  1234. if (u.info & 0xfc)
  1235. return -EINVAL;
  1236. scp->info_loc = u.info;
  1237. break;
  1238. }
  1239. return 0;
  1240. }
  1241. static int dn_getsockopt(struct socket *sock, int level, int optname, char __user *optval, int __user *optlen)
  1242. {
  1243. struct sock *sk = sock->sk;
  1244. int err;
  1245. lock_sock(sk);
  1246. err = __dn_getsockopt(sock, level, optname, optval, optlen, 0);
  1247. release_sock(sk);
  1248. return err;
  1249. }
  1250. static int __dn_getsockopt(struct socket *sock, int level,int optname, char __user *optval,int __user *optlen, int flags)
  1251. {
  1252. struct sock *sk = sock->sk;
  1253. struct dn_scp *scp = DN_SK(sk);
  1254. struct linkinfo_dn link;
  1255. unsigned int r_len;
  1256. void *r_data = NULL;
  1257. unsigned int val;
  1258. if(get_user(r_len , optlen))
  1259. return -EFAULT;
  1260. switch(optname) {
  1261. case DSO_CONDATA:
  1262. if (r_len > sizeof(struct optdata_dn))
  1263. r_len = sizeof(struct optdata_dn);
  1264. r_data = &scp->conndata_in;
  1265. break;
  1266. case DSO_DISDATA:
  1267. if (r_len > sizeof(struct optdata_dn))
  1268. r_len = sizeof(struct optdata_dn);
  1269. r_data = &scp->discdata_in;
  1270. break;
  1271. case DSO_CONACCESS:
  1272. if (r_len > sizeof(struct accessdata_dn))
  1273. r_len = sizeof(struct accessdata_dn);
  1274. r_data = &scp->accessdata;
  1275. break;
  1276. case DSO_ACCEPTMODE:
  1277. if (r_len > sizeof(unsigned char))
  1278. r_len = sizeof(unsigned char);
  1279. r_data = &scp->accept_mode;
  1280. break;
  1281. case DSO_LINKINFO:
  1282. if (r_len > sizeof(struct linkinfo_dn))
  1283. r_len = sizeof(struct linkinfo_dn);
  1284. memset(&link, 0, sizeof(link));
  1285. switch(sock->state) {
  1286. case SS_CONNECTING:
  1287. link.idn_linkstate = LL_CONNECTING;
  1288. break;
  1289. case SS_DISCONNECTING:
  1290. link.idn_linkstate = LL_DISCONNECTING;
  1291. break;
  1292. case SS_CONNECTED:
  1293. link.idn_linkstate = LL_RUNNING;
  1294. break;
  1295. default:
  1296. link.idn_linkstate = LL_INACTIVE;
  1297. }
  1298. link.idn_segsize = scp->segsize_rem;
  1299. r_data = &link;
  1300. break;
  1301. default:
  1302. #ifdef CONFIG_NETFILTER
  1303. {
  1304. int ret, len;
  1305. if(get_user(len, optlen))
  1306. return -EFAULT;
  1307. ret = nf_getsockopt(sk, PF_DECnet, optname,
  1308. optval, &len);
  1309. if (ret >= 0)
  1310. ret = put_user(len, optlen);
  1311. return ret;
  1312. }
  1313. #endif
  1314. case DSO_STREAM:
  1315. case DSO_SEQPACKET:
  1316. case DSO_CONACCEPT:
  1317. case DSO_CONREJECT:
  1318. return -ENOPROTOOPT;
  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. }
  1347. if (r_data) {
  1348. if (copy_to_user(optval, r_data, r_len))
  1349. return -EFAULT;
  1350. if (put_user(r_len, optlen))
  1351. return -EFAULT;
  1352. }
  1353. return 0;
  1354. }
  1355. static int dn_data_ready(struct sock *sk, struct sk_buff_head *q, int flags, int target)
  1356. {
  1357. struct sk_buff *skb;
  1358. int len = 0;
  1359. if (flags & MSG_OOB)
  1360. return !skb_queue_empty(q) ? 1 : 0;
  1361. skb_queue_walk(q, skb) {
  1362. struct dn_skb_cb *cb = DN_SKB_CB(skb);
  1363. len += skb->len;
  1364. if (cb->nsp_flags & 0x40) {
  1365. /* SOCK_SEQPACKET reads to EOM */
  1366. if (sk->sk_type == SOCK_SEQPACKET)
  1367. return 1;
  1368. /* so does SOCK_STREAM unless WAITALL is specified */
  1369. if (!(flags & MSG_WAITALL))
  1370. return 1;
  1371. }
  1372. /* minimum data length for read exceeded */
  1373. if (len >= target)
  1374. return 1;
  1375. }
  1376. return 0;
  1377. }
  1378. static int dn_recvmsg(struct kiocb *iocb, struct socket *sock,
  1379. struct msghdr *msg, size_t size, int flags)
  1380. {
  1381. struct sock *sk = sock->sk;
  1382. struct dn_scp *scp = DN_SK(sk);
  1383. struct sk_buff_head *queue = &sk->sk_receive_queue;
  1384. size_t target = size > 1 ? 1 : 0;
  1385. size_t copied = 0;
  1386. int rv = 0;
  1387. struct sk_buff *skb, *n;
  1388. struct dn_skb_cb *cb = NULL;
  1389. unsigned char eor = 0;
  1390. long timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
  1391. lock_sock(sk);
  1392. if (sock_flag(sk, SOCK_ZAPPED)) {
  1393. rv = -EADDRNOTAVAIL;
  1394. goto out;
  1395. }
  1396. if (sk->sk_shutdown & RCV_SHUTDOWN) {
  1397. rv = 0;
  1398. goto out;
  1399. }
  1400. rv = dn_check_state(sk, NULL, 0, &timeo, flags);
  1401. if (rv)
  1402. goto out;
  1403. if (flags & ~(MSG_CMSG_COMPAT|MSG_PEEK|MSG_OOB|MSG_WAITALL|MSG_DONTWAIT|MSG_NOSIGNAL)) {
  1404. rv = -EOPNOTSUPP;
  1405. goto out;
  1406. }
  1407. if (flags & MSG_OOB)
  1408. queue = &scp->other_receive_queue;
  1409. if (flags & MSG_WAITALL)
  1410. target = size;
  1411. /*
  1412. * See if there is data ready to read, sleep if there isn't
  1413. */
  1414. for(;;) {
  1415. DEFINE_WAIT(wait);
  1416. if (sk->sk_err)
  1417. goto out;
  1418. if (!skb_queue_empty(&scp->other_receive_queue)) {
  1419. if (!(flags & MSG_OOB)) {
  1420. msg->msg_flags |= MSG_OOB;
  1421. if (!scp->other_report) {
  1422. scp->other_report = 1;
  1423. goto out;
  1424. }
  1425. }
  1426. }
  1427. if (scp->state != DN_RUN)
  1428. goto out;
  1429. if (signal_pending(current)) {
  1430. rv = sock_intr_errno(timeo);
  1431. goto out;
  1432. }
  1433. if (dn_data_ready(sk, queue, flags, target))
  1434. break;
  1435. if (flags & MSG_DONTWAIT) {
  1436. rv = -EWOULDBLOCK;
  1437. goto out;
  1438. }
  1439. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1440. set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  1441. sk_wait_event(sk, &timeo, dn_data_ready(sk, queue, flags, target));
  1442. clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  1443. finish_wait(sk_sleep(sk), &wait);
  1444. }
  1445. skb_queue_walk_safe(queue, skb, n) {
  1446. unsigned int chunk = skb->len;
  1447. cb = DN_SKB_CB(skb);
  1448. if ((chunk + copied) > size)
  1449. chunk = size - copied;
  1450. if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
  1451. rv = -EFAULT;
  1452. break;
  1453. }
  1454. copied += chunk;
  1455. if (!(flags & MSG_PEEK))
  1456. skb_pull(skb, chunk);
  1457. eor = cb->nsp_flags & 0x40;
  1458. if (skb->len == 0) {
  1459. skb_unlink(skb, queue);
  1460. kfree_skb(skb);
  1461. /*
  1462. * N.B. Don't refer to skb or cb after this point
  1463. * in loop.
  1464. */
  1465. if ((scp->flowloc_sw == DN_DONTSEND) && !dn_congested(sk)) {
  1466. scp->flowloc_sw = DN_SEND;
  1467. dn_nsp_send_link(sk, DN_SEND, 0);
  1468. }
  1469. }
  1470. if (eor) {
  1471. if (sk->sk_type == SOCK_SEQPACKET)
  1472. break;
  1473. if (!(flags & MSG_WAITALL))
  1474. break;
  1475. }
  1476. if (flags & MSG_OOB)
  1477. break;
  1478. if (copied >= target)
  1479. break;
  1480. }
  1481. rv = copied;
  1482. if (eor && (sk->sk_type == SOCK_SEQPACKET))
  1483. msg->msg_flags |= MSG_EOR;
  1484. out:
  1485. if (rv == 0)
  1486. rv = (flags & MSG_PEEK) ? -sk->sk_err : sock_error(sk);
  1487. if ((rv >= 0) && msg->msg_name) {
  1488. memcpy(msg->msg_name, &scp->peer, sizeof(struct sockaddr_dn));
  1489. msg->msg_namelen = sizeof(struct sockaddr_dn);
  1490. }
  1491. release_sock(sk);
  1492. return rv;
  1493. }
  1494. static inline int dn_queue_too_long(struct dn_scp *scp, struct sk_buff_head *queue, int flags)
  1495. {
  1496. unsigned char fctype = scp->services_rem & NSP_FC_MASK;
  1497. if (skb_queue_len(queue) >= scp->snd_window)
  1498. return 1;
  1499. if (fctype != NSP_FC_NONE) {
  1500. if (flags & MSG_OOB) {
  1501. if (scp->flowrem_oth == 0)
  1502. return 1;
  1503. } else {
  1504. if (scp->flowrem_dat == 0)
  1505. return 1;
  1506. }
  1507. }
  1508. return 0;
  1509. }
  1510. /*
  1511. * The DECnet spec requires that the "routing layer" accepts packets which
  1512. * are at least 230 bytes in size. This excludes any headers which the NSP
  1513. * layer might add, so we always assume that we'll be using the maximal
  1514. * length header on data packets. The variation in length is due to the
  1515. * inclusion (or not) of the two 16 bit acknowledgement fields so it doesn't
  1516. * make much practical difference.
  1517. */
  1518. unsigned dn_mss_from_pmtu(struct net_device *dev, int mtu)
  1519. {
  1520. unsigned mss = 230 - DN_MAX_NSP_DATA_HEADER;
  1521. if (dev) {
  1522. struct dn_dev *dn_db = rcu_dereference_raw(dev->dn_ptr);
  1523. mtu -= LL_RESERVED_SPACE(dev);
  1524. if (dn_db->use_long)
  1525. mtu -= 21;
  1526. else
  1527. mtu -= 6;
  1528. mtu -= DN_MAX_NSP_DATA_HEADER;
  1529. } else {
  1530. /*
  1531. * 21 = long header, 16 = guess at MAC header length
  1532. */
  1533. mtu -= (21 + DN_MAX_NSP_DATA_HEADER + 16);
  1534. }
  1535. if (mtu > mss)
  1536. mss = mtu;
  1537. return mss;
  1538. }
  1539. static inline unsigned int dn_current_mss(struct sock *sk, int flags)
  1540. {
  1541. struct dst_entry *dst = __sk_dst_get(sk);
  1542. struct dn_scp *scp = DN_SK(sk);
  1543. int mss_now = min_t(int, scp->segsize_loc, scp->segsize_rem);
  1544. /* Other data messages are limited to 16 bytes per packet */
  1545. if (flags & MSG_OOB)
  1546. return 16;
  1547. /* This works out the maximum size of segment we can send out */
  1548. if (dst) {
  1549. u32 mtu = dst_mtu(dst);
  1550. mss_now = min_t(int, dn_mss_from_pmtu(dst->dev, mtu), mss_now);
  1551. }
  1552. return mss_now;
  1553. }
  1554. /*
  1555. * N.B. We get the timeout wrong here, but then we always did get it
  1556. * wrong before and this is another step along the road to correcting
  1557. * it. It ought to get updated each time we pass through the routine,
  1558. * but in practise it probably doesn't matter too much for now.
  1559. */
  1560. static inline struct sk_buff *dn_alloc_send_pskb(struct sock *sk,
  1561. unsigned long datalen, int noblock,
  1562. int *errcode)
  1563. {
  1564. struct sk_buff *skb = sock_alloc_send_skb(sk, datalen,
  1565. noblock, errcode);
  1566. if (skb) {
  1567. skb->protocol = htons(ETH_P_DNA_RT);
  1568. skb->pkt_type = PACKET_OUTGOING;
  1569. }
  1570. return skb;
  1571. }
  1572. static int dn_sendmsg(struct kiocb *iocb, struct socket *sock,
  1573. struct msghdr *msg, size_t size)
  1574. {
  1575. struct sock *sk = sock->sk;
  1576. struct dn_scp *scp = DN_SK(sk);
  1577. size_t mss;
  1578. struct sk_buff_head *queue = &scp->data_xmit_queue;
  1579. int flags = msg->msg_flags;
  1580. int err = 0;
  1581. size_t sent = 0;
  1582. int addr_len = msg->msg_namelen;
  1583. struct sockaddr_dn *addr = (struct sockaddr_dn *)msg->msg_name;
  1584. struct sk_buff *skb = NULL;
  1585. struct dn_skb_cb *cb;
  1586. size_t len;
  1587. unsigned char fctype;
  1588. long timeo;
  1589. if (flags & ~(MSG_TRYHARD|MSG_OOB|MSG_DONTWAIT|MSG_EOR|MSG_NOSIGNAL|MSG_MORE|MSG_CMSG_COMPAT))
  1590. return -EOPNOTSUPP;
  1591. if (addr_len && (addr_len != sizeof(struct sockaddr_dn)))
  1592. return -EINVAL;
  1593. lock_sock(sk);
  1594. timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
  1595. /*
  1596. * The only difference between stream sockets and sequenced packet
  1597. * sockets is that the stream sockets always behave as if MSG_EOR
  1598. * has been set.
  1599. */
  1600. if (sock->type == SOCK_STREAM) {
  1601. if (flags & MSG_EOR) {
  1602. err = -EINVAL;
  1603. goto out;
  1604. }
  1605. flags |= MSG_EOR;
  1606. }
  1607. err = dn_check_state(sk, addr, addr_len, &timeo, flags);
  1608. if (err)
  1609. goto out_err;
  1610. if (sk->sk_shutdown & SEND_SHUTDOWN) {
  1611. err = -EPIPE;
  1612. if (!(flags & MSG_NOSIGNAL))
  1613. send_sig(SIGPIPE, current, 0);
  1614. goto out_err;
  1615. }
  1616. if ((flags & MSG_TRYHARD) && sk->sk_dst_cache)
  1617. dst_negative_advice(sk);
  1618. mss = scp->segsize_rem;
  1619. fctype = scp->services_rem & NSP_FC_MASK;
  1620. mss = dn_current_mss(sk, flags);
  1621. if (flags & MSG_OOB) {
  1622. queue = &scp->other_xmit_queue;
  1623. if (size > mss) {
  1624. err = -EMSGSIZE;
  1625. goto out;
  1626. }
  1627. }
  1628. scp->persist_fxn = dn_nsp_xmit_timeout;
  1629. while(sent < size) {
  1630. err = sock_error(sk);
  1631. if (err)
  1632. goto out;
  1633. if (signal_pending(current)) {
  1634. err = sock_intr_errno(timeo);
  1635. goto out;
  1636. }
  1637. /*
  1638. * Calculate size that we wish to send.
  1639. */
  1640. len = size - sent;
  1641. if (len > mss)
  1642. len = mss;
  1643. /*
  1644. * Wait for queue size to go down below the window
  1645. * size.
  1646. */
  1647. if (dn_queue_too_long(scp, queue, flags)) {
  1648. DEFINE_WAIT(wait);
  1649. if (flags & MSG_DONTWAIT) {
  1650. err = -EWOULDBLOCK;
  1651. goto out;
  1652. }
  1653. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  1654. set_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  1655. sk_wait_event(sk, &timeo,
  1656. !dn_queue_too_long(scp, queue, flags));
  1657. clear_bit(SOCK_ASYNC_WAITDATA, &sk->sk_socket->flags);
  1658. finish_wait(sk_sleep(sk), &wait);
  1659. continue;
  1660. }
  1661. /*
  1662. * Get a suitably sized skb.
  1663. * 64 is a bit of a hack really, but its larger than any
  1664. * link-layer headers and has served us well as a good
  1665. * guess as to their real length.
  1666. */
  1667. skb = dn_alloc_send_pskb(sk, len + 64 + DN_MAX_NSP_DATA_HEADER,
  1668. flags & MSG_DONTWAIT, &err);
  1669. if (err)
  1670. break;
  1671. if (!skb)
  1672. continue;
  1673. cb = DN_SKB_CB(skb);
  1674. skb_reserve(skb, 64 + DN_MAX_NSP_DATA_HEADER);
  1675. if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
  1676. err = -EFAULT;
  1677. goto out;
  1678. }
  1679. if (flags & MSG_OOB) {
  1680. cb->nsp_flags = 0x30;
  1681. if (fctype != NSP_FC_NONE)
  1682. scp->flowrem_oth--;
  1683. } else {
  1684. cb->nsp_flags = 0x00;
  1685. if (scp->seg_total == 0)
  1686. cb->nsp_flags |= 0x20;
  1687. scp->seg_total += len;
  1688. if (((sent + len) == size) && (flags & MSG_EOR)) {
  1689. cb->nsp_flags |= 0x40;
  1690. scp->seg_total = 0;
  1691. if (fctype == NSP_FC_SCMC)
  1692. scp->flowrem_dat--;
  1693. }
  1694. if (fctype == NSP_FC_SRC)
  1695. scp->flowrem_dat--;
  1696. }
  1697. sent += len;
  1698. dn_nsp_queue_xmit(sk, skb, sk->sk_allocation, flags & MSG_OOB);
  1699. skb = NULL;
  1700. scp->persist = dn_nsp_persist(sk);
  1701. }
  1702. out:
  1703. kfree_skb(skb);
  1704. release_sock(sk);
  1705. return sent ? sent : err;
  1706. out_err:
  1707. err = sk_stream_error(sk, flags, err);
  1708. release_sock(sk);
  1709. return err;
  1710. }
  1711. static int dn_device_event(struct notifier_block *this, unsigned long event,
  1712. void *ptr)
  1713. {
  1714. struct net_device *dev = (struct net_device *)ptr;
  1715. if (!net_eq(dev_net(dev), &init_net))
  1716. return NOTIFY_DONE;
  1717. switch(event) {
  1718. case NETDEV_UP:
  1719. dn_dev_up(dev);
  1720. break;
  1721. case NETDEV_DOWN:
  1722. dn_dev_down(dev);
  1723. break;
  1724. default:
  1725. break;
  1726. }
  1727. return NOTIFY_DONE;
  1728. }
  1729. static struct notifier_block dn_dev_notifier = {
  1730. .notifier_call = dn_device_event,
  1731. };
  1732. extern int dn_route_rcv(struct sk_buff *, struct net_device *, struct packet_type *, struct net_device *);
  1733. static struct packet_type dn_dix_packet_type __read_mostly = {
  1734. .type = cpu_to_be16(ETH_P_DNA_RT),
  1735. .func = dn_route_rcv,
  1736. };
  1737. #ifdef CONFIG_PROC_FS
  1738. struct dn_iter_state {
  1739. int bucket;
  1740. };
  1741. static struct sock *dn_socket_get_first(struct seq_file *seq)
  1742. {
  1743. struct dn_iter_state *state = seq->private;
  1744. struct sock *n = NULL;
  1745. for(state->bucket = 0;
  1746. state->bucket < DN_SK_HASH_SIZE;
  1747. ++state->bucket) {
  1748. n = sk_head(&dn_sk_hash[state->bucket]);
  1749. if (n)
  1750. break;
  1751. }
  1752. return n;
  1753. }
  1754. static struct sock *dn_socket_get_next(struct seq_file *seq,
  1755. struct sock *n)
  1756. {
  1757. struct dn_iter_state *state = seq->private;
  1758. n = sk_next(n);
  1759. try_again:
  1760. if (n)
  1761. goto out;
  1762. if (++state->bucket >= DN_SK_HASH_SIZE)
  1763. goto out;
  1764. n = sk_head(&dn_sk_hash[state->bucket]);
  1765. goto try_again;
  1766. out:
  1767. return n;
  1768. }
  1769. static struct sock *socket_get_idx(struct seq_file *seq, loff_t *pos)
  1770. {
  1771. struct sock *sk = dn_socket_get_first(seq);
  1772. if (sk) {
  1773. while(*pos && (sk = dn_socket_get_next(seq, sk)))
  1774. --*pos;
  1775. }
  1776. return *pos ? NULL : sk;
  1777. }
  1778. static void *dn_socket_get_idx(struct seq_file *seq, loff_t pos)
  1779. {
  1780. void *rc;
  1781. read_lock_bh(&dn_hash_lock);
  1782. rc = socket_get_idx(seq, &pos);
  1783. if (!rc) {
  1784. read_unlock_bh(&dn_hash_lock);
  1785. }
  1786. return rc;
  1787. }
  1788. static void *dn_socket_seq_start(struct seq_file *seq, loff_t *pos)
  1789. {
  1790. return *pos ? dn_socket_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1791. }
  1792. static void *dn_socket_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1793. {
  1794. void *rc;
  1795. if (v == SEQ_START_TOKEN) {
  1796. rc = dn_socket_get_idx(seq, 0);
  1797. goto out;
  1798. }
  1799. rc = dn_socket_get_next(seq, v);
  1800. if (rc)
  1801. goto out;
  1802. read_unlock_bh(&dn_hash_lock);
  1803. out:
  1804. ++*pos;
  1805. return rc;
  1806. }
  1807. static void dn_socket_seq_stop(struct seq_file *seq, void *v)
  1808. {
  1809. if (v && v != SEQ_START_TOKEN)
  1810. read_unlock_bh(&dn_hash_lock);
  1811. }
  1812. #define IS_NOT_PRINTABLE(x) ((x) < 32 || (x) > 126)
  1813. static void dn_printable_object(struct sockaddr_dn *dn, unsigned char *buf)
  1814. {
  1815. int i;
  1816. switch (le16_to_cpu(dn->sdn_objnamel)) {
  1817. case 0:
  1818. sprintf(buf, "%d", dn->sdn_objnum);
  1819. break;
  1820. default:
  1821. for (i = 0; i < le16_to_cpu(dn->sdn_objnamel); i++) {
  1822. buf[i] = dn->sdn_objname[i];
  1823. if (IS_NOT_PRINTABLE(buf[i]))
  1824. buf[i] = '.';
  1825. }
  1826. buf[i] = 0;
  1827. }
  1828. }
  1829. static char *dn_state2asc(unsigned char state)
  1830. {
  1831. switch(state) {
  1832. case DN_O:
  1833. return "OPEN";
  1834. case DN_CR:
  1835. return " CR";
  1836. case DN_DR:
  1837. return " DR";
  1838. case DN_DRC:
  1839. return " DRC";
  1840. case DN_CC:
  1841. return " CC";
  1842. case DN_CI:
  1843. return " CI";
  1844. case DN_NR:
  1845. return " NR";
  1846. case DN_NC:
  1847. return " NC";
  1848. case DN_CD:
  1849. return " CD";
  1850. case DN_RJ:
  1851. return " RJ";
  1852. case DN_RUN:
  1853. return " RUN";
  1854. case DN_DI:
  1855. return " DI";
  1856. case DN_DIC:
  1857. return " DIC";
  1858. case DN_DN:
  1859. return " DN";
  1860. case DN_CL:
  1861. return " CL";
  1862. case DN_CN:
  1863. return " CN";
  1864. }
  1865. return "????";
  1866. }
  1867. static inline void dn_socket_format_entry(struct seq_file *seq, struct sock *sk)
  1868. {
  1869. struct dn_scp *scp = DN_SK(sk);
  1870. char buf1[DN_ASCBUF_LEN];
  1871. char buf2[DN_ASCBUF_LEN];
  1872. char local_object[DN_MAXOBJL+3];
  1873. char remote_object[DN_MAXOBJL+3];
  1874. dn_printable_object(&scp->addr, local_object);
  1875. dn_printable_object(&scp->peer, remote_object);
  1876. seq_printf(seq,
  1877. "%6s/%04X %04d:%04d %04d:%04d %01d %-16s "
  1878. "%6s/%04X %04d:%04d %04d:%04d %01d %-16s %4s %s\n",
  1879. dn_addr2asc(le16_to_cpu(dn_saddr2dn(&scp->addr)), buf1),
  1880. scp->addrloc,
  1881. scp->numdat,
  1882. scp->numoth,
  1883. scp->ackxmt_dat,
  1884. scp->ackxmt_oth,
  1885. scp->flowloc_sw,
  1886. local_object,
  1887. dn_addr2asc(le16_to_cpu(dn_saddr2dn(&scp->peer)), buf2),
  1888. scp->addrrem,
  1889. scp->numdat_rcv,
  1890. scp->numoth_rcv,
  1891. scp->ackrcv_dat,
  1892. scp->ackrcv_oth,
  1893. scp->flowrem_sw,
  1894. remote_object,
  1895. dn_state2asc(scp->state),
  1896. ((scp->accept_mode == ACC_IMMED) ? "IMMED" : "DEFER"));
  1897. }
  1898. static int dn_socket_seq_show(struct seq_file *seq, void *v)
  1899. {
  1900. if (v == SEQ_START_TOKEN) {
  1901. seq_puts(seq, "Local Remote\n");
  1902. } else {
  1903. dn_socket_format_entry(seq, v);
  1904. }
  1905. return 0;
  1906. }
  1907. static const struct seq_operations dn_socket_seq_ops = {
  1908. .start = dn_socket_seq_start,
  1909. .next = dn_socket_seq_next,
  1910. .stop = dn_socket_seq_stop,
  1911. .show = dn_socket_seq_show,
  1912. };
  1913. static int dn_socket_seq_open(struct inode *inode, struct file *file)
  1914. {
  1915. return seq_open_private(file, &dn_socket_seq_ops,
  1916. sizeof(struct dn_iter_state));
  1917. }
  1918. static const struct file_operations dn_socket_seq_fops = {
  1919. .owner = THIS_MODULE,
  1920. .open = dn_socket_seq_open,
  1921. .read = seq_read,
  1922. .llseek = seq_lseek,
  1923. .release = seq_release_private,
  1924. };
  1925. #endif
  1926. static const struct net_proto_family dn_family_ops = {
  1927. .family = AF_DECnet,
  1928. .create = dn_create,
  1929. .owner = THIS_MODULE,
  1930. };
  1931. static const struct proto_ops dn_proto_ops = {
  1932. .family = AF_DECnet,
  1933. .owner = THIS_MODULE,
  1934. .release = dn_release,
  1935. .bind = dn_bind,
  1936. .connect = dn_connect,
  1937. .socketpair = sock_no_socketpair,
  1938. .accept = dn_accept,
  1939. .getname = dn_getname,
  1940. .poll = dn_poll,
  1941. .ioctl = dn_ioctl,
  1942. .listen = dn_listen,
  1943. .shutdown = dn_shutdown,
  1944. .setsockopt = dn_setsockopt,
  1945. .getsockopt = dn_getsockopt,
  1946. .sendmsg = dn_sendmsg,
  1947. .recvmsg = dn_recvmsg,
  1948. .mmap = sock_no_mmap,
  1949. .sendpage = sock_no_sendpage,
  1950. };
  1951. void dn_register_sysctl(void);
  1952. void dn_unregister_sysctl(void);
  1953. MODULE_DESCRIPTION("The Linux DECnet Network Protocol");
  1954. MODULE_AUTHOR("Linux DECnet Project Team");
  1955. MODULE_LICENSE("GPL");
  1956. MODULE_ALIAS_NETPROTO(PF_DECnet);
  1957. static char banner[] __initdata = KERN_INFO "NET4: DECnet for Linux: V.2.5.68s (C) 1995-2003 Linux DECnet Project Team\n";
  1958. static int __init decnet_init(void)
  1959. {
  1960. int rc;
  1961. printk(banner);
  1962. rc = proto_register(&dn_proto, 1);
  1963. if (rc != 0)
  1964. goto out;
  1965. dn_neigh_init();
  1966. dn_dev_init();
  1967. dn_route_init();
  1968. dn_fib_init();
  1969. sock_register(&dn_family_ops);
  1970. dev_add_pack(&dn_dix_packet_type);
  1971. register_netdevice_notifier(&dn_dev_notifier);
  1972. proc_net_fops_create(&init_net, "decnet", S_IRUGO, &dn_socket_seq_fops);
  1973. dn_register_sysctl();
  1974. out:
  1975. return rc;
  1976. }
  1977. module_init(decnet_init);
  1978. /*
  1979. * Prevent DECnet module unloading until its fixed properly.
  1980. * Requires an audit of the code to check for memory leaks and
  1981. * initialisation problems etc.
  1982. */
  1983. #if 0
  1984. static void __exit decnet_exit(void)
  1985. {
  1986. sock_unregister(AF_DECnet);
  1987. rtnl_unregister_all(PF_DECnet);
  1988. dev_remove_pack(&dn_dix_packet_type);
  1989. dn_unregister_sysctl();
  1990. unregister_netdevice_notifier(&dn_dev_notifier);
  1991. dn_route_cleanup();
  1992. dn_dev_cleanup();
  1993. dn_neigh_cleanup();
  1994. dn_fib_cleanup();
  1995. proc_net_remove(&init_net, "decnet");
  1996. proto_unregister(&dn_proto);
  1997. rcu_barrier_bh(); /* Wait for completion of call_rcu_bh()'s */
  1998. }
  1999. module_exit(decnet_exit);
  2000. #endif