af_decnet.c 53 KB

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