af_wanpipe.c 66 KB

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