af_wanpipe.c 66 KB

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