af_wanpipe.c 66 KB

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