af_wanpipe.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600
  1. /*****************************************************************************
  2. * af_wanpipe.c WANPIPE(tm) Secure Socket Layer.
  3. *
  4. * Author: Nenad Corbic <ncorbic@sangoma.com>
  5. *
  6. * Copyright: (c) 2000 Sangoma Technologies Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. * ============================================================================
  13. * Due Credit:
  14. * Wanpipe socket layer is based on Packet and
  15. * the X25 socket layers. The above sockets were
  16. * used for the specific use of Sangoma Technoloiges
  17. * API programs.
  18. * Packet socket Authors: Ross Biro, Fred N. van Kempen and
  19. * Alan Cox.
  20. * X25 socket Author: Jonathan Naylor.
  21. * ============================================================================
  22. * Mar 15, 2002 Arnaldo C. Melo o Use wp_sk()->num, as it isnt anymore in sock
  23. * Apr 25, 2000 Nenad Corbic o Added the ability to send zero length packets.
  24. * Mar 13, 2000 Nenad Corbic o Added a tx buffer check via ioctl call.
  25. * Mar 06, 2000 Nenad Corbic o Fixed the corrupt sock lcn problem.
  26. * Server and client applicaton can run
  27. * simultaneously without conflicts.
  28. * Feb 29, 2000 Nenad Corbic o Added support for PVC protocols, such as
  29. * CHDLC, Frame Relay and HDLC API.
  30. * Jan 17, 2000 Nenad Corbic o Initial version, based on AF_PACKET socket.
  31. * X25API support only.
  32. *
  33. ******************************************************************************/
  34. #include <linux/types.h>
  35. #include <linux/sched.h>
  36. #include <linux/mm.h>
  37. #include <linux/capability.h>
  38. #include <linux/fcntl.h>
  39. #include <linux/socket.h>
  40. #include <linux/in.h>
  41. #include <linux/inet.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/poll.h>
  44. #include <linux/wireless.h>
  45. #include <linux/kmod.h>
  46. #include <net/ip.h>
  47. #include <net/protocol.h>
  48. #include <linux/skbuff.h>
  49. #include <net/sock.h>
  50. #include <linux/errno.h>
  51. #include <linux/timer.h>
  52. #include <asm/system.h>
  53. #include <asm/uaccess.h>
  54. #include <linux/module.h>
  55. #include <linux/init.h>
  56. #include <linux/if_wanpipe.h>
  57. #include <linux/pkt_sched.h>
  58. #include <linux/tcp_states.h>
  59. #include <linux/if_wanpipe_common.h>
  60. #ifdef CONFIG_INET
  61. #include <net/inet_common.h>
  62. #endif
  63. #define SLOW_BACKOFF 0.1*HZ
  64. #define FAST_BACKOFF 0.01*HZ
  65. //#define PRINT_DEBUG
  66. #ifdef PRINT_DEBUG
  67. #define DBG_PRINTK(format, a...) printk(format, ## a)
  68. #else
  69. #define DBG_PRINTK(format, a...)
  70. #endif
  71. /* SECURE SOCKET IMPLEMENTATION
  72. *
  73. * TRANSMIT:
  74. *
  75. * When the user sends a packet via send() system call
  76. * the wanpipe_sendmsg() function is executed.
  77. *
  78. * Each packet is enqueud into sk->sk_write_queue transmit
  79. * queue. When the packet is enqueued, a delayed transmit
  80. * timer is triggerd which acts as a Bottom Half hander.
  81. *
  82. * wanpipe_delay_transmit() function (BH), dequeues packets
  83. * from the sk->sk_write_queue transmit queue and sends it
  84. * to the deriver via dev->hard_start_xmit(skb, dev) function.
  85. * Note, this function is actual a function pointer of if_send()
  86. * routine in the wanpipe driver.
  87. *
  88. * X25API GUARANTEED DELIVERY:
  89. *
  90. * In order to provide 100% guaranteed packet delivery,
  91. * an atomic 'packet_sent' counter is implemented. Counter
  92. * is incremented for each packet enqueued
  93. * into sk->sk_write_queue. Counter is decremented each
  94. * time wanpipe_delayed_transmit() function successfuly
  95. * passes the packet to the driver. Before each send(), a poll
  96. * routine checks the sock resources The maximum value of
  97. * packet sent counter is 1, thus if one packet is queued, the
  98. * application will block until that packet is passed to the
  99. * driver.
  100. *
  101. * RECEIVE:
  102. *
  103. * Wanpipe device drivers call the socket bottom half
  104. * function, wanpipe_rcv() to queue the incoming packets
  105. * into an AF_WANPIPE socket queue. Based on wanpipe_rcv()
  106. * return code, the driver knows whether the packet was
  107. * successfully queued. If the socket queue is full,
  108. * protocol flow control is used by the driver, if any,
  109. * to slow down the traffic until the sock queue is free.
  110. *
  111. * Every time a packet arrives into a socket queue the
  112. * socket wakes up processes which are waiting to receive
  113. * data.
  114. *
  115. * If the socket queue is full, the driver sets a block
  116. * bit which signals the socket to kick the wanpipe driver
  117. * bottom half hander when the socket queue is partialy
  118. * empty. wanpipe_recvmsg() function performs this action.
  119. *
  120. * In case of x25api, packets will never be dropped, since
  121. * flow control is available.
  122. *
  123. * In case of streaming protocols like CHDLC, packets will
  124. * be dropped but the statistics will be generated.
  125. */
  126. /* The code below is used to test memory leaks. It prints out
  127. * a message every time kmalloc and kfree system calls get executed.
  128. * If the calls match there is no leak :)
  129. */
  130. /***********FOR DEBUGGING PURPOSES*********************************************
  131. #define KMEM_SAFETYZONE 8
  132. static void * dbg_kmalloc(unsigned int size, int prio, int line) {
  133. void * v = kmalloc(size,prio);
  134. printk(KERN_INFO "line %d kmalloc(%d,%d) = %p\n",line,size,prio,v);
  135. return v;
  136. }
  137. static void dbg_kfree(void * v, int line) {
  138. printk(KERN_INFO "line %d kfree(%p)\n",line,v);
  139. kfree(v);
  140. }
  141. #define kmalloc(x,y) dbg_kmalloc(x,y,__LINE__)
  142. #define kfree(x) dbg_kfree(x,__LINE__)
  143. ******************************************************************************/
  144. /* List of all wanpipe sockets. */
  145. HLIST_HEAD(wanpipe_sklist);
  146. static DEFINE_RWLOCK(wanpipe_sklist_lock);
  147. atomic_t wanpipe_socks_nr;
  148. static unsigned long wanpipe_tx_critical;
  149. #if 0
  150. /* Private wanpipe socket structures. */
  151. struct wanpipe_opt
  152. {
  153. void *mbox; /* Mail box */
  154. void *card; /* Card bouded to */
  155. struct net_device *dev; /* Bounded device */
  156. unsigned short lcn; /* Binded LCN */
  157. unsigned char svc; /* 0=pvc, 1=svc */
  158. unsigned char timer; /* flag for delayed transmit*/
  159. struct timer_list tx_timer;
  160. unsigned poll_cnt;
  161. unsigned char force; /* Used to force sock release */
  162. atomic_t packet_sent;
  163. };
  164. #endif
  165. static int sk_count;
  166. extern const struct proto_ops wanpipe_ops;
  167. static unsigned long find_free_critical;
  168. static void wanpipe_unlink_driver(struct sock *sk);
  169. static void wanpipe_link_driver(struct net_device *dev, struct sock *sk);
  170. static void wanpipe_wakeup_driver(struct sock *sk);
  171. static int execute_command(struct sock *, unsigned char, unsigned int);
  172. static int check_dev(struct net_device *dev, sdla_t *card);
  173. struct net_device *wanpipe_find_free_dev(sdla_t *card);
  174. static void wanpipe_unlink_card (struct sock *);
  175. static int wanpipe_link_card (struct sock *);
  176. static struct sock *wanpipe_make_new(struct sock *);
  177. static struct sock *wanpipe_alloc_socket(void);
  178. static inline int get_atomic_device(struct net_device *dev);
  179. static int wanpipe_exec_cmd(struct sock *, int, unsigned int);
  180. static int get_ioctl_cmd (struct sock *, void *);
  181. static int set_ioctl_cmd (struct sock *, void *);
  182. static void release_device(struct net_device *dev);
  183. static void wanpipe_kill_sock_timer (unsigned long data);
  184. static void wanpipe_kill_sock_irq (struct sock *);
  185. static void wanpipe_kill_sock_accept (struct sock *);
  186. static int wanpipe_do_bind(struct sock *sk, struct net_device *dev,
  187. int protocol);
  188. struct sock * get_newsk_from_skb (struct sk_buff *);
  189. static int wanpipe_debug (struct sock *, void *);
  190. static void wanpipe_delayed_transmit (unsigned long data);
  191. static void release_driver(struct sock *);
  192. static void start_cleanup_timer (struct sock *);
  193. static void check_write_queue(struct sock *);
  194. static int check_driver_busy (struct sock *);
  195. /*============================================================
  196. * wanpipe_rcv
  197. *
  198. * Wanpipe socket bottom half handler. This function
  199. * is called by the WANPIPE device drivers to queue a
  200. * incoming packet into the socket receive queue.
  201. * Once the packet is queued, all processes waiting to
  202. * read are woken up.
  203. *
  204. * During socket bind, this function is bounded into
  205. * WANPIPE driver private.
  206. *===========================================================*/
  207. static int wanpipe_rcv(struct sk_buff *skb, struct net_device *dev,
  208. struct sock *sk)
  209. {
  210. struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)skb->cb;
  211. wanpipe_common_t *chan = dev->priv;
  212. /*
  213. * When we registered the protocol we saved the socket in the data
  214. * field for just this event.
  215. */
  216. skb->dev = dev;
  217. sll->sll_family = AF_WANPIPE;
  218. sll->sll_hatype = dev->type;
  219. sll->sll_protocol = skb->protocol;
  220. sll->sll_pkttype = skb->pkt_type;
  221. sll->sll_ifindex = dev->ifindex;
  222. sll->sll_halen = 0;
  223. if (dev->hard_header_parse)
  224. sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
  225. /*
  226. * WAN_PACKET_DATA : Data which should be passed up the receive queue.
  227. * WAN_PACKET_ASYC : Asynchronous data like place call, which should
  228. * be passed up the listening sock.
  229. * WAN_PACKET_ERR : Asynchronous data like clear call or restart
  230. * which should go into an error queue.
  231. */
  232. switch (skb->pkt_type){
  233. case WAN_PACKET_DATA:
  234. if (sock_queue_rcv_skb(sk,skb)<0){
  235. return -ENOMEM;
  236. }
  237. break;
  238. case WAN_PACKET_CMD:
  239. sk->sk_state = chan->state;
  240. /* Bug fix: update Mar6.
  241. * Do not set the sock lcn number here, since
  242. * cmd is not guaranteed to be executed on the
  243. * board, thus Lcn could be wrong */
  244. sk->sk_data_ready(sk, skb->len);
  245. kfree_skb(skb);
  246. break;
  247. case WAN_PACKET_ERR:
  248. sk->sk_state = chan->state;
  249. if (sock_queue_err_skb(sk,skb)<0){
  250. return -ENOMEM;
  251. }
  252. break;
  253. default:
  254. printk(KERN_INFO "wansock: BH Illegal Packet Type Dropping\n");
  255. kfree_skb(skb);
  256. break;
  257. }
  258. //??????????????????????
  259. // if (sk->sk_state == WANSOCK_DISCONNECTED){
  260. // if (sk->sk_zapped) {
  261. // //printk(KERN_INFO "wansock: Disconnected, killing early\n");
  262. // wanpipe_unlink_driver(sk);
  263. // sk->sk_bound_dev_if = 0;
  264. // }
  265. // }
  266. return 0;
  267. }
  268. /*============================================================
  269. * wanpipe_listen_rcv
  270. *
  271. * Wanpipe LISTEN socket bottom half handler. This function
  272. * is called by the WANPIPE device drivers to queue an
  273. * incoming call into the socket listening queue.
  274. * Once the packet is queued, the waiting accept() process
  275. * is woken up.
  276. *
  277. * During socket bind, this function is bounded into
  278. * WANPIPE driver private.
  279. *
  280. * IMPORTANT NOTE:
  281. * The accept call() is waiting for an skb packet
  282. * which contains a pointer to a device structure.
  283. *
  284. * When we do a bind to a device structre, we
  285. * bind a newly created socket into "chan->sk". Thus,
  286. * when accept receives the skb packet, it will know
  287. * from which dev it came form, and in turn it will know
  288. * the address of the new sock.
  289. *
  290. * NOTE: This function gets called from driver ISR.
  291. *===========================================================*/
  292. static int wanpipe_listen_rcv (struct sk_buff *skb, struct sock *sk)
  293. {
  294. wanpipe_opt *wp = wp_sk(sk), *newwp;
  295. struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)skb->cb;
  296. struct sock *newsk;
  297. struct net_device *dev;
  298. sdla_t *card;
  299. mbox_cmd_t *mbox_ptr;
  300. wanpipe_common_t *chan;
  301. /* Find a free device, if none found, all svc's are busy
  302. */
  303. card = (sdla_t*)wp->card;
  304. if (!card){
  305. printk(KERN_INFO "wansock: LISTEN ERROR, No Card\n");
  306. return -ENODEV;
  307. }
  308. dev = wanpipe_find_free_dev(card);
  309. if (!dev){
  310. printk(KERN_INFO "wansock: LISTEN ERROR, No Free Device\n");
  311. return -ENODEV;
  312. }
  313. chan=dev->priv;
  314. chan->state = WANSOCK_CONNECTING;
  315. /* Allocate a new sock, which accept will bind
  316. * and pass up to the user
  317. */
  318. if ((newsk = wanpipe_make_new(sk)) == NULL){
  319. release_device(dev);
  320. return -ENOMEM;
  321. }
  322. /* Initialize the new sock structure
  323. */
  324. newsk->sk_bound_dev_if = dev->ifindex;
  325. newwp = wp_sk(newsk);
  326. newwp->card = wp->card;
  327. /* Insert the sock into the main wanpipe
  328. * sock list.
  329. */
  330. atomic_inc(&wanpipe_socks_nr);
  331. /* Allocate and fill in the new Mail Box. Then
  332. * bind the mail box to the sock. It will be
  333. * used by the ioctl call to read call information
  334. * and to execute commands.
  335. */
  336. if ((mbox_ptr = kzalloc(sizeof(mbox_cmd_t), GFP_ATOMIC)) == NULL) {
  337. wanpipe_kill_sock_irq (newsk);
  338. release_device(dev);
  339. return -ENOMEM;
  340. }
  341. memcpy(mbox_ptr,skb->data,skb->len);
  342. /* Register the lcn on which incoming call came
  343. * from. Thus, if we have to clear it, we know
  344. * which lcn to clear
  345. */
  346. newwp->lcn = mbox_ptr->cmd.lcn;
  347. newwp->mbox = (void *)mbox_ptr;
  348. DBG_PRINTK(KERN_INFO "NEWSOCK : Device %s, bind to lcn %i\n",
  349. dev->name,mbox_ptr->cmd.lcn);
  350. chan->lcn = mbox_ptr->cmd.lcn;
  351. card->u.x.svc_to_dev_map[(chan->lcn%MAX_X25_LCN)] = dev;
  352. sock_reset_flag(newsk, SOCK_ZAPPED);
  353. newwp->num = htons(X25_PROT);
  354. if (wanpipe_do_bind(newsk, dev, newwp->num)) {
  355. wanpipe_kill_sock_irq (newsk);
  356. release_device(dev);
  357. return -EINVAL;
  358. }
  359. newsk->sk_state = WANSOCK_CONNECTING;
  360. /* Fill in the standard sock address info */
  361. sll->sll_family = AF_WANPIPE;
  362. sll->sll_hatype = dev->type;
  363. sll->sll_protocol = skb->protocol;
  364. sll->sll_pkttype = skb->pkt_type;
  365. sll->sll_ifindex = dev->ifindex;
  366. sll->sll_halen = 0;
  367. skb->dev = dev;
  368. sk->sk_ack_backlog++;
  369. /* We must do this manually, since the sock_queue_rcv_skb()
  370. * function sets the skb->dev to NULL. However, we use
  371. * the dev field in the accept function.*/
  372. if (atomic_read(&sk->sk_rmem_alloc) + skb->truesize >=
  373. (unsigned)sk->sk_rcvbuf) {
  374. wanpipe_unlink_driver(newsk);
  375. wanpipe_kill_sock_irq (newsk);
  376. --sk->sk_ack_backlog;
  377. return -ENOMEM;
  378. }
  379. skb_set_owner_r(skb, sk);
  380. skb_queue_tail(&sk->sk_receive_queue, skb);
  381. sk->sk_data_ready(sk, skb->len);
  382. return 0;
  383. }
  384. /*============================================================
  385. * wanpipe_make_new
  386. *
  387. * Create a new sock, and allocate a wanpipe private
  388. * structure to it. Also, copy the important data
  389. * from the original sock to the new sock.
  390. *
  391. * This function is used by wanpipe_listen_rcv() listen
  392. * bottom half handler. A copy of the listening sock
  393. * is created using this function.
  394. *
  395. *===========================================================*/
  396. static struct sock *wanpipe_make_new(struct sock *osk)
  397. {
  398. struct sock *sk;
  399. if (osk->sk_type != SOCK_RAW)
  400. return NULL;
  401. if ((sk = wanpipe_alloc_socket()) == NULL)
  402. return NULL;
  403. sk->sk_type = osk->sk_type;
  404. sk->sk_socket = osk->sk_socket;
  405. sk->sk_priority = osk->sk_priority;
  406. sk->sk_protocol = osk->sk_protocol;
  407. wp_sk(sk)->num = wp_sk(osk)->num;
  408. sk->sk_rcvbuf = osk->sk_rcvbuf;
  409. sk->sk_sndbuf = osk->sk_sndbuf;
  410. sk->sk_state = WANSOCK_CONNECTING;
  411. sk->sk_sleep = osk->sk_sleep;
  412. if (sock_flag(osk, SOCK_DBG))
  413. sock_set_flag(sk, SOCK_DBG);
  414. return sk;
  415. }
  416. /*
  417. * FIXME: wanpipe_opt has to include a sock in its definition and stop using
  418. * sk_protinfo, but this code is not even compilable now, so lets leave it for
  419. * later.
  420. */
  421. static struct proto wanpipe_proto = {
  422. .name = "WANPIPE",
  423. .owner = THIS_MODULE,
  424. .obj_size = sizeof(struct sock),
  425. };
  426. /*============================================================
  427. * wanpipe_make_new
  428. *
  429. * Allocate memory for the a new sock, and sock
  430. * private data.
  431. *
  432. * Increment the module use count.
  433. *
  434. * This function is used by wanpipe_create() and
  435. * wanpipe_make_new() functions.
  436. *
  437. *===========================================================*/
  438. static struct sock *wanpipe_alloc_socket(void)
  439. {
  440. struct sock *sk;
  441. struct wanpipe_opt *wan_opt;
  442. if ((sk = sk_alloc(PF_WANPIPE, GFP_ATOMIC, &wanpipe_proto, 1)) == NULL)
  443. return NULL;
  444. if ((wan_opt = kzalloc(sizeof(struct wanpipe_opt), GFP_ATOMIC)) == NULL) {
  445. sk_free(sk);
  446. return NULL;
  447. }
  448. wp_sk(sk) = wan_opt;
  449. /* Use timer to send data to the driver. This will act
  450. * as a BH handler for sendmsg functions */
  451. init_timer(&wan_opt->tx_timer);
  452. wan_opt->tx_timer.data = (unsigned long)sk;
  453. wan_opt->tx_timer.function = wanpipe_delayed_transmit;
  454. sock_init_data(NULL, sk);
  455. return sk;
  456. }
  457. /*============================================================
  458. * wanpipe_sendmsg
  459. *
  460. * This function implements a sendto() system call,
  461. * for AF_WANPIPE socket family.
  462. * During socket bind() sk->sk_bound_dev_if is initialized
  463. * to a correct network device. This number is used
  464. * to find a network device to which the packet should
  465. * be passed to.
  466. *
  467. * Each packet is queued into sk->sk_write_queue and
  468. * delayed transmit bottom half handler is marked for
  469. * execution.
  470. *
  471. * A socket must be in WANSOCK_CONNECTED state before
  472. * a packet is queued into sk->sk_write_queue.
  473. *===========================================================*/
  474. static int wanpipe_sendmsg(struct kiocb *iocb, struct socket *sock,
  475. struct msghdr *msg, int len)
  476. {
  477. wanpipe_opt *wp;
  478. struct sock *sk = sock->sk;
  479. struct wan_sockaddr_ll *saddr=(struct wan_sockaddr_ll *)msg->msg_name;
  480. struct sk_buff *skb;
  481. struct net_device *dev;
  482. unsigned short proto;
  483. unsigned char *addr;
  484. int ifindex, err, reserve = 0;
  485. if (!sock_flag(sk, SOCK_ZAPPED))
  486. return -ENETDOWN;
  487. if (sk->sk_state != WANSOCK_CONNECTED)
  488. return -ENOTCONN;
  489. if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
  490. return(-EINVAL);
  491. /* it was <=, now one can send
  492. * zero length packets */
  493. if (len < sizeof(x25api_hdr_t))
  494. return -EINVAL;
  495. wp = wp_sk(sk);
  496. if (saddr == NULL) {
  497. ifindex = sk->sk_bound_dev_if;
  498. proto = wp->num;
  499. addr = NULL;
  500. }else{
  501. if (msg->msg_namelen < sizeof(struct wan_sockaddr_ll)){
  502. return -EINVAL;
  503. }
  504. ifindex = sk->sk_bound_dev_if;
  505. proto = saddr->sll_protocol;
  506. addr = saddr->sll_addr;
  507. }
  508. dev = dev_get_by_index(ifindex);
  509. if (dev == NULL){
  510. printk(KERN_INFO "wansock: Send failed, dev index: %i\n",ifindex);
  511. return -ENXIO;
  512. }
  513. dev_put(dev);
  514. if (sock->type == SOCK_RAW)
  515. reserve = dev->hard_header_len;
  516. if (len > dev->mtu+reserve){
  517. return -EMSGSIZE;
  518. }
  519. skb = sock_alloc_send_skb(sk, len + LL_RESERVED_SPACE(dev),
  520. msg->msg_flags & MSG_DONTWAIT, &err);
  521. if (skb==NULL){
  522. goto out_unlock;
  523. }
  524. skb_reserve(skb, LL_RESERVED_SPACE(dev));
  525. skb->nh.raw = skb->data;
  526. /* Returns -EFAULT on error */
  527. err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
  528. if (err){
  529. goto out_free;
  530. }
  531. if (dev->hard_header) {
  532. int res;
  533. err = -EINVAL;
  534. res = dev->hard_header(skb, dev, ntohs(proto), addr, NULL, len);
  535. if (res<0){
  536. goto out_free;
  537. }
  538. }
  539. skb->protocol = proto;
  540. skb->dev = dev;
  541. skb->priority = sk->sk_priority;
  542. skb->pkt_type = WAN_PACKET_DATA;
  543. err = -ENETDOWN;
  544. if (!(dev->flags & IFF_UP))
  545. goto out_free;
  546. if (atomic_read(&sk->sk_wmem_alloc) + skb->truesize >
  547. (unsigned int)sk->sk_sndbuf){
  548. kfree_skb(skb);
  549. return -ENOBUFS;
  550. }
  551. skb_queue_tail(&sk->sk_write_queue,skb);
  552. atomic_inc(&wp->packet_sent);
  553. if (!(test_and_set_bit(0, &wp->timer)))
  554. mod_timer(&wp->tx_timer, jiffies + 1);
  555. return(len);
  556. out_free:
  557. kfree_skb(skb);
  558. out_unlock:
  559. return err;
  560. }
  561. /*============================================================
  562. * wanpipe_delayed_tarnsmit
  563. *
  564. * Transmit bottom half handler. It dequeues packets
  565. * from sk->sk_write_queue and passes them to the
  566. * driver. If the driver is busy, the packet is
  567. * re-enqueued.
  568. *
  569. * Packet Sent counter is decremented on successful
  570. * transmission.
  571. *===========================================================*/
  572. static void wanpipe_delayed_transmit (unsigned long data)
  573. {
  574. struct sock *sk=(struct sock *)data;
  575. struct sk_buff *skb;
  576. wanpipe_opt *wp = wp_sk(sk);
  577. struct net_device *dev = wp->dev;
  578. sdla_t *card = (sdla_t*)wp->card;
  579. if (!card || !dev){
  580. clear_bit(0, &wp->timer);
  581. DBG_PRINTK(KERN_INFO "wansock: Transmit delay, no dev or card\n");
  582. return;
  583. }
  584. if (sk->sk_state != WANSOCK_CONNECTED || !sock_flag(sk, SOCK_ZAPPED)) {
  585. clear_bit(0, &wp->timer);
  586. DBG_PRINTK(KERN_INFO "wansock: Tx Timer, State not CONNECTED\n");
  587. return;
  588. }
  589. /* If driver is executing command, we must offload
  590. * the board by not sending data. Otherwise a
  591. * pending command will never get a free buffer
  592. * to execute */
  593. if (atomic_read(&card->u.x.command_busy)){
  594. wp->tx_timer.expires = jiffies + SLOW_BACKOFF;
  595. add_timer(&wp->tx_timer);
  596. DBG_PRINTK(KERN_INFO "wansock: Tx Timer, command bys BACKOFF\n");
  597. return;
  598. }
  599. if (test_and_set_bit(0,&wanpipe_tx_critical)){
  600. printk(KERN_INFO "WanSock: Tx timer critical %s\n",dev->name);
  601. wp->tx_timer.expires = jiffies + SLOW_BACKOFF;
  602. add_timer(&wp->tx_timer);
  603. return;
  604. }
  605. /* Check for a packet in the fifo and send */
  606. if ((skb = skb_dequeue(&sk->sk_write_queue)) != NULL){
  607. if (dev->hard_start_xmit(skb, dev) != 0){
  608. /* Driver failed to transmit, re-enqueue
  609. * the packet and retry again later */
  610. skb_queue_head(&sk->sk_write_queue,skb);
  611. clear_bit(0,&wanpipe_tx_critical);
  612. return;
  613. }else{
  614. /* Packet Sent successful. Check for more packets
  615. * if more packets, re-trigger the transmit routine
  616. * other wise exit
  617. */
  618. atomic_dec(&wp->packet_sent);
  619. if (skb_peek(&sk->sk_write_queue) == NULL) {
  620. /* If there is nothing to send, kick
  621. * the poll routine, which will trigger
  622. * the application to send more data */
  623. sk->sk_data_ready(sk, 0);
  624. clear_bit(0, &wp->timer);
  625. }else{
  626. /* Reschedule as fast as possible */
  627. wp->tx_timer.expires = jiffies + 1;
  628. add_timer(&wp->tx_timer);
  629. }
  630. }
  631. }
  632. clear_bit(0,&wanpipe_tx_critical);
  633. }
  634. /*============================================================
  635. * execute_command
  636. *
  637. * Execute x25api commands. The atomic variable
  638. * chan->command is used to indicate to the driver that
  639. * command is pending for execution. The acutal command
  640. * structure is placed into a sock mbox structure
  641. * (wp_sk(sk)->mbox).
  642. *
  643. * The sock private structure, mbox is
  644. * used as shared memory between sock and the driver.
  645. * Driver uses the sock mbox to execute the command
  646. * and return the result.
  647. *
  648. * For all command except PLACE CALL, the function
  649. * waits for the result. PLACE CALL can be ether
  650. * blocking or nonblocking. The user sets this option
  651. * via ioctl call.
  652. *===========================================================*/
  653. static int execute_command(struct sock *sk, unsigned char cmd, unsigned int flags)
  654. {
  655. wanpipe_opt *wp = wp_sk(sk);
  656. struct net_device *dev;
  657. wanpipe_common_t *chan=NULL;
  658. int err=0;
  659. DECLARE_WAITQUEUE(wait, current);
  660. dev = dev_get_by_index(sk->sk_bound_dev_if);
  661. if (dev == NULL){
  662. printk(KERN_INFO "wansock: Exec failed no dev %i\n",
  663. sk->sk_bound_dev_if);
  664. return -ENODEV;
  665. }
  666. dev_put(dev);
  667. if ((chan=dev->priv) == NULL){
  668. printk(KERN_INFO "wansock: Exec cmd failed no priv area\n");
  669. return -ENODEV;
  670. }
  671. if (atomic_read(&chan->command)){
  672. printk(KERN_INFO "wansock: ERROR: Command already running %x, %s\n",
  673. atomic_read(&chan->command),dev->name);
  674. return -EINVAL;
  675. }
  676. if (!wp->mbox) {
  677. printk(KERN_INFO "wansock: In execute without MBOX\n");
  678. return -EINVAL;
  679. }
  680. ((mbox_cmd_t*)wp->mbox)->cmd.command = cmd;
  681. ((mbox_cmd_t*)wp->mbox)->cmd.lcn = wp->lcn;
  682. ((mbox_cmd_t*)wp->mbox)->cmd.result = 0x7F;
  683. if (flags & O_NONBLOCK){
  684. cmd |= 0x80;
  685. atomic_set(&chan->command, cmd);
  686. }else{
  687. atomic_set(&chan->command, cmd);
  688. }
  689. add_wait_queue(sk->sk_sleep,&wait);
  690. current->state = TASK_INTERRUPTIBLE;
  691. for (;;){
  692. if (((mbox_cmd_t*)wp->mbox)->cmd.result != 0x7F) {
  693. err = 0;
  694. break;
  695. }
  696. if (signal_pending(current)) {
  697. err = -ERESTARTSYS;
  698. break;
  699. }
  700. schedule();
  701. }
  702. current->state = TASK_RUNNING;
  703. remove_wait_queue(sk->sk_sleep,&wait);
  704. return err;
  705. }
  706. /*============================================================
  707. * wanpipe_destroy_timer
  708. *
  709. * Used by wanpipe_release, to delay release of
  710. * the socket.
  711. *===========================================================*/
  712. static void wanpipe_destroy_timer(unsigned long data)
  713. {
  714. struct sock *sk=(struct sock *)data;
  715. wanpipe_opt *wp = wp_sk(sk);
  716. if ((!atomic_read(&sk->sk_wmem_alloc) &&
  717. !atomic_read(&sk->sk_rmem_alloc)) ||
  718. (++wp->force == 5)) {
  719. if (atomic_read(&sk->sk_wmem_alloc) ||
  720. atomic_read(&sk->sk_rmem_alloc))
  721. printk(KERN_INFO "wansock: Warning, Packet Discarded due to sock shutdown!\n");
  722. kfree(wp);
  723. wp_sk(sk) = NULL;
  724. if (atomic_read(&sk->sk_refcnt) != 1) {
  725. atomic_set(&sk->sk_refcnt, 1);
  726. DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i ! :delay.\n",
  727. atomic_read(&sk->sk_refcnt));
  728. }
  729. sock_put(sk);
  730. atomic_dec(&wanpipe_socks_nr);
  731. return;
  732. }
  733. sk->sk_timer.expires = jiffies + 5 * HZ;
  734. add_timer(&sk->sk_timer);
  735. printk(KERN_INFO "wansock: packet sk destroy delayed\n");
  736. }
  737. /*============================================================
  738. * wanpipe_unlink_driver
  739. *
  740. * When the socket is released, this function is
  741. * used to remove links that bind the sock and the
  742. * driver together.
  743. *===========================================================*/
  744. static void wanpipe_unlink_driver (struct sock *sk)
  745. {
  746. struct net_device *dev;
  747. wanpipe_common_t *chan=NULL;
  748. sock_reset_flag(sk, SOCK_ZAPPED);
  749. sk->sk_state = WANSOCK_DISCONNECTED;
  750. wp_sk(sk)->dev = NULL;
  751. dev = dev_get_by_index(sk->sk_bound_dev_if);
  752. if (!dev){
  753. printk(KERN_INFO "wansock: No dev on release\n");
  754. return;
  755. }
  756. dev_put(dev);
  757. if ((chan = dev->priv) == NULL){
  758. printk(KERN_INFO "wansock: No Priv Area on release\n");
  759. return;
  760. }
  761. set_bit(0,&chan->common_critical);
  762. chan->sk=NULL;
  763. chan->func=NULL;
  764. chan->mbox=NULL;
  765. chan->tx_timer=NULL;
  766. clear_bit(0,&chan->common_critical);
  767. release_device(dev);
  768. return;
  769. }
  770. /*============================================================
  771. * wanpipe_link_driver
  772. *
  773. * Upon successful bind(), sock is linked to a driver
  774. * by binding in the wanpipe_rcv() bottom half handler
  775. * to the driver function pointer, as well as sock and
  776. * sock mailbox addresses. This way driver can pass
  777. * data up the socket.
  778. *===========================================================*/
  779. static void wanpipe_link_driver(struct net_device *dev, struct sock *sk)
  780. {
  781. wanpipe_opt *wp = wp_sk(sk);
  782. wanpipe_common_t *chan = dev->priv;
  783. if (!chan)
  784. return;
  785. set_bit(0,&chan->common_critical);
  786. chan->sk=sk;
  787. chan->func=wanpipe_rcv;
  788. chan->mbox = wp->mbox;
  789. chan->tx_timer = &wp->tx_timer;
  790. wp->dev = dev;
  791. sock_set_flag(sk, SOCK_ZAPPED);
  792. clear_bit(0,&chan->common_critical);
  793. }
  794. /*============================================================
  795. * release_device
  796. *
  797. * During sock release, clear a critical bit, which
  798. * marks the device a being taken.
  799. *===========================================================*/
  800. static void release_device(struct net_device *dev)
  801. {
  802. wanpipe_common_t *chan=dev->priv;
  803. clear_bit(0,(void*)&chan->rw_bind);
  804. }
  805. /*============================================================
  806. * wanpipe_release
  807. *
  808. * Close a PACKET socket. This is fairly simple. We
  809. * immediately go to 'closed' state and remove our
  810. * protocol entry in the device list.
  811. *===========================================================*/
  812. static int wanpipe_release(struct socket *sock)
  813. {
  814. wanpipe_opt *wp;
  815. struct sock *sk = sock->sk;
  816. if (!sk)
  817. return 0;
  818. wp = wp_sk(sk);
  819. check_write_queue(sk);
  820. /* Kill the tx timer, if we don't kill it now, the timer
  821. * will run after we kill the sock. Timer code will
  822. * try to access the sock which has been killed and cause
  823. * kernel panic */
  824. del_timer(&wp->tx_timer);
  825. /*
  826. * Unhook packet receive handler.
  827. */
  828. if (wp->num == htons(X25_PROT) &&
  829. sk->sk_state != WANSOCK_DISCONNECTED && sock_flag(sk, SOCK_ZAPPED)) {
  830. struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
  831. wanpipe_common_t *chan;
  832. if (dev){
  833. chan=dev->priv;
  834. atomic_set(&chan->disconnect,1);
  835. DBG_PRINTK(KERN_INFO "wansock: Sending Clear Indication %i\n",
  836. sk->sk_state);
  837. dev_put(dev);
  838. }
  839. }
  840. set_bit(1,&wanpipe_tx_critical);
  841. write_lock(&wanpipe_sklist_lock);
  842. sk_del_node_init(sk);
  843. write_unlock(&wanpipe_sklist_lock);
  844. clear_bit(1,&wanpipe_tx_critical);
  845. release_driver(sk);
  846. /*
  847. * Now the socket is dead. No more input will appear.
  848. */
  849. sk->sk_state_change(sk); /* It is useless. Just for sanity. */
  850. sock->sk = NULL;
  851. sk->sk_socket = NULL;
  852. sock_set_flag(sk, SOCK_DEAD);
  853. /* Purge queues */
  854. skb_queue_purge(&sk->sk_receive_queue);
  855. skb_queue_purge(&sk->sk_write_queue);
  856. skb_queue_purge(&sk->sk_error_queue);
  857. if (atomic_read(&sk->sk_rmem_alloc) ||
  858. atomic_read(&sk->sk_wmem_alloc)) {
  859. del_timer(&sk->sk_timer);
  860. printk(KERN_INFO "wansock: Killing in Timer R %i , W %i\n",
  861. atomic_read(&sk->sk_rmem_alloc),
  862. atomic_read(&sk->sk_wmem_alloc));
  863. sk->sk_timer.data = (unsigned long)sk;
  864. sk->sk_timer.expires = jiffies + HZ;
  865. sk->sk_timer.function = wanpipe_destroy_timer;
  866. add_timer(&sk->sk_timer);
  867. return 0;
  868. }
  869. kfree(wp);
  870. wp_sk(sk) = NULL;
  871. if (atomic_read(&sk->sk_refcnt) != 1) {
  872. DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i !:release.\n",
  873. atomic_read(&sk->sk_refcnt));
  874. atomic_set(&sk->sk_refcnt, 1);
  875. }
  876. sock_put(sk);
  877. atomic_dec(&wanpipe_socks_nr);
  878. return 0;
  879. }
  880. /*============================================================
  881. * check_write_queue
  882. *
  883. * During sock shutdown, if the sock state is
  884. * WANSOCK_CONNECTED and there is transmit data
  885. * pending. Wait until data is released
  886. * before proceeding.
  887. *===========================================================*/
  888. static void check_write_queue(struct sock *sk)
  889. {
  890. if (sk->sk_state != WANSOCK_CONNECTED)
  891. return;
  892. if (!atomic_read(&sk->sk_wmem_alloc))
  893. return;
  894. printk(KERN_INFO "wansock: MAJOR ERROR, Data lost on sock release !!!\n");
  895. }
  896. /*============================================================
  897. * release_driver
  898. *
  899. * This function is called during sock shutdown, to
  900. * release any resources and links that bind the sock
  901. * to the driver. It also changes the state of the
  902. * sock to WANSOCK_DISCONNECTED
  903. *===========================================================*/
  904. static void release_driver(struct sock *sk)
  905. {
  906. wanpipe_opt *wp;
  907. struct sk_buff *skb=NULL;
  908. struct sock *deadsk=NULL;
  909. if (sk->sk_state == WANSOCK_LISTEN ||
  910. sk->sk_state == WANSOCK_BIND_LISTEN) {
  911. while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
  912. if ((deadsk = get_newsk_from_skb(skb))){
  913. DBG_PRINTK (KERN_INFO "wansock: RELEASE: FOUND DEAD SOCK\n");
  914. sock_set_flag(deadsk, SOCK_DEAD);
  915. start_cleanup_timer(deadsk);
  916. }
  917. kfree_skb(skb);
  918. }
  919. if (sock_flag(sk, SOCK_ZAPPED))
  920. wanpipe_unlink_card(sk);
  921. }else{
  922. if (sock_flag(sk, SOCK_ZAPPED))
  923. wanpipe_unlink_driver(sk);
  924. }
  925. sk->sk_state = WANSOCK_DISCONNECTED;
  926. sk->sk_bound_dev_if = 0;
  927. sock_reset_flag(sk, SOCK_ZAPPED);
  928. wp = wp_sk(sk);
  929. if (wp) {
  930. kfree(wp->mbox);
  931. wp->mbox = NULL;
  932. }
  933. }
  934. /*============================================================
  935. * start_cleanup_timer
  936. *
  937. * If new incoming call's are pending but the socket
  938. * is being released, start the timer which will
  939. * envoke the kill routines for pending socks.
  940. *===========================================================*/
  941. static void start_cleanup_timer (struct sock *sk)
  942. {
  943. del_timer(&sk->sk_timer);
  944. sk->sk_timer.data = (unsigned long)sk;
  945. sk->sk_timer.expires = jiffies + HZ;
  946. sk->sk_timer.function = wanpipe_kill_sock_timer;
  947. add_timer(&sk->sk_timer);
  948. }
  949. /*============================================================
  950. * wanpipe_kill_sock
  951. *
  952. * This is a function which performs actual killing
  953. * of the sock. It releases socket resources,
  954. * and unlinks the sock from the driver.
  955. *===========================================================*/
  956. static void wanpipe_kill_sock_timer (unsigned long data)
  957. {
  958. struct sock *sk = (struct sock *)data;
  959. struct sock **skp;
  960. if (!sk)
  961. return;
  962. /* This function can be called from interrupt. We must use
  963. * appropriate locks */
  964. if (test_bit(1,&wanpipe_tx_critical)){
  965. sk->sk_timer.expires = jiffies + 10;
  966. add_timer(&sk->sk_timer);
  967. return;
  968. }
  969. write_lock(&wanpipe_sklist_lock);
  970. sk_del_node_init(sk);
  971. write_unlock(&wanpipe_sklist_lock);
  972. if (wp_sk(sk)->num == htons(X25_PROT) &&
  973. sk->sk_state != WANSOCK_DISCONNECTED) {
  974. struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
  975. wanpipe_common_t *chan;
  976. if (dev){
  977. chan=dev->priv;
  978. atomic_set(&chan->disconnect,1);
  979. dev_put(dev);
  980. }
  981. }
  982. release_driver(sk);
  983. sk->sk_socket = NULL;
  984. /* Purge queues */
  985. skb_queue_purge(&sk->sk_receive_queue);
  986. skb_queue_purge(&sk->sk_write_queue);
  987. skb_queue_purge(&sk->sk_error_queue);
  988. if (atomic_read(&sk->sk_rmem_alloc) ||
  989. atomic_read(&sk->sk_wmem_alloc)) {
  990. del_timer(&sk->sk_timer);
  991. printk(KERN_INFO "wansock: Killing SOCK in Timer\n");
  992. sk->sk_timer.data = (unsigned long)sk;
  993. sk->sk_timer.expires = jiffies + HZ;
  994. sk->sk_timer.function = wanpipe_destroy_timer;
  995. add_timer(&sk->sk_timer);
  996. return;
  997. }
  998. kfree(wp_sk(sk));
  999. wp_sk(sk) = NULL;
  1000. if (atomic_read(&sk->sk_refcnt) != 1) {
  1001. atomic_set(&sk->sk_refcnt, 1);
  1002. DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i ! :timer.\n",
  1003. atomic_read(&sk->sk_refcnt));
  1004. }
  1005. sock_put(sk);
  1006. atomic_dec(&wanpipe_socks_nr);
  1007. return;
  1008. }
  1009. static void wanpipe_kill_sock_accept (struct sock *sk)
  1010. {
  1011. struct sock **skp;
  1012. if (!sk)
  1013. return;
  1014. /* This function can be called from interrupt. We must use
  1015. * appropriate locks */
  1016. write_lock(&wanpipe_sklist_lock);
  1017. sk_del_node_init(sk);
  1018. write_unlock(&wanpipe_sklist_lock);
  1019. sk->sk_socket = NULL;
  1020. kfree(wp_sk(sk));
  1021. wp_sk(sk) = NULL;
  1022. if (atomic_read(&sk->sk_refcnt) != 1) {
  1023. atomic_set(&sk->sk_refcnt, 1);
  1024. DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i ! :timer.\n",
  1025. atomic_read(&sk->sk_refcnt));
  1026. }
  1027. sock_put(sk);
  1028. atomic_dec(&wanpipe_socks_nr);
  1029. return;
  1030. }
  1031. static void wanpipe_kill_sock_irq (struct sock *sk)
  1032. {
  1033. if (!sk)
  1034. return;
  1035. sk->sk_socket = NULL;
  1036. kfree(wp_sk(sk));
  1037. wp_sk(sk) = NULL;
  1038. if (atomic_read(&sk->sk_refcnt) != 1) {
  1039. atomic_set(&sk->sk_refcnt, 1);
  1040. DBG_PRINTK(KERN_INFO "wansock: Error, wrong reference count: %i !:listen.\n",
  1041. atomic_read(&sk->sk_refcnt));
  1042. }
  1043. sock_put(sk);
  1044. atomic_dec(&wanpipe_socks_nr);
  1045. }
  1046. /*============================================================
  1047. * wanpipe_do_bind
  1048. *
  1049. * Bottom half of the binding system call.
  1050. * Once the wanpipe_bind() function checks the
  1051. * legality of the call, this function binds the
  1052. * sock to the driver.
  1053. *===========================================================*/
  1054. static int wanpipe_do_bind(struct sock *sk, struct net_device *dev,
  1055. int protocol)
  1056. {
  1057. wanpipe_opt *wp = wp_sk(sk);
  1058. wanpipe_common_t *chan=NULL;
  1059. int err=0;
  1060. if (sock_flag(sk, SOCK_ZAPPED)) {
  1061. err = -EALREADY;
  1062. goto bind_unlock_exit;
  1063. }
  1064. wp->num = protocol;
  1065. if (protocol == 0){
  1066. release_device(dev);
  1067. err = -EINVAL;
  1068. goto bind_unlock_exit;
  1069. }
  1070. if (dev) {
  1071. if (dev->flags&IFF_UP) {
  1072. chan=dev->priv;
  1073. sk->sk_state = chan->state;
  1074. if (wp->num == htons(X25_PROT) &&
  1075. sk->sk_state != WANSOCK_DISCONNECTED &&
  1076. sk->sk_state != WANSOCK_CONNECTING) {
  1077. DBG_PRINTK(KERN_INFO
  1078. "wansock: Binding to Device not DISCONNECTED %i\n",
  1079. sk->sk_state);
  1080. release_device(dev);
  1081. err = -EAGAIN;
  1082. goto bind_unlock_exit;
  1083. }
  1084. wanpipe_link_driver(dev,sk);
  1085. sk->sk_bound_dev_if = dev->ifindex;
  1086. /* X25 Specific option */
  1087. if (wp->num == htons(X25_PROT))
  1088. wp_sk(sk)->svc = chan->svc;
  1089. } else {
  1090. sk->sk_err = ENETDOWN;
  1091. sk->sk_error_report(sk);
  1092. release_device(dev);
  1093. err = -EINVAL;
  1094. }
  1095. } else {
  1096. err = -ENODEV;
  1097. }
  1098. bind_unlock_exit:
  1099. /* FIXME where is this lock */
  1100. return err;
  1101. }
  1102. /*============================================================
  1103. * wanpipe_bind
  1104. *
  1105. * BIND() System call, which is bound to the AF_WANPIPE
  1106. * operations structure. It checks for correct wanpipe
  1107. * card name, and cross references interface names with
  1108. * the card names. Thus, interface name must belong to
  1109. * the actual card.
  1110. *===========================================================*/
  1111. static int wanpipe_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  1112. {
  1113. struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)uaddr;
  1114. struct sock *sk=sock->sk;
  1115. wanpipe_opt *wp = wp_sk(sk);
  1116. struct net_device *dev = NULL;
  1117. sdla_t *card=NULL;
  1118. char name[15];
  1119. /*
  1120. * Check legality
  1121. */
  1122. if (addr_len < sizeof(struct wan_sockaddr_ll)){
  1123. printk(KERN_INFO "wansock: Address length error\n");
  1124. return -EINVAL;
  1125. }
  1126. if (sll->sll_family != AF_WANPIPE){
  1127. printk(KERN_INFO "wansock: Illegal family name specified.\n");
  1128. return -EINVAL;
  1129. }
  1130. card = wanpipe_find_card (sll->sll_card);
  1131. if (!card){
  1132. printk(KERN_INFO "wansock: Wanpipe card not found: %s\n",sll->sll_card);
  1133. return -ENODEV;
  1134. }else{
  1135. wp_sk(sk)->card = (void *)card;
  1136. }
  1137. if (!strcmp(sll->sll_device,"svc_listen")){
  1138. /* Bind a sock to a card structure for listening
  1139. */
  1140. int err=0;
  1141. /* This is x25 specific area if protocol doesn't
  1142. * match, return error */
  1143. if (sll->sll_protocol != htons(X25_PROT))
  1144. return -EINVAL;
  1145. err= wanpipe_link_card (sk);
  1146. if (err < 0)
  1147. return err;
  1148. if (sll->sll_protocol)
  1149. wp->num = sll->sll_protocol;
  1150. sk->sk_state = WANSOCK_BIND_LISTEN;
  1151. return 0;
  1152. }else if (!strcmp(sll->sll_device,"svc_connect")){
  1153. /* This is x25 specific area if protocol doesn't
  1154. * match, return error */
  1155. if (sll->sll_protocol != htons(X25_PROT))
  1156. return -EINVAL;
  1157. /* Find a free device
  1158. */
  1159. dev = wanpipe_find_free_dev(card);
  1160. if (dev == NULL){
  1161. DBG_PRINTK(KERN_INFO "wansock: No free network devices for card %s\n",
  1162. card->devname);
  1163. return -EINVAL;
  1164. }
  1165. }else{
  1166. /* Bind a socket to a interface name
  1167. * This is used by PVC mostly
  1168. */
  1169. strlcpy(name,sll->sll_device,sizeof(name));
  1170. dev = dev_get_by_name(name);
  1171. if (dev == NULL){
  1172. printk(KERN_INFO "wansock: Failed to get Dev from name: %s,\n",
  1173. name);
  1174. return -ENODEV;
  1175. }
  1176. dev_put(dev);
  1177. if (check_dev(dev, card)){
  1178. printk(KERN_INFO "wansock: Device %s, doesn't belong to card %s\n",
  1179. dev->name, card->devname);
  1180. return -EINVAL;
  1181. }
  1182. if (get_atomic_device (dev))
  1183. return -EINVAL;
  1184. }
  1185. return wanpipe_do_bind(sk, dev, sll->sll_protocol ? : wp->num);
  1186. }
  1187. /*============================================================
  1188. * get_atomic_device
  1189. *
  1190. * Sets a bit atomically which indicates that
  1191. * the interface is taken. This avoids race conditions.
  1192. *===========================================================*/
  1193. static inline int get_atomic_device(struct net_device *dev)
  1194. {
  1195. wanpipe_common_t *chan = dev->priv;
  1196. if (!test_and_set_bit(0,(void *)&chan->rw_bind)){
  1197. return 0;
  1198. }
  1199. return 1;
  1200. }
  1201. /*============================================================
  1202. * check_dev
  1203. *
  1204. * Check that device name belongs to a particular card.
  1205. *===========================================================*/
  1206. static int check_dev(struct net_device *dev, sdla_t *card)
  1207. {
  1208. struct net_device* tmp_dev;
  1209. for (tmp_dev = card->wandev.dev; tmp_dev;
  1210. tmp_dev = *((struct net_device **)tmp_dev->priv)) {
  1211. if (tmp_dev->ifindex == dev->ifindex){
  1212. return 0;
  1213. }
  1214. }
  1215. return 1;
  1216. }
  1217. /*============================================================
  1218. * wanpipe_find_free_dev
  1219. *
  1220. * Find a free network interface. If found set atomic
  1221. * bit indicating that the interface is taken.
  1222. * X25API Specific.
  1223. *===========================================================*/
  1224. struct net_device *wanpipe_find_free_dev(sdla_t *card)
  1225. {
  1226. struct net_device* dev;
  1227. volatile wanpipe_common_t *chan;
  1228. if (test_and_set_bit(0,&find_free_critical)){
  1229. printk(KERN_INFO "CRITICAL in Find Free\n");
  1230. }
  1231. for (dev = card->wandev.dev; dev;
  1232. dev = *((struct net_device **)dev->priv)) {
  1233. chan = dev->priv;
  1234. if (!chan)
  1235. continue;
  1236. if (chan->usedby == API && chan->svc){
  1237. if (!get_atomic_device (dev)){
  1238. if (chan->state != WANSOCK_DISCONNECTED){
  1239. release_device(dev);
  1240. }else{
  1241. clear_bit(0,&find_free_critical);
  1242. return dev;
  1243. }
  1244. }
  1245. }
  1246. }
  1247. clear_bit(0,&find_free_critical);
  1248. return NULL;
  1249. }
  1250. /*============================================================
  1251. * wanpipe_create
  1252. *
  1253. * SOCKET() System call. It allocates a sock structure
  1254. * and adds the socket to the wanpipe_sk_list.
  1255. * Crates AF_WANPIPE socket.
  1256. *===========================================================*/
  1257. static int wanpipe_create(struct socket *sock, int protocol)
  1258. {
  1259. struct sock *sk;
  1260. //FIXME: This checks for root user, SECURITY ?
  1261. //if (!capable(CAP_NET_RAW))
  1262. // return -EPERM;
  1263. if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW)
  1264. return -ESOCKTNOSUPPORT;
  1265. sock->state = SS_UNCONNECTED;
  1266. if ((sk = wanpipe_alloc_socket()) == NULL)
  1267. return -ENOBUFS;
  1268. sk->sk_reuse = 1;
  1269. sock->ops = &wanpipe_ops;
  1270. sock_init_data(sock,sk);
  1271. sock_reset_flag(sk, SOCK_ZAPPED);
  1272. sk->sk_family = PF_WANPIPE;
  1273. wp_sk(sk)->num = protocol;
  1274. sk->sk_state = WANSOCK_DISCONNECTED;
  1275. sk->sk_ack_backlog = 0;
  1276. sk->sk_bound_dev_if = 0;
  1277. atomic_inc(&wanpipe_socks_nr);
  1278. /* We must disable interrupts because the ISR
  1279. * can also change the list */
  1280. set_bit(1,&wanpipe_tx_critical);
  1281. write_lock(&wanpipe_sklist_lock);
  1282. sk_add_node(sk, &wanpipe_sklist);
  1283. write_unlock(&wanpipe_sklist_lock);
  1284. clear_bit(1,&wanpipe_tx_critical);
  1285. return(0);
  1286. }
  1287. /*============================================================
  1288. * wanpipe_recvmsg
  1289. *
  1290. * Pull a packet from our receive queue and hand it
  1291. * to the user. If necessary we block.
  1292. *===========================================================*/
  1293. static int wanpipe_recvmsg(struct kiocb *iocb, struct socket *sock,
  1294. struct msghdr *msg, int len, int flags)
  1295. {
  1296. struct sock *sk = sock->sk;
  1297. struct sk_buff *skb;
  1298. int copied, err=-ENOBUFS;
  1299. /*
  1300. * If the address length field is there to be filled in, we fill
  1301. * it in now.
  1302. */
  1303. msg->msg_namelen = sizeof(struct wan_sockaddr_ll);
  1304. /*
  1305. * Call the generic datagram receiver. This handles all sorts
  1306. * of horrible races and re-entrancy so we can forget about it
  1307. * in the protocol layers.
  1308. *
  1309. * Now it will return ENETDOWN, if device have just gone down,
  1310. * but then it will block.
  1311. */
  1312. if (flags & MSG_OOB){
  1313. skb = skb_dequeue(&sk->sk_error_queue);
  1314. }else{
  1315. skb=skb_recv_datagram(sk,flags,1,&err);
  1316. }
  1317. /*
  1318. * An error occurred so return it. Because skb_recv_datagram()
  1319. * handles the blocking we don't see and worry about blocking
  1320. * retries.
  1321. */
  1322. if(skb==NULL)
  1323. goto out;
  1324. /*
  1325. * You lose any data beyond the buffer you gave. If it worries a
  1326. * user program they can ask the device for its MTU anyway.
  1327. */
  1328. copied = skb->len;
  1329. if (copied > len)
  1330. {
  1331. copied=len;
  1332. msg->msg_flags|=MSG_TRUNC;
  1333. }
  1334. wanpipe_wakeup_driver(sk);
  1335. /* We can't use skb_copy_datagram here */
  1336. err = memcpy_toiovec(msg->msg_iov, skb->data, copied);
  1337. if (err)
  1338. goto out_free;
  1339. sock_recv_timestamp(msg, sk, skb);
  1340. if (msg->msg_name)
  1341. memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
  1342. /*
  1343. * Free or return the buffer as appropriate. Again this
  1344. * hides all the races and re-entrancy issues from us.
  1345. */
  1346. err = (flags&MSG_TRUNC) ? skb->len : copied;
  1347. out_free:
  1348. skb_free_datagram(sk, skb);
  1349. out:
  1350. return err;
  1351. }
  1352. /*============================================================
  1353. * wanpipe_wakeup_driver
  1354. *
  1355. * If socket receive buffer is full and driver cannot
  1356. * pass data up the sock, it sets a packet_block flag.
  1357. * This function check that flag and if sock receive
  1358. * queue has room it kicks the driver BH handler.
  1359. *
  1360. * This way, driver doesn't have to poll the sock
  1361. * receive queue.
  1362. *===========================================================*/
  1363. static void wanpipe_wakeup_driver(struct sock *sk)
  1364. {
  1365. struct net_device *dev = NULL;
  1366. wanpipe_common_t *chan=NULL;
  1367. dev = dev_get_by_index(sk->sk_bound_dev_if);
  1368. if (!dev)
  1369. return;
  1370. dev_put(dev);
  1371. if ((chan = dev->priv) == NULL)
  1372. return;
  1373. if (atomic_read(&chan->receive_block)){
  1374. if (atomic_read(&sk->sk_rmem_alloc) <
  1375. ((unsigned)sk->sk_rcvbuf * 0.9)) {
  1376. printk(KERN_INFO "wansock: Queuing task for wanpipe\n");
  1377. atomic_set(&chan->receive_block,0);
  1378. wanpipe_queue_tq(&chan->wanpipe_task);
  1379. wanpipe_mark_bh();
  1380. }
  1381. }
  1382. }
  1383. /*============================================================
  1384. * wanpipe_getname
  1385. *
  1386. * I don't know what to do with this yet.
  1387. * User can use this function to get sock address
  1388. * information. Not very useful for Sangoma's purposes.
  1389. *===========================================================*/
  1390. static int wanpipe_getname(struct socket *sock, struct sockaddr *uaddr,
  1391. int *uaddr_len, int peer)
  1392. {
  1393. struct net_device *dev;
  1394. struct sock *sk = sock->sk;
  1395. struct wan_sockaddr_ll *sll = (struct wan_sockaddr_ll*)uaddr;
  1396. sll->sll_family = AF_WANPIPE;
  1397. sll->sll_ifindex = sk->sk_bound_dev_if;
  1398. sll->sll_protocol = wp_sk(sk)->num;
  1399. dev = dev_get_by_index(sk->sk_bound_dev_if);
  1400. if (dev) {
  1401. sll->sll_hatype = dev->type;
  1402. sll->sll_halen = dev->addr_len;
  1403. memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
  1404. } else {
  1405. sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */
  1406. sll->sll_halen = 0;
  1407. }
  1408. *uaddr_len = sizeof(*sll);
  1409. dev_put(dev);
  1410. return 0;
  1411. }
  1412. /*============================================================
  1413. * wanpipe_notifier
  1414. *
  1415. * If driver turns off network interface, this function
  1416. * will be envoked. Currently I treate it as a
  1417. * call disconnect. More thought should go into this
  1418. * function.
  1419. *
  1420. * FIXME: More thought should go into this function.
  1421. *
  1422. *===========================================================*/
  1423. static int wanpipe_notifier(struct notifier_block *this, unsigned long msg, void *data)
  1424. {
  1425. struct sock *sk;
  1426. hlist_node *node;
  1427. struct net_device *dev = (struct net_device *)data;
  1428. sk_for_each(sk, node, &wanpipe_sklist) {
  1429. struct wanpipe_opt *po = wp_sk(sk);
  1430. if (!po)
  1431. continue;
  1432. if (dev == NULL)
  1433. continue;
  1434. switch (msg) {
  1435. case NETDEV_DOWN:
  1436. case NETDEV_UNREGISTER:
  1437. if (dev->ifindex == sk->sk_bound_dev_if) {
  1438. printk(KERN_INFO "wansock: Device down %s\n",dev->name);
  1439. if (sock_flag(sk, SOCK_ZAPPED)) {
  1440. wanpipe_unlink_driver(sk);
  1441. sk->sk_err = ENETDOWN;
  1442. sk->sk_error_report(sk);
  1443. }
  1444. if (msg == NETDEV_UNREGISTER) {
  1445. printk(KERN_INFO "wansock: Unregistering Device: %s\n",
  1446. dev->name);
  1447. wanpipe_unlink_driver(sk);
  1448. sk->sk_bound_dev_if = 0;
  1449. }
  1450. }
  1451. break;
  1452. case NETDEV_UP:
  1453. if (dev->ifindex == sk->sk_bound_dev_if &&
  1454. po->num && !sock_flag(sk, SOCK_ZAPPED)) {
  1455. printk(KERN_INFO "wansock: Registering Device: %s\n",
  1456. dev->name);
  1457. wanpipe_link_driver(dev,sk);
  1458. }
  1459. break;
  1460. }
  1461. }
  1462. return NOTIFY_DONE;
  1463. }
  1464. /*============================================================
  1465. * wanpipe_ioctl
  1466. *
  1467. * Execute a user commands, and set socket options.
  1468. *
  1469. * FIXME: More thought should go into this function.
  1470. *
  1471. *===========================================================*/
  1472. static int wanpipe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  1473. {
  1474. struct sock *sk = sock->sk;
  1475. int err;
  1476. switch(cmd)
  1477. {
  1478. case SIOCGSTAMP:
  1479. return sock_get_timestamp(sk, (struct timeval __user *)arg);
  1480. case SIOC_WANPIPE_CHECK_TX:
  1481. return atomic_read(&sk->sk_wmem_alloc);
  1482. case SIOC_WANPIPE_SOCK_STATE:
  1483. if (sk->sk_state == WANSOCK_CONNECTED)
  1484. return 0;
  1485. return 1;
  1486. case SIOC_WANPIPE_GET_CALL_DATA:
  1487. return get_ioctl_cmd (sk,(void*)arg);
  1488. case SIOC_WANPIPE_SET_CALL_DATA:
  1489. return set_ioctl_cmd (sk,(void*)arg);
  1490. case SIOC_WANPIPE_ACCEPT_CALL:
  1491. case SIOC_WANPIPE_CLEAR_CALL:
  1492. case SIOC_WANPIPE_RESET_CALL:
  1493. if ((err=set_ioctl_cmd(sk,(void*)arg)) < 0)
  1494. return err;
  1495. err=wanpipe_exec_cmd(sk,cmd,0);
  1496. get_ioctl_cmd(sk,(void*)arg);
  1497. return err;
  1498. case SIOC_WANPIPE_DEBUG:
  1499. return wanpipe_debug(sk,(void*)arg);
  1500. case SIOC_WANPIPE_SET_NONBLOCK:
  1501. if (sk->sk_state != WANSOCK_DISCONNECTED)
  1502. return -EINVAL;
  1503. sock->file->f_flags |= O_NONBLOCK;
  1504. return 0;
  1505. #ifdef CONFIG_INET
  1506. case SIOCADDRT:
  1507. case SIOCDELRT:
  1508. case SIOCDARP:
  1509. case SIOCGARP:
  1510. case SIOCSARP:
  1511. case SIOCDRARP:
  1512. case SIOCGRARP:
  1513. case SIOCSRARP:
  1514. case SIOCGIFADDR:
  1515. case SIOCSIFADDR:
  1516. case SIOCGIFBRDADDR:
  1517. case SIOCSIFBRDADDR:
  1518. case SIOCGIFNETMASK:
  1519. case SIOCSIFNETMASK:
  1520. case SIOCGIFDSTADDR:
  1521. case SIOCSIFDSTADDR:
  1522. case SIOCSIFFLAGS:
  1523. return inet_dgram_ops.ioctl(sock, cmd, arg);
  1524. #endif
  1525. default:
  1526. return -ENOIOCTLCMD;
  1527. }
  1528. /*NOTREACHED*/
  1529. }
  1530. /*============================================================
  1531. * wanpipe_debug
  1532. *
  1533. * This function will pass up information about all
  1534. * active sockets.
  1535. *
  1536. * FIXME: More thought should go into this function.
  1537. *
  1538. *===========================================================*/
  1539. static int wanpipe_debug (struct sock *origsk, void *arg)
  1540. {
  1541. struct sock *sk;
  1542. struct hlist_node *node;
  1543. struct net_device *dev = NULL;
  1544. wanpipe_common_t *chan=NULL;
  1545. int cnt=0, err=0;
  1546. wan_debug_t *dbg_data = (wan_debug_t *)arg;
  1547. sk_for_each(sk, node, &wanpipe_sklist) {
  1548. wanpipe_opt *wp = wp_sk(sk);
  1549. if (sk == origsk){
  1550. continue;
  1551. }
  1552. if ((err=put_user(1, &dbg_data->debug[cnt].free)))
  1553. return err;
  1554. if ((err = put_user(sk->sk_state,
  1555. &dbg_data->debug[cnt].state_sk)))
  1556. return err;
  1557. if ((err = put_user(sk->sk_rcvbuf,
  1558. &dbg_data->debug[cnt].rcvbuf)))
  1559. return err;
  1560. if ((err = put_user(atomic_read(&sk->sk_rmem_alloc),
  1561. &dbg_data->debug[cnt].rmem)))
  1562. return err;
  1563. if ((err = put_user(atomic_read(&sk->sk_wmem_alloc),
  1564. &dbg_data->debug[cnt].wmem)))
  1565. return err;
  1566. if ((err = put_user(sk->sk_sndbuf,
  1567. &dbg_data->debug[cnt].sndbuf)))
  1568. return err;
  1569. if ((err=put_user(sk_count, &dbg_data->debug[cnt].sk_count)))
  1570. return err;
  1571. if ((err=put_user(wp->poll_cnt, &dbg_data->debug[cnt].poll_cnt)))
  1572. return err;
  1573. if ((err = put_user(sk->sk_bound_dev_if,
  1574. &dbg_data->debug[cnt].bound)))
  1575. return err;
  1576. if (sk->sk_bound_dev_if) {
  1577. dev = dev_get_by_index(sk->sk_bound_dev_if);
  1578. if (!dev)
  1579. continue;
  1580. chan=dev->priv;
  1581. dev_put(dev);
  1582. if ((err=put_user(chan->state, &dbg_data->debug[cnt].d_state)))
  1583. return err;
  1584. if ((err=put_user(chan->svc, &dbg_data->debug[cnt].svc)))
  1585. return err;
  1586. if ((err=put_user(atomic_read(&chan->command),
  1587. &dbg_data->debug[cnt].command)))
  1588. return err;
  1589. if (wp){
  1590. sdla_t *card = (sdla_t*)wp->card;
  1591. if (card){
  1592. if ((err=put_user(atomic_read(&card->u.x.command_busy),
  1593. &dbg_data->debug[cnt].cmd_busy)))
  1594. return err;
  1595. }
  1596. if ((err=put_user(wp->lcn,
  1597. &dbg_data->debug[cnt].lcn)))
  1598. return err;
  1599. if (wp->mbox) {
  1600. if ((err=put_user(1, &dbg_data->debug[cnt].mbox)))
  1601. return err;
  1602. }
  1603. }
  1604. if ((err=put_user(atomic_read(&chan->receive_block),
  1605. &dbg_data->debug[cnt].rblock)))
  1606. return err;
  1607. if (copy_to_user(dbg_data->debug[cnt].name, dev->name, strlen(dev->name)))
  1608. return -EFAULT;
  1609. }
  1610. if (++cnt == MAX_NUM_DEBUG)
  1611. break;
  1612. }
  1613. return 0;
  1614. }
  1615. /*============================================================
  1616. * get_ioctl_cmd
  1617. *
  1618. * Pass up the contents of socket MBOX to the user.
  1619. *===========================================================*/
  1620. static int get_ioctl_cmd (struct sock *sk, void *arg)
  1621. {
  1622. x25api_t *usr_data = (x25api_t *)arg;
  1623. mbox_cmd_t *mbox_ptr;
  1624. int err;
  1625. if (usr_data == NULL)
  1626. return -EINVAL;
  1627. if (!wp_sk(sk)->mbox) {
  1628. return -EINVAL;
  1629. }
  1630. mbox_ptr = (mbox_cmd_t *)wp_sk(sk)->mbox;
  1631. if ((err=put_user(mbox_ptr->cmd.qdm, &usr_data->hdr.qdm)))
  1632. return err;
  1633. if ((err=put_user(mbox_ptr->cmd.cause, &usr_data->hdr.cause)))
  1634. return err;
  1635. if ((err=put_user(mbox_ptr->cmd.diagn, &usr_data->hdr.diagn)))
  1636. return err;
  1637. if ((err=put_user(mbox_ptr->cmd.length, &usr_data->hdr.length)))
  1638. return err;
  1639. if ((err=put_user(mbox_ptr->cmd.result, &usr_data->hdr.result)))
  1640. return err;
  1641. if ((err=put_user(mbox_ptr->cmd.lcn, &usr_data->hdr.lcn)))
  1642. return err;
  1643. if (mbox_ptr->cmd.length > 0){
  1644. if (mbox_ptr->cmd.length > X25_MAX_DATA)
  1645. return -EINVAL;
  1646. if (copy_to_user(usr_data->data, mbox_ptr->data, mbox_ptr->cmd.length)){
  1647. printk(KERN_INFO "wansock: Copy failed !!!\n");
  1648. return -EFAULT;
  1649. }
  1650. }
  1651. return 0;
  1652. }
  1653. /*============================================================
  1654. * set_ioctl_cmd
  1655. *
  1656. * Before command can be execute, socket MBOX must
  1657. * be created, and initialized with user data.
  1658. *===========================================================*/
  1659. static int set_ioctl_cmd (struct sock *sk, void *arg)
  1660. {
  1661. x25api_t *usr_data = (x25api_t *)arg;
  1662. mbox_cmd_t *mbox_ptr;
  1663. int err;
  1664. if (!wp_sk(sk)->mbox) {
  1665. void *mbox_ptr;
  1666. struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
  1667. if (!dev)
  1668. return -ENODEV;
  1669. dev_put(dev);
  1670. if ((mbox_ptr = kzalloc(sizeof(mbox_cmd_t), GFP_ATOMIC)) == NULL)
  1671. return -ENOMEM;
  1672. wp_sk(sk)->mbox = mbox_ptr;
  1673. wanpipe_link_driver(dev,sk);
  1674. }
  1675. mbox_ptr = (mbox_cmd_t*)wp_sk(sk)->mbox;
  1676. memset(mbox_ptr, 0, sizeof(mbox_cmd_t));
  1677. if (usr_data == NULL){
  1678. return 0;
  1679. }
  1680. if ((err=get_user(mbox_ptr->cmd.qdm, &usr_data->hdr.qdm)))
  1681. return err;
  1682. if ((err=get_user(mbox_ptr->cmd.cause, &usr_data->hdr.cause)))
  1683. return err;
  1684. if ((err=get_user(mbox_ptr->cmd.diagn, &usr_data->hdr.diagn)))
  1685. return err;
  1686. if ((err=get_user(mbox_ptr->cmd.length, &usr_data->hdr.length)))
  1687. return err;
  1688. if ((err=get_user(mbox_ptr->cmd.result, &usr_data->hdr.result)))
  1689. return err;
  1690. if (mbox_ptr->cmd.length > 0){
  1691. if (mbox_ptr->cmd.length > X25_MAX_DATA)
  1692. return -EINVAL;
  1693. if (copy_from_user(mbox_ptr->data, usr_data->data, mbox_ptr->cmd.length)){
  1694. printk(KERN_INFO "Copy failed\n");
  1695. return -EFAULT;
  1696. }
  1697. }
  1698. return 0;
  1699. }
  1700. /*======================================================================
  1701. * wanpipe_poll
  1702. *
  1703. * Datagram poll: Again totally generic. This also handles
  1704. * sequenced packet sockets providing the socket receive queue
  1705. * is only ever holding data ready to receive.
  1706. *
  1707. * Note: when you _don't_ use this routine for this protocol,
  1708. * and you use a different write policy from sock_writeable()
  1709. * then please supply your own write_space callback.
  1710. *=====================================================================*/
  1711. unsigned int wanpipe_poll(struct file * file, struct socket *sock, poll_table *wait)
  1712. {
  1713. struct sock *sk = sock->sk;
  1714. unsigned int mask;
  1715. ++wp_sk(sk)->poll_cnt;
  1716. poll_wait(file, sk->sk_sleep, wait);
  1717. mask = 0;
  1718. /* exceptional events? */
  1719. if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) {
  1720. mask |= POLLPRI;
  1721. return mask;
  1722. }
  1723. if (sk->sk_shutdown & RCV_SHUTDOWN)
  1724. mask |= POLLHUP;
  1725. /* readable? */
  1726. if (!skb_queue_empty(&sk->sk_receive_queue)) {
  1727. mask |= POLLIN | POLLRDNORM;
  1728. }
  1729. /* connection hasn't started yet */
  1730. if (sk->sk_state == WANSOCK_CONNECTING) {
  1731. return mask;
  1732. }
  1733. if (sk->sk_state == WANSOCK_DISCONNECTED) {
  1734. mask = POLLPRI;
  1735. return mask;
  1736. }
  1737. /* This check blocks the user process if there is
  1738. * a packet already queued in the socket write queue.
  1739. * This option is only for X25API protocol, for other
  1740. * protocol like chdlc enable streaming mode,
  1741. * where multiple packets can be pending in the socket
  1742. * transmit queue */
  1743. if (wp_sk(sk)->num == htons(X25_PROT)) {
  1744. if (atomic_read(&wp_sk(sk)->packet_sent))
  1745. return mask;
  1746. }
  1747. /* writable? */
  1748. if (sock_writeable(sk)){
  1749. mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
  1750. }else{
  1751. set_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags);
  1752. }
  1753. return mask;
  1754. }
  1755. /*======================================================================
  1756. * wanpipe_listen
  1757. *
  1758. * X25API Specific function. Set a socket into LISTENING MODE.
  1759. *=====================================================================*/
  1760. static int wanpipe_listen(struct socket *sock, int backlog)
  1761. {
  1762. struct sock *sk = sock->sk;
  1763. /* This is x25 specific area if protocol doesn't
  1764. * match, return error */
  1765. if (wp_sk(sk)->num != htons(X25_PROT))
  1766. return -EINVAL;
  1767. if (sk->sk_state == WANSOCK_BIND_LISTEN) {
  1768. sk->sk_max_ack_backlog = backlog;
  1769. sk->sk_state = WANSOCK_LISTEN;
  1770. return 0;
  1771. }else{
  1772. printk(KERN_INFO "wansock: Listening sock was not binded\n");
  1773. }
  1774. return -EINVAL;
  1775. }
  1776. /*======================================================================
  1777. * wanpipe_link_card
  1778. *
  1779. * Connects the listening socket to the driver
  1780. *=====================================================================*/
  1781. static int wanpipe_link_card (struct sock *sk)
  1782. {
  1783. sdla_t *card = (sdla_t*)wp_sk(sk)->card;
  1784. if (!card)
  1785. return -ENOMEM;
  1786. if ((card->sk != NULL) || (card->func != NULL)){
  1787. printk(KERN_INFO "wansock: Listening queue is already established\n");
  1788. return -EINVAL;
  1789. }
  1790. card->sk=sk;
  1791. card->func=wanpipe_listen_rcv;
  1792. sock_set_flag(sk, SOCK_ZAPPED);
  1793. return 0;
  1794. }
  1795. /*======================================================================
  1796. * wanpipe_listen
  1797. *
  1798. * X25API Specific function. Disconnect listening socket from
  1799. * the driver.
  1800. *=====================================================================*/
  1801. static void wanpipe_unlink_card (struct sock *sk)
  1802. {
  1803. sdla_t *card = (sdla_t*)wp_sk(sk)->card;
  1804. if (card){
  1805. card->sk=NULL;
  1806. card->func=NULL;
  1807. }
  1808. }
  1809. /*======================================================================
  1810. * wanpipe_exec_cmd
  1811. *
  1812. * Ioctl function calls this function to execute user command.
  1813. * Connect() sytem call also calls this function to execute
  1814. * place call. This function blocks until command is executed.
  1815. *=====================================================================*/
  1816. static int wanpipe_exec_cmd(struct sock *sk, int cmd, unsigned int flags)
  1817. {
  1818. int err = -EINVAL;
  1819. wanpipe_opt *wp = wp_sk(sk);
  1820. mbox_cmd_t *mbox_ptr = (mbox_cmd_t*)wp->mbox;
  1821. if (!mbox_ptr){
  1822. printk(KERN_INFO "NO MBOX PTR !!!!!\n");
  1823. return -EINVAL;
  1824. }
  1825. /* This is x25 specific area if protocol doesn't
  1826. * match, return error */
  1827. if (wp->num != htons(X25_PROT))
  1828. return -EINVAL;
  1829. switch (cmd){
  1830. case SIOC_WANPIPE_ACCEPT_CALL:
  1831. if (sk->sk_state != WANSOCK_CONNECTING) {
  1832. err = -EHOSTDOWN;
  1833. break;
  1834. }
  1835. err = execute_command(sk,X25_ACCEPT_CALL,0);
  1836. if (err < 0)
  1837. break;
  1838. /* Update. Mar6 2000.
  1839. * Do not set the sock lcn number here, since
  1840. * it is done in wanpipe_listen_rcv().
  1841. */
  1842. if (sk->sk_state == WANSOCK_CONNECTED) {
  1843. wp->lcn = ((mbox_cmd_t*)wp->mbox)->cmd.lcn;
  1844. DBG_PRINTK(KERN_INFO "\nwansock: Accept OK %i\n",
  1845. wp->lcn);
  1846. err = 0;
  1847. }else{
  1848. DBG_PRINTK (KERN_INFO "\nwansock: Accept Failed %i\n",
  1849. wp->lcn);
  1850. wp->lcn = 0;
  1851. err = -ECONNREFUSED;
  1852. }
  1853. break;
  1854. case SIOC_WANPIPE_CLEAR_CALL:
  1855. if (sk->sk_state == WANSOCK_DISCONNECTED) {
  1856. err = -EINVAL;
  1857. break;
  1858. }
  1859. /* Check if data buffers are pending for transmission,
  1860. * if so, check whether user wants to wait until data
  1861. * is transmitted, or clear a call and drop packets */
  1862. if (atomic_read(&sk->sk_wmem_alloc) ||
  1863. check_driver_busy(sk)) {
  1864. mbox_cmd_t *mbox = wp->mbox;
  1865. if (mbox->cmd.qdm & 0x80){
  1866. mbox->cmd.result = 0x35;
  1867. err = -EAGAIN;
  1868. break;
  1869. }
  1870. }
  1871. sk->sk_state = WANSOCK_DISCONNECTING;
  1872. err = execute_command(sk,X25_CLEAR_CALL,0);
  1873. if (err < 0)
  1874. break;
  1875. err = -ECONNREFUSED;
  1876. if (sk->sk_state == WANSOCK_DISCONNECTED) {
  1877. DBG_PRINTK(KERN_INFO "\nwansock: CLEAR OK %i\n",
  1878. wp->lcn);
  1879. wp->lcn = 0;
  1880. err = 0;
  1881. }
  1882. break;
  1883. case SIOC_WANPIPE_RESET_CALL:
  1884. if (sk->sk_state != WANSOCK_CONNECTED) {
  1885. err = -EINVAL;
  1886. break;
  1887. }
  1888. /* Check if data buffers are pending for transmission,
  1889. * if so, check whether user wants to wait until data
  1890. * is transmitted, or reset a call and drop packets */
  1891. if (atomic_read(&sk->sk_wmem_alloc) ||
  1892. check_driver_busy(sk)) {
  1893. mbox_cmd_t *mbox = wp->mbox;
  1894. if (mbox->cmd.qdm & 0x80){
  1895. mbox->cmd.result = 0x35;
  1896. err = -EAGAIN;
  1897. break;
  1898. }
  1899. }
  1900. err = execute_command(sk, X25_RESET,0);
  1901. if (err < 0)
  1902. break;
  1903. err = mbox_ptr->cmd.result;
  1904. break;
  1905. case X25_PLACE_CALL:
  1906. err=execute_command(sk,X25_PLACE_CALL,flags);
  1907. if (err < 0)
  1908. break;
  1909. if (sk->sk_state == WANSOCK_CONNECTED) {
  1910. wp->lcn = ((mbox_cmd_t*)wp->mbox)->cmd.lcn;
  1911. DBG_PRINTK(KERN_INFO "\nwansock: PLACE CALL OK %i\n",
  1912. wp->lcn);
  1913. err = 0;
  1914. } else if (sk->sk_state == WANSOCK_CONNECTING &&
  1915. (flags & O_NONBLOCK)) {
  1916. wp->lcn = ((mbox_cmd_t*)wp->mbox)->cmd.lcn;
  1917. DBG_PRINTK(KERN_INFO "\nwansock: Place Call OK: Waiting %i\n",
  1918. wp->lcn);
  1919. err = 0;
  1920. }else{
  1921. DBG_PRINTK(KERN_INFO "\nwansock: Place call Failed\n");
  1922. err = -ECONNREFUSED;
  1923. }
  1924. break;
  1925. default:
  1926. return -EINVAL;
  1927. }
  1928. return err;
  1929. }
  1930. static int check_driver_busy (struct sock *sk)
  1931. {
  1932. struct net_device *dev = dev_get_by_index(sk->sk_bound_dev_if);
  1933. wanpipe_common_t *chan;
  1934. if (!dev)
  1935. return 0;
  1936. dev_put(dev);
  1937. if ((chan=dev->priv) == NULL)
  1938. return 0;
  1939. return atomic_read(&chan->driver_busy);
  1940. }
  1941. /*======================================================================
  1942. * wanpipe_accept
  1943. *
  1944. * ACCEPT() System call. X25API Specific function.
  1945. * For each incoming call, create a new socket and
  1946. * return it to the user.
  1947. *=====================================================================*/
  1948. static int wanpipe_accept(struct socket *sock, struct socket *newsock, int flags)
  1949. {
  1950. struct sock *sk;
  1951. struct sock *newsk;
  1952. struct sk_buff *skb;
  1953. DECLARE_WAITQUEUE(wait, current);
  1954. int err=0;
  1955. if (newsock->sk != NULL){
  1956. wanpipe_kill_sock_accept(newsock->sk);
  1957. newsock->sk=NULL;
  1958. }
  1959. if ((sk = sock->sk) == NULL)
  1960. return -EINVAL;
  1961. if (sk->sk_type != SOCK_RAW)
  1962. return -EOPNOTSUPP;
  1963. if (sk->sk_state != WANSOCK_LISTEN)
  1964. return -EINVAL;
  1965. if (wp_sk(sk)->num != htons(X25_PROT))
  1966. return -EINVAL;
  1967. add_wait_queue(sk->sk_sleep,&wait);
  1968. current->state = TASK_INTERRUPTIBLE;
  1969. for (;;){
  1970. skb = skb_dequeue(&sk->sk_receive_queue);
  1971. if (skb){
  1972. err=0;
  1973. break;
  1974. }
  1975. if (signal_pending(current)) {
  1976. err = -ERESTARTSYS;
  1977. break;
  1978. }
  1979. schedule();
  1980. }
  1981. current->state = TASK_RUNNING;
  1982. remove_wait_queue(sk->sk_sleep,&wait);
  1983. if (err != 0)
  1984. return err;
  1985. newsk = get_newsk_from_skb(skb);
  1986. if (!newsk){
  1987. return -EINVAL;
  1988. }
  1989. set_bit(1,&wanpipe_tx_critical);
  1990. write_lock(&wanpipe_sklist_lock);
  1991. sk_add_node(newsk, &wanpipe_sklist);
  1992. write_unlock(&wanpipe_sklist_lock);
  1993. clear_bit(1,&wanpipe_tx_critical);
  1994. newsk->sk_socket = newsock;
  1995. newsk->sk_sleep = &newsock->wait;
  1996. /* Now attach up the new socket */
  1997. sk->sk_ack_backlog--;
  1998. newsock->sk = newsk;
  1999. kfree_skb(skb);
  2000. DBG_PRINTK(KERN_INFO "\nwansock: ACCEPT Got LCN %i\n",
  2001. wp_sk(newsk)->lcn);
  2002. return 0;
  2003. }
  2004. /*======================================================================
  2005. * get_newsk_from_skb
  2006. *
  2007. * Accept() uses this function to get the address of the new
  2008. * socket structure.
  2009. *=====================================================================*/
  2010. struct sock * get_newsk_from_skb (struct sk_buff *skb)
  2011. {
  2012. struct net_device *dev = skb->dev;
  2013. wanpipe_common_t *chan;
  2014. if (!dev){
  2015. return NULL;
  2016. }
  2017. if ((chan = dev->priv) == NULL){
  2018. return NULL;
  2019. }
  2020. if (!chan->sk){
  2021. return NULL;
  2022. }
  2023. return (struct sock *)chan->sk;
  2024. }
  2025. /*======================================================================
  2026. * wanpipe_connect
  2027. *
  2028. * CONNECT() System Call. X25API specific function
  2029. * Check the state of the sock, and execute PLACE_CALL command.
  2030. * Connect can ether block or return without waiting for connection,
  2031. * if specified by user.
  2032. *=====================================================================*/
  2033. static int wanpipe_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
  2034. {
  2035. struct sock *sk = sock->sk;
  2036. struct wan_sockaddr_ll *addr = (struct wan_sockaddr_ll*)uaddr;
  2037. struct net_device *dev;
  2038. int err;
  2039. if (wp_sk(sk)->num != htons(X25_PROT))
  2040. return -EINVAL;
  2041. if (sk->sk_state == WANSOCK_CONNECTED)
  2042. return -EISCONN; /* No reconnect on a seqpacket socket */
  2043. if (sk->sk_state != WAN_DISCONNECTED) {
  2044. printk(KERN_INFO "wansock: Trying to connect on channel NON DISCONNECT\n");
  2045. return -ECONNREFUSED;
  2046. }
  2047. sk->sk_state = WANSOCK_DISCONNECTED;
  2048. sock->state = SS_UNCONNECTED;
  2049. if (addr_len != sizeof(struct wan_sockaddr_ll))
  2050. return -EINVAL;
  2051. if (addr->sll_family != AF_WANPIPE)
  2052. return -EINVAL;
  2053. if ((dev = dev_get_by_index(sk->sk_bound_dev_if)) == NULL)
  2054. return -ENETUNREACH;
  2055. dev_put(dev);
  2056. if (!sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
  2057. return -EINVAL;
  2058. sock->state = SS_CONNECTING;
  2059. sk->sk_state = WANSOCK_CONNECTING;
  2060. if (!wp_sk(sk)->mbox) {
  2061. if (wp_sk (sk)->svc)
  2062. return -EINVAL;
  2063. else {
  2064. int err;
  2065. if ((err=set_ioctl_cmd(sk,NULL)) < 0)
  2066. return err;
  2067. }
  2068. }
  2069. if ((err=wanpipe_exec_cmd(sk, X25_PLACE_CALL,flags)) != 0){
  2070. sock->state = SS_UNCONNECTED;
  2071. sk->sk_state = WANSOCK_CONNECTED;
  2072. return err;
  2073. }
  2074. if (sk->sk_state != WANSOCK_CONNECTED && (flags & O_NONBLOCK)) {
  2075. return 0;
  2076. }
  2077. if (sk->sk_state != WANSOCK_CONNECTED) {
  2078. sock->state = SS_UNCONNECTED;
  2079. return -ECONNREFUSED;
  2080. }
  2081. sock->state = SS_CONNECTED;
  2082. return 0;
  2083. }
  2084. const struct proto_ops wanpipe_ops = {
  2085. .family = PF_WANPIPE,
  2086. .owner = THIS_MODULE,
  2087. .release = wanpipe_release,
  2088. .bind = wanpipe_bind,
  2089. .connect = wanpipe_connect,
  2090. .socketpair = sock_no_socketpair,
  2091. .accept = wanpipe_accept,
  2092. .getname = wanpipe_getname,
  2093. .poll = wanpipe_poll,
  2094. .ioctl = wanpipe_ioctl,
  2095. .listen = wanpipe_listen,
  2096. .shutdown = sock_no_shutdown,
  2097. .setsockopt = sock_no_setsockopt,
  2098. .getsockopt = sock_no_getsockopt,
  2099. .sendmsg = wanpipe_sendmsg,
  2100. .recvmsg = wanpipe_recvmsg
  2101. };
  2102. static struct net_proto_family wanpipe_family_ops = {
  2103. .family = PF_WANPIPE,
  2104. .create = wanpipe_create,
  2105. .owner = THIS_MODULE,
  2106. };
  2107. struct notifier_block wanpipe_netdev_notifier = {
  2108. .notifier_call = wanpipe_notifier,
  2109. };
  2110. #ifdef MODULE
  2111. void cleanup_module(void)
  2112. {
  2113. printk(KERN_INFO "wansock: Cleaning up \n");
  2114. unregister_netdevice_notifier(&wanpipe_netdev_notifier);
  2115. sock_unregister(PF_WANPIPE);
  2116. proto_unregister(&wanpipe_proto);
  2117. }
  2118. int init_module(void)
  2119. {
  2120. int rc;
  2121. printk(KERN_INFO "wansock: Registering Socket \n");
  2122. rc = proto_register(&wanpipe_proto, 0);
  2123. if (rc != 0)
  2124. goto out;
  2125. sock_register(&wanpipe_family_ops);
  2126. register_netdevice_notifier(&wanpipe_netdev_notifier);
  2127. out:
  2128. return rc;
  2129. }
  2130. #endif
  2131. MODULE_LICENSE("GPL");
  2132. MODULE_ALIAS_NETPROTO(PF_WANPIPE);