strip.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811
  1. /*
  2. * Copyright 1996 The Board of Trustees of The Leland Stanford
  3. * Junior University. All Rights Reserved.
  4. *
  5. * Permission to use, copy, modify, and distribute this
  6. * software and its documentation for any purpose and without
  7. * fee is hereby granted, provided that the above copyright
  8. * notice appear in all copies. Stanford University
  9. * makes no representations about the suitability of this
  10. * software for any purpose. It is provided "as is" without
  11. * express or implied warranty.
  12. *
  13. * strip.c This module implements Starmode Radio IP (STRIP)
  14. * for kernel-based devices like TTY. It interfaces between a
  15. * raw TTY, and the kernel's INET protocol layers (via DDI).
  16. *
  17. * Version: @(#)strip.c 1.3 July 1997
  18. *
  19. * Author: Stuart Cheshire <cheshire@cs.stanford.edu>
  20. *
  21. * Fixes: v0.9 12th Feb 1996 (SC)
  22. * New byte stuffing (2+6 run-length encoding)
  23. * New watchdog timer task
  24. * New Protocol key (SIP0)
  25. *
  26. * v0.9.1 3rd March 1996 (SC)
  27. * Changed to dynamic device allocation -- no more compile
  28. * time (or boot time) limit on the number of STRIP devices.
  29. *
  30. * v0.9.2 13th March 1996 (SC)
  31. * Uses arp cache lookups (but doesn't send arp packets yet)
  32. *
  33. * v0.9.3 17th April 1996 (SC)
  34. * Fixed bug where STR_ERROR flag was getting set unneccessarily
  35. * (causing otherwise good packets to be unneccessarily dropped)
  36. *
  37. * v0.9.4 27th April 1996 (SC)
  38. * First attempt at using "&COMMAND" Starmode AT commands
  39. *
  40. * v0.9.5 29th May 1996 (SC)
  41. * First attempt at sending (unicast) ARP packets
  42. *
  43. * v0.9.6 5th June 1996 (Elliot)
  44. * Put "message level" tags in every "printk" statement
  45. *
  46. * v0.9.7 13th June 1996 (laik)
  47. * Added support for the /proc fs
  48. *
  49. * v0.9.8 July 1996 (Mema)
  50. * Added packet logging
  51. *
  52. * v1.0 November 1996 (SC)
  53. * Fixed (severe) memory leaks in the /proc fs code
  54. * Fixed race conditions in the logging code
  55. *
  56. * v1.1 January 1997 (SC)
  57. * Deleted packet logging (use tcpdump instead)
  58. * Added support for Metricom Firmware v204 features
  59. * (like message checksums)
  60. *
  61. * v1.2 January 1997 (SC)
  62. * Put portables list back in
  63. *
  64. * v1.3 July 1997 (SC)
  65. * Made STRIP driver set the radio's baud rate automatically.
  66. * It is no longer necessarily to manually set the radio's
  67. * rate permanently to 115200 -- the driver handles setting
  68. * the rate automatically.
  69. */
  70. #ifdef MODULE
  71. static const char StripVersion[] = "1.3A-STUART.CHESHIRE-MODULAR";
  72. #else
  73. static const char StripVersion[] = "1.3A-STUART.CHESHIRE";
  74. #endif
  75. #define TICKLE_TIMERS 0
  76. #define EXT_COUNTERS 1
  77. /************************************************************************/
  78. /* Header files */
  79. #include <linux/kernel.h>
  80. #include <linux/module.h>
  81. #include <linux/init.h>
  82. #include <linux/bitops.h>
  83. #include <asm/system.h>
  84. #include <asm/uaccess.h>
  85. # include <linux/ctype.h>
  86. #include <linux/string.h>
  87. #include <linux/mm.h>
  88. #include <linux/interrupt.h>
  89. #include <linux/in.h>
  90. #include <linux/tty.h>
  91. #include <linux/errno.h>
  92. #include <linux/netdevice.h>
  93. #include <linux/inetdevice.h>
  94. #include <linux/etherdevice.h>
  95. #include <linux/skbuff.h>
  96. #include <linux/if_arp.h>
  97. #include <linux/if_strip.h>
  98. #include <linux/proc_fs.h>
  99. #include <linux/seq_file.h>
  100. #include <linux/serial.h>
  101. #include <linux/serialP.h>
  102. #include <linux/rcupdate.h>
  103. #include <net/arp.h>
  104. #include <net/net_namespace.h>
  105. #include <linux/ip.h>
  106. #include <linux/tcp.h>
  107. #include <linux/time.h>
  108. #include <linux/jiffies.h>
  109. /************************************************************************/
  110. /* Useful structures and definitions */
  111. /*
  112. * A MetricomKey identifies the protocol being carried inside a Metricom
  113. * Starmode packet.
  114. */
  115. typedef union {
  116. __u8 c[4];
  117. __u32 l;
  118. } MetricomKey;
  119. /*
  120. * An IP address can be viewed as four bytes in memory (which is what it is) or as
  121. * a single 32-bit long (which is convenient for assignment, equality testing etc.)
  122. */
  123. typedef union {
  124. __u8 b[4];
  125. __u32 l;
  126. } IPaddr;
  127. /*
  128. * A MetricomAddressString is used to hold a printable representation of
  129. * a Metricom address.
  130. */
  131. typedef struct {
  132. __u8 c[24];
  133. } MetricomAddressString;
  134. /* Encapsulation can expand packet of size x to 65/64x + 1
  135. * Sent packet looks like "<CR>*<address>*<key><encaps payload><CR>"
  136. * 1 1 1-18 1 4 ? 1
  137. * eg. <CR>*0000-1234*SIP0<encaps payload><CR>
  138. * We allow 31 bytes for the stars, the key, the address and the <CR>s
  139. */
  140. #define STRIP_ENCAP_SIZE(X) (32 + (X)*65L/64L)
  141. /*
  142. * A STRIP_Header is never really sent over the radio, but making a dummy
  143. * header for internal use within the kernel that looks like an Ethernet
  144. * header makes certain other software happier. For example, tcpdump
  145. * already understands Ethernet headers.
  146. */
  147. typedef struct {
  148. MetricomAddress dst_addr; /* Destination address, e.g. "0000-1234" */
  149. MetricomAddress src_addr; /* Source address, e.g. "0000-5678" */
  150. unsigned short protocol; /* The protocol type, using Ethernet codes */
  151. } STRIP_Header;
  152. typedef struct {
  153. char c[60];
  154. } MetricomNode;
  155. #define NODE_TABLE_SIZE 32
  156. typedef struct {
  157. struct timeval timestamp;
  158. int num_nodes;
  159. MetricomNode node[NODE_TABLE_SIZE];
  160. } MetricomNodeTable;
  161. enum { FALSE = 0, TRUE = 1 };
  162. /*
  163. * Holds the radio's firmware version.
  164. */
  165. typedef struct {
  166. char c[50];
  167. } FirmwareVersion;
  168. /*
  169. * Holds the radio's serial number.
  170. */
  171. typedef struct {
  172. char c[18];
  173. } SerialNumber;
  174. /*
  175. * Holds the radio's battery voltage.
  176. */
  177. typedef struct {
  178. char c[11];
  179. } BatteryVoltage;
  180. typedef struct {
  181. char c[8];
  182. } char8;
  183. enum {
  184. NoStructure = 0, /* Really old firmware */
  185. StructuredMessages = 1, /* Parsable AT response msgs */
  186. ChecksummedMessages = 2 /* Parsable AT response msgs with checksums */
  187. };
  188. struct strip {
  189. int magic;
  190. /*
  191. * These are pointers to the malloc()ed frame buffers.
  192. */
  193. unsigned char *rx_buff; /* buffer for received IP packet */
  194. unsigned char *sx_buff; /* buffer for received serial data */
  195. int sx_count; /* received serial data counter */
  196. int sx_size; /* Serial buffer size */
  197. unsigned char *tx_buff; /* transmitter buffer */
  198. unsigned char *tx_head; /* pointer to next byte to XMIT */
  199. int tx_left; /* bytes left in XMIT queue */
  200. int tx_size; /* Serial buffer size */
  201. /*
  202. * STRIP interface statistics.
  203. */
  204. unsigned long rx_packets; /* inbound frames counter */
  205. unsigned long tx_packets; /* outbound frames counter */
  206. unsigned long rx_errors; /* Parity, etc. errors */
  207. unsigned long tx_errors; /* Planned stuff */
  208. unsigned long rx_dropped; /* No memory for skb */
  209. unsigned long tx_dropped; /* When MTU change */
  210. unsigned long rx_over_errors; /* Frame bigger than STRIP buf. */
  211. unsigned long pps_timer; /* Timer to determine pps */
  212. unsigned long rx_pps_count; /* Counter to determine pps */
  213. unsigned long tx_pps_count; /* Counter to determine pps */
  214. unsigned long sx_pps_count; /* Counter to determine pps */
  215. unsigned long rx_average_pps; /* rx packets per second * 8 */
  216. unsigned long tx_average_pps; /* tx packets per second * 8 */
  217. unsigned long sx_average_pps; /* sent packets per second * 8 */
  218. #ifdef EXT_COUNTERS
  219. unsigned long rx_bytes; /* total received bytes */
  220. unsigned long tx_bytes; /* total received bytes */
  221. unsigned long rx_rbytes; /* bytes thru radio i/f */
  222. unsigned long tx_rbytes; /* bytes thru radio i/f */
  223. unsigned long rx_sbytes; /* tot bytes thru serial i/f */
  224. unsigned long tx_sbytes; /* tot bytes thru serial i/f */
  225. unsigned long rx_ebytes; /* tot stat/err bytes */
  226. unsigned long tx_ebytes; /* tot stat/err bytes */
  227. #endif
  228. /*
  229. * Internal variables.
  230. */
  231. struct list_head list; /* Linked list of devices */
  232. int discard; /* Set if serial error */
  233. int working; /* Is radio working correctly? */
  234. int firmware_level; /* Message structuring level */
  235. int next_command; /* Next periodic command */
  236. unsigned int user_baud; /* The user-selected baud rate */
  237. int mtu; /* Our mtu (to spot changes!) */
  238. long watchdog_doprobe; /* Next time to test the radio */
  239. long watchdog_doreset; /* Time to do next reset */
  240. long gratuitous_arp; /* Time to send next ARP refresh */
  241. long arp_interval; /* Next ARP interval */
  242. struct timer_list idle_timer; /* For periodic wakeup calls */
  243. MetricomAddress true_dev_addr; /* True address of radio */
  244. int manual_dev_addr; /* Hack: See note below */
  245. FirmwareVersion firmware_version; /* The radio's firmware version */
  246. SerialNumber serial_number; /* The radio's serial number */
  247. BatteryVoltage battery_voltage; /* The radio's battery voltage */
  248. /*
  249. * Other useful structures.
  250. */
  251. struct tty_struct *tty; /* ptr to TTY structure */
  252. struct net_device *dev; /* Our device structure */
  253. /*
  254. * Neighbour radio records
  255. */
  256. MetricomNodeTable portables;
  257. MetricomNodeTable poletops;
  258. };
  259. /*
  260. * Note: manual_dev_addr hack
  261. *
  262. * It is not possible to change the hardware address of a Metricom radio,
  263. * or to send packets with a user-specified hardware source address, thus
  264. * trying to manually set a hardware source address is a questionable
  265. * thing to do. However, if the user *does* manually set the hardware
  266. * source address of a STRIP interface, then the kernel will believe it,
  267. * and use it in certain places. For example, the hardware address listed
  268. * by ifconfig will be the manual address, not the true one.
  269. * (Both addresses are listed in /proc/net/strip.)
  270. * Also, ARP packets will be sent out giving the user-specified address as
  271. * the source address, not the real address. This is dangerous, because
  272. * it means you won't receive any replies -- the ARP replies will go to
  273. * the specified address, which will be some other radio. The case where
  274. * this is useful is when that other radio is also connected to the same
  275. * machine. This allows you to connect a pair of radios to one machine,
  276. * and to use one exclusively for inbound traffic, and the other
  277. * exclusively for outbound traffic. Pretty neat, huh?
  278. *
  279. * Here's the full procedure to set this up:
  280. *
  281. * 1. "slattach" two interfaces, e.g. st0 for outgoing packets,
  282. * and st1 for incoming packets
  283. *
  284. * 2. "ifconfig" st0 (outbound radio) to have the hardware address
  285. * which is the real hardware address of st1 (inbound radio).
  286. * Now when it sends out packets, it will masquerade as st1, and
  287. * replies will be sent to that radio, which is exactly what we want.
  288. *
  289. * 3. Set the route table entry ("route add default ..." or
  290. * "route add -net ...", as appropriate) to send packets via the st0
  291. * interface (outbound radio). Do not add any route which sends packets
  292. * out via the st1 interface -- that radio is for inbound traffic only.
  293. *
  294. * 4. "ifconfig" st1 (inbound radio) to have hardware address zero.
  295. * This tells the STRIP driver to "shut down" that interface and not
  296. * send any packets through it. In particular, it stops sending the
  297. * periodic gratuitous ARP packets that a STRIP interface normally sends.
  298. * Also, when packets arrive on that interface, it will search the
  299. * interface list to see if there is another interface who's manual
  300. * hardware address matches its own real address (i.e. st0 in this
  301. * example) and if so it will transfer ownership of the skbuff to
  302. * that interface, so that it looks to the kernel as if the packet
  303. * arrived on that interface. This is necessary because when the
  304. * kernel sends an ARP packet on st0, it expects to get a reply on
  305. * st0, and if it sees the reply come from st1 then it will ignore
  306. * it (to be accurate, it puts the entry in the ARP table, but
  307. * labelled in such a way that st0 can't use it).
  308. *
  309. * Thanks to Petros Maniatis for coming up with the idea of splitting
  310. * inbound and outbound traffic between two interfaces, which turned
  311. * out to be really easy to implement, even if it is a bit of a hack.
  312. *
  313. * Having set a manual address on an interface, you can restore it
  314. * to automatic operation (where the address is automatically kept
  315. * consistent with the real address of the radio) by setting a manual
  316. * address of all ones, e.g. "ifconfig st0 hw strip FFFFFFFFFFFF"
  317. * This 'turns off' manual override mode for the device address.
  318. *
  319. * Note: The IEEE 802 headers reported in tcpdump will show the *real*
  320. * radio addresses the packets were sent and received from, so that you
  321. * can see what is really going on with packets, and which interfaces
  322. * they are really going through.
  323. */
  324. /************************************************************************/
  325. /* Constants */
  326. /*
  327. * CommandString1 works on all radios
  328. * Other CommandStrings are only used with firmware that provides structured responses.
  329. *
  330. * ats319=1 Enables Info message for node additions and deletions
  331. * ats319=2 Enables Info message for a new best node
  332. * ats319=4 Enables checksums
  333. * ats319=8 Enables ACK messages
  334. */
  335. static const int MaxCommandStringLength = 32;
  336. static const int CompatibilityCommand = 1;
  337. static const char CommandString0[] = "*&COMMAND*ATS319=7"; /* Turn on checksums & info messages */
  338. static const char CommandString1[] = "*&COMMAND*ATS305?"; /* Query radio name */
  339. static const char CommandString2[] = "*&COMMAND*ATS325?"; /* Query battery voltage */
  340. static const char CommandString3[] = "*&COMMAND*ATS300?"; /* Query version information */
  341. static const char CommandString4[] = "*&COMMAND*ATS311?"; /* Query poletop list */
  342. static const char CommandString5[] = "*&COMMAND*AT~LA"; /* Query portables list */
  343. typedef struct {
  344. const char *string;
  345. long length;
  346. } StringDescriptor;
  347. static const StringDescriptor CommandString[] = {
  348. {CommandString0, sizeof(CommandString0) - 1},
  349. {CommandString1, sizeof(CommandString1) - 1},
  350. {CommandString2, sizeof(CommandString2) - 1},
  351. {CommandString3, sizeof(CommandString3) - 1},
  352. {CommandString4, sizeof(CommandString4) - 1},
  353. {CommandString5, sizeof(CommandString5) - 1}
  354. };
  355. #define GOT_ALL_RADIO_INFO(S) \
  356. ((S)->firmware_version.c[0] && \
  357. (S)->battery_voltage.c[0] && \
  358. memcmp(&(S)->true_dev_addr, zero_address.c, sizeof(zero_address)))
  359. static const char hextable[16] = "0123456789ABCDEF";
  360. static const MetricomAddress zero_address;
  361. static const MetricomAddress broadcast_address =
  362. { {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} };
  363. static const MetricomKey SIP0Key = { "SIP0" };
  364. static const MetricomKey ARP0Key = { "ARP0" };
  365. static const MetricomKey ATR_Key = { "ATR " };
  366. static const MetricomKey ACK_Key = { "ACK_" };
  367. static const MetricomKey INF_Key = { "INF_" };
  368. static const MetricomKey ERR_Key = { "ERR_" };
  369. static const long MaxARPInterval = 60 * HZ; /* One minute */
  370. /*
  371. * Maximum Starmode packet length is 1183 bytes. Allowing 4 bytes for
  372. * protocol key, 4 bytes for checksum, one byte for CR, and 65/64 expansion
  373. * for STRIP encoding, that translates to a maximum payload MTU of 1155.
  374. * Note: A standard NFS 1K data packet is a total of 0x480 (1152) bytes
  375. * long, including IP header, UDP header, and NFS header. Setting the STRIP
  376. * MTU to 1152 allows us to send default sized NFS packets without fragmentation.
  377. */
  378. static const unsigned short MAX_SEND_MTU = 1152;
  379. static const unsigned short MAX_RECV_MTU = 1500; /* Hoping for Ethernet sized packets in the future! */
  380. static const unsigned short DEFAULT_STRIP_MTU = 1152;
  381. static const int STRIP_MAGIC = 0x5303;
  382. static const long LongTime = 0x7FFFFFFF;
  383. /************************************************************************/
  384. /* Global variables */
  385. static LIST_HEAD(strip_list);
  386. static DEFINE_SPINLOCK(strip_lock);
  387. /************************************************************************/
  388. /* Macros */
  389. /* Returns TRUE if text T begins with prefix P */
  390. #define has_prefix(T,L,P) (((L) >= sizeof(P)-1) && !strncmp((T), (P), sizeof(P)-1))
  391. /* Returns TRUE if text T of length L is equal to string S */
  392. #define text_equal(T,L,S) (((L) == sizeof(S)-1) && !strncmp((T), (S), sizeof(S)-1))
  393. #define READHEX(X) ((X)>='0' && (X)<='9' ? (X)-'0' : \
  394. (X)>='a' && (X)<='f' ? (X)-'a'+10 : \
  395. (X)>='A' && (X)<='F' ? (X)-'A'+10 : 0 )
  396. #define READHEX16(X) ((__u16)(READHEX(X)))
  397. #define READDEC(X) ((X)>='0' && (X)<='9' ? (X)-'0' : 0)
  398. #define ARRAY_END(X) (&((X)[ARRAY_SIZE(X)]))
  399. #define JIFFIE_TO_SEC(X) ((X) / HZ)
  400. /************************************************************************/
  401. /* Utility routines */
  402. static int arp_query(unsigned char *haddr, u32 paddr,
  403. struct net_device *dev)
  404. {
  405. struct neighbour *neighbor_entry;
  406. int ret = 0;
  407. neighbor_entry = neigh_lookup(&arp_tbl, &paddr, dev);
  408. if (neighbor_entry != NULL) {
  409. neighbor_entry->used = jiffies;
  410. if (neighbor_entry->nud_state & NUD_VALID) {
  411. memcpy(haddr, neighbor_entry->ha, dev->addr_len);
  412. ret = 1;
  413. }
  414. neigh_release(neighbor_entry);
  415. }
  416. return ret;
  417. }
  418. static void DumpData(char *msg, struct strip *strip_info, __u8 * ptr,
  419. __u8 * end)
  420. {
  421. static const int MAX_DumpData = 80;
  422. __u8 pkt_text[MAX_DumpData], *p = pkt_text;
  423. *p++ = '\"';
  424. while (ptr < end && p < &pkt_text[MAX_DumpData - 4]) {
  425. if (*ptr == '\\') {
  426. *p++ = '\\';
  427. *p++ = '\\';
  428. } else {
  429. if (*ptr >= 32 && *ptr <= 126) {
  430. *p++ = *ptr;
  431. } else {
  432. sprintf(p, "\\%02X", *ptr);
  433. p += 3;
  434. }
  435. }
  436. ptr++;
  437. }
  438. if (ptr == end)
  439. *p++ = '\"';
  440. *p++ = 0;
  441. printk(KERN_INFO "%s: %-13s%s\n", strip_info->dev->name, msg, pkt_text);
  442. }
  443. /************************************************************************/
  444. /* Byte stuffing/unstuffing routines */
  445. /* Stuffing scheme:
  446. * 00 Unused (reserved character)
  447. * 01-3F Run of 2-64 different characters
  448. * 40-7F Run of 1-64 different characters plus a single zero at the end
  449. * 80-BF Run of 1-64 of the same character
  450. * C0-FF Run of 1-64 zeroes (ASCII 0)
  451. */
  452. typedef enum {
  453. Stuff_Diff = 0x00,
  454. Stuff_DiffZero = 0x40,
  455. Stuff_Same = 0x80,
  456. Stuff_Zero = 0xC0,
  457. Stuff_NoCode = 0xFF, /* Special code, meaning no code selected */
  458. Stuff_CodeMask = 0xC0,
  459. Stuff_CountMask = 0x3F,
  460. Stuff_MaxCount = 0x3F,
  461. Stuff_Magic = 0x0D /* The value we are eliminating */
  462. } StuffingCode;
  463. /* StuffData encodes the data starting at "src" for "length" bytes.
  464. * It writes it to the buffer pointed to by "dst" (which must be at least
  465. * as long as 1 + 65/64 of the input length). The output may be up to 1.6%
  466. * larger than the input for pathological input, but will usually be smaller.
  467. * StuffData returns the new value of the dst pointer as its result.
  468. * "code_ptr_ptr" points to a "__u8 *" which is used to hold encoding state
  469. * between calls, allowing an encoded packet to be incrementally built up
  470. * from small parts. On the first call, the "__u8 *" pointed to should be
  471. * initialized to NULL; between subsequent calls the calling routine should
  472. * leave the value alone and simply pass it back unchanged so that the
  473. * encoder can recover its current state.
  474. */
  475. #define StuffData_FinishBlock(X) \
  476. (*code_ptr = (X) ^ Stuff_Magic, code = Stuff_NoCode)
  477. static __u8 *StuffData(__u8 * src, __u32 length, __u8 * dst,
  478. __u8 ** code_ptr_ptr)
  479. {
  480. __u8 *end = src + length;
  481. __u8 *code_ptr = *code_ptr_ptr;
  482. __u8 code = Stuff_NoCode, count = 0;
  483. if (!length)
  484. return (dst);
  485. if (code_ptr) {
  486. /*
  487. * Recover state from last call, if applicable
  488. */
  489. code = (*code_ptr ^ Stuff_Magic) & Stuff_CodeMask;
  490. count = (*code_ptr ^ Stuff_Magic) & Stuff_CountMask;
  491. }
  492. while (src < end) {
  493. switch (code) {
  494. /* Stuff_NoCode: If no current code, select one */
  495. case Stuff_NoCode:
  496. /* Record where we're going to put this code */
  497. code_ptr = dst++;
  498. count = 0; /* Reset the count (zero means one instance) */
  499. /* Tentatively start a new block */
  500. if (*src == 0) {
  501. code = Stuff_Zero;
  502. src++;
  503. } else {
  504. code = Stuff_Same;
  505. *dst++ = *src++ ^ Stuff_Magic;
  506. }
  507. /* Note: We optimistically assume run of same -- */
  508. /* which will be fixed later in Stuff_Same */
  509. /* if it turns out not to be true. */
  510. break;
  511. /* Stuff_Zero: We already have at least one zero encoded */
  512. case Stuff_Zero:
  513. /* If another zero, count it, else finish this code block */
  514. if (*src == 0) {
  515. count++;
  516. src++;
  517. } else {
  518. StuffData_FinishBlock(Stuff_Zero + count);
  519. }
  520. break;
  521. /* Stuff_Same: We already have at least one byte encoded */
  522. case Stuff_Same:
  523. /* If another one the same, count it */
  524. if ((*src ^ Stuff_Magic) == code_ptr[1]) {
  525. count++;
  526. src++;
  527. break;
  528. }
  529. /* else, this byte does not match this block. */
  530. /* If we already have two or more bytes encoded, finish this code block */
  531. if (count) {
  532. StuffData_FinishBlock(Stuff_Same + count);
  533. break;
  534. }
  535. /* else, we only have one so far, so switch to Stuff_Diff code */
  536. code = Stuff_Diff;
  537. /* and fall through to Stuff_Diff case below
  538. * Note cunning cleverness here: case Stuff_Diff compares
  539. * the current character with the previous two to see if it
  540. * has a run of three the same. Won't this be an error if
  541. * there aren't two previous characters stored to compare with?
  542. * No. Because we know the current character is *not* the same
  543. * as the previous one, the first test below will necessarily
  544. * fail and the send half of the "if" won't be executed.
  545. */
  546. /* Stuff_Diff: We have at least two *different* bytes encoded */
  547. case Stuff_Diff:
  548. /* If this is a zero, must encode a Stuff_DiffZero, and begin a new block */
  549. if (*src == 0) {
  550. StuffData_FinishBlock(Stuff_DiffZero +
  551. count);
  552. }
  553. /* else, if we have three in a row, it is worth starting a Stuff_Same block */
  554. else if ((*src ^ Stuff_Magic) == dst[-1]
  555. && dst[-1] == dst[-2]) {
  556. /* Back off the last two characters we encoded */
  557. code += count - 2;
  558. /* Note: "Stuff_Diff + 0" is an illegal code */
  559. if (code == Stuff_Diff + 0) {
  560. code = Stuff_Same + 0;
  561. }
  562. StuffData_FinishBlock(code);
  563. code_ptr = dst - 2;
  564. /* dst[-1] already holds the correct value */
  565. count = 2; /* 2 means three bytes encoded */
  566. code = Stuff_Same;
  567. }
  568. /* else, another different byte, so add it to the block */
  569. else {
  570. *dst++ = *src ^ Stuff_Magic;
  571. count++;
  572. }
  573. src++; /* Consume the byte */
  574. break;
  575. }
  576. if (count == Stuff_MaxCount) {
  577. StuffData_FinishBlock(code + count);
  578. }
  579. }
  580. if (code == Stuff_NoCode) {
  581. *code_ptr_ptr = NULL;
  582. } else {
  583. *code_ptr_ptr = code_ptr;
  584. StuffData_FinishBlock(code + count);
  585. }
  586. return (dst);
  587. }
  588. /*
  589. * UnStuffData decodes the data at "src", up to (but not including) "end".
  590. * It writes the decoded data into the buffer pointed to by "dst", up to a
  591. * maximum of "dst_length", and returns the new value of "src" so that a
  592. * follow-on call can read more data, continuing from where the first left off.
  593. *
  594. * There are three types of results:
  595. * 1. The source data runs out before extracting "dst_length" bytes:
  596. * UnStuffData returns NULL to indicate failure.
  597. * 2. The source data produces exactly "dst_length" bytes:
  598. * UnStuffData returns new_src = end to indicate that all bytes were consumed.
  599. * 3. "dst_length" bytes are extracted, with more remaining.
  600. * UnStuffData returns new_src < end to indicate that there are more bytes
  601. * to be read.
  602. *
  603. * Note: The decoding may be destructive, in that it may alter the source
  604. * data in the process of decoding it (this is necessary to allow a follow-on
  605. * call to resume correctly).
  606. */
  607. static __u8 *UnStuffData(__u8 * src, __u8 * end, __u8 * dst,
  608. __u32 dst_length)
  609. {
  610. __u8 *dst_end = dst + dst_length;
  611. /* Sanity check */
  612. if (!src || !end || !dst || !dst_length)
  613. return (NULL);
  614. while (src < end && dst < dst_end) {
  615. int count = (*src ^ Stuff_Magic) & Stuff_CountMask;
  616. switch ((*src ^ Stuff_Magic) & Stuff_CodeMask) {
  617. case Stuff_Diff:
  618. if (src + 1 + count >= end)
  619. return (NULL);
  620. do {
  621. *dst++ = *++src ^ Stuff_Magic;
  622. }
  623. while (--count >= 0 && dst < dst_end);
  624. if (count < 0)
  625. src += 1;
  626. else {
  627. if (count == 0)
  628. *src = Stuff_Same ^ Stuff_Magic;
  629. else
  630. *src =
  631. (Stuff_Diff +
  632. count) ^ Stuff_Magic;
  633. }
  634. break;
  635. case Stuff_DiffZero:
  636. if (src + 1 + count >= end)
  637. return (NULL);
  638. do {
  639. *dst++ = *++src ^ Stuff_Magic;
  640. }
  641. while (--count >= 0 && dst < dst_end);
  642. if (count < 0)
  643. *src = Stuff_Zero ^ Stuff_Magic;
  644. else
  645. *src =
  646. (Stuff_DiffZero + count) ^ Stuff_Magic;
  647. break;
  648. case Stuff_Same:
  649. if (src + 1 >= end)
  650. return (NULL);
  651. do {
  652. *dst++ = src[1] ^ Stuff_Magic;
  653. }
  654. while (--count >= 0 && dst < dst_end);
  655. if (count < 0)
  656. src += 2;
  657. else
  658. *src = (Stuff_Same + count) ^ Stuff_Magic;
  659. break;
  660. case Stuff_Zero:
  661. do {
  662. *dst++ = 0;
  663. }
  664. while (--count >= 0 && dst < dst_end);
  665. if (count < 0)
  666. src += 1;
  667. else
  668. *src = (Stuff_Zero + count) ^ Stuff_Magic;
  669. break;
  670. }
  671. }
  672. if (dst < dst_end)
  673. return (NULL);
  674. else
  675. return (src);
  676. }
  677. /************************************************************************/
  678. /* General routines for STRIP */
  679. /*
  680. * set_baud sets the baud rate to the rate defined by baudcode
  681. */
  682. static void set_baud(struct tty_struct *tty, speed_t baudrate)
  683. {
  684. struct ktermios old_termios;
  685. mutex_lock(&tty->termios_mutex);
  686. old_termios =*(tty->termios);
  687. tty_encode_baud_rate(tty, baudrate, baudrate);
  688. tty->ops->set_termios(tty, &old_termios);
  689. mutex_unlock(&tty->termios_mutex);
  690. }
  691. /*
  692. * Convert a string to a Metricom Address.
  693. */
  694. #define IS_RADIO_ADDRESS(p) ( \
  695. isdigit((p)[0]) && isdigit((p)[1]) && isdigit((p)[2]) && isdigit((p)[3]) && \
  696. (p)[4] == '-' && \
  697. isdigit((p)[5]) && isdigit((p)[6]) && isdigit((p)[7]) && isdigit((p)[8]) )
  698. static int string_to_radio_address(MetricomAddress * addr, __u8 * p)
  699. {
  700. if (!IS_RADIO_ADDRESS(p))
  701. return (1);
  702. addr->c[0] = 0;
  703. addr->c[1] = 0;
  704. addr->c[2] = READHEX(p[0]) << 4 | READHEX(p[1]);
  705. addr->c[3] = READHEX(p[2]) << 4 | READHEX(p[3]);
  706. addr->c[4] = READHEX(p[5]) << 4 | READHEX(p[6]);
  707. addr->c[5] = READHEX(p[7]) << 4 | READHEX(p[8]);
  708. return (0);
  709. }
  710. /*
  711. * Convert a Metricom Address to a string.
  712. */
  713. static __u8 *radio_address_to_string(const MetricomAddress * addr,
  714. MetricomAddressString * p)
  715. {
  716. sprintf(p->c, "%02X%02X-%02X%02X", addr->c[2], addr->c[3],
  717. addr->c[4], addr->c[5]);
  718. return (p->c);
  719. }
  720. /*
  721. * Note: Must make sure sx_size is big enough to receive a stuffed
  722. * MAX_RECV_MTU packet. Additionally, we also want to ensure that it's
  723. * big enough to receive a large radio neighbour list (currently 4K).
  724. */
  725. static int allocate_buffers(struct strip *strip_info, int mtu)
  726. {
  727. struct net_device *dev = strip_info->dev;
  728. int sx_size = max_t(int, STRIP_ENCAP_SIZE(MAX_RECV_MTU), 4096);
  729. int tx_size = STRIP_ENCAP_SIZE(mtu) + MaxCommandStringLength;
  730. __u8 *r = kmalloc(MAX_RECV_MTU, GFP_ATOMIC);
  731. __u8 *s = kmalloc(sx_size, GFP_ATOMIC);
  732. __u8 *t = kmalloc(tx_size, GFP_ATOMIC);
  733. if (r && s && t) {
  734. strip_info->rx_buff = r;
  735. strip_info->sx_buff = s;
  736. strip_info->tx_buff = t;
  737. strip_info->sx_size = sx_size;
  738. strip_info->tx_size = tx_size;
  739. strip_info->mtu = dev->mtu = mtu;
  740. return (1);
  741. }
  742. kfree(r);
  743. kfree(s);
  744. kfree(t);
  745. return (0);
  746. }
  747. /*
  748. * MTU has been changed by the IP layer.
  749. * We could be in
  750. * an upcall from the tty driver, or in an ip packet queue.
  751. */
  752. static int strip_change_mtu(struct net_device *dev, int new_mtu)
  753. {
  754. struct strip *strip_info = netdev_priv(dev);
  755. int old_mtu = strip_info->mtu;
  756. unsigned char *orbuff = strip_info->rx_buff;
  757. unsigned char *osbuff = strip_info->sx_buff;
  758. unsigned char *otbuff = strip_info->tx_buff;
  759. unsigned long flags;
  760. if (new_mtu > MAX_SEND_MTU) {
  761. printk(KERN_ERR
  762. "%s: MTU exceeds maximum allowable (%d), MTU change cancelled.\n",
  763. strip_info->dev->name, MAX_SEND_MTU);
  764. return -EINVAL;
  765. }
  766. spin_lock_irqsave(&strip_lock, flags);
  767. if (!allocate_buffers(strip_info, new_mtu)) {
  768. printk(KERN_ERR "%s: unable to grow strip buffers, MTU change cancelled.\n",
  769. strip_info->dev->name);
  770. spin_unlock_irqrestore(&strip_lock, flags);
  771. return -ENOMEM;
  772. }
  773. if (strip_info->sx_count) {
  774. if (strip_info->sx_count <= strip_info->sx_size)
  775. memcpy(strip_info->sx_buff, osbuff,
  776. strip_info->sx_count);
  777. else {
  778. strip_info->discard = strip_info->sx_count;
  779. strip_info->rx_over_errors++;
  780. }
  781. }
  782. if (strip_info->tx_left) {
  783. if (strip_info->tx_left <= strip_info->tx_size)
  784. memcpy(strip_info->tx_buff, strip_info->tx_head,
  785. strip_info->tx_left);
  786. else {
  787. strip_info->tx_left = 0;
  788. strip_info->tx_dropped++;
  789. }
  790. }
  791. strip_info->tx_head = strip_info->tx_buff;
  792. spin_unlock_irqrestore(&strip_lock, flags);
  793. printk(KERN_NOTICE "%s: strip MTU changed fom %d to %d.\n",
  794. strip_info->dev->name, old_mtu, strip_info->mtu);
  795. kfree(orbuff);
  796. kfree(osbuff);
  797. kfree(otbuff);
  798. return 0;
  799. }
  800. static void strip_unlock(struct strip *strip_info)
  801. {
  802. /*
  803. * Set the timer to go off in one second.
  804. */
  805. strip_info->idle_timer.expires = jiffies + 1 * HZ;
  806. add_timer(&strip_info->idle_timer);
  807. netif_wake_queue(strip_info->dev);
  808. }
  809. /*
  810. * If the time is in the near future, time_delta prints the number of
  811. * seconds to go into the buffer and returns the address of the buffer.
  812. * If the time is not in the near future, it returns the address of the
  813. * string "Not scheduled" The buffer must be long enough to contain the
  814. * ascii representation of the number plus 9 charactes for the " seconds"
  815. * and the null character.
  816. */
  817. #ifdef CONFIG_PROC_FS
  818. static char *time_delta(char buffer[], long time)
  819. {
  820. time -= jiffies;
  821. if (time > LongTime / 2)
  822. return ("Not scheduled");
  823. if (time < 0)
  824. time = 0; /* Don't print negative times */
  825. sprintf(buffer, "%ld seconds", time / HZ);
  826. return (buffer);
  827. }
  828. /* get Nth element of the linked list */
  829. static struct strip *strip_get_idx(loff_t pos)
  830. {
  831. struct strip *str;
  832. int i = 0;
  833. list_for_each_entry_rcu(str, &strip_list, list) {
  834. if (pos == i)
  835. return str;
  836. ++i;
  837. }
  838. return NULL;
  839. }
  840. static void *strip_seq_start(struct seq_file *seq, loff_t *pos)
  841. __acquires(RCU)
  842. {
  843. rcu_read_lock();
  844. return *pos ? strip_get_idx(*pos - 1) : SEQ_START_TOKEN;
  845. }
  846. static void *strip_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  847. {
  848. struct list_head *l;
  849. struct strip *s;
  850. ++*pos;
  851. if (v == SEQ_START_TOKEN)
  852. return strip_get_idx(1);
  853. s = v;
  854. l = &s->list;
  855. list_for_each_continue_rcu(l, &strip_list) {
  856. return list_entry(l, struct strip, list);
  857. }
  858. return NULL;
  859. }
  860. static void strip_seq_stop(struct seq_file *seq, void *v)
  861. __releases(RCU)
  862. {
  863. rcu_read_unlock();
  864. }
  865. static void strip_seq_neighbours(struct seq_file *seq,
  866. const MetricomNodeTable * table,
  867. const char *title)
  868. {
  869. unsigned long flags;
  870. struct timeval t;
  871. /*
  872. * We wrap this in a do/while loop, so if the table changes
  873. * while we're reading it, we just go around and try again.
  874. */
  875. do {
  876. int i;
  877. t = table->timestamp;
  878. if (table->num_nodes)
  879. seq_printf(seq, "\n %s\n", title);
  880. for (i = 0; i < table->num_nodes; i++) {
  881. MetricomNode node;
  882. spin_lock_irqsave(&strip_lock, flags);
  883. node = table->node[i];
  884. spin_unlock_irqrestore(&strip_lock, flags);
  885. seq_printf(seq, " %s\n", node.c);
  886. }
  887. } while (table->timestamp.tv_sec != t.tv_sec
  888. || table->timestamp.tv_usec != t.tv_usec);
  889. }
  890. /*
  891. * This function prints radio status information via the seq_file
  892. * interface. The interface takes care of buffer size and over
  893. * run issues.
  894. *
  895. * The buffer in seq_file is PAGESIZE (4K)
  896. * so this routine should never print more or it will get truncated.
  897. * With the maximum of 32 portables and 32 poletops
  898. * reported, the routine outputs 3107 bytes into the buffer.
  899. */
  900. static void strip_seq_status_info(struct seq_file *seq,
  901. const struct strip *strip_info)
  902. {
  903. char temp[32];
  904. MetricomAddressString addr_string;
  905. /* First, we must copy all of our data to a safe place, */
  906. /* in case a serial interrupt comes in and changes it. */
  907. int tx_left = strip_info->tx_left;
  908. unsigned long rx_average_pps = strip_info->rx_average_pps;
  909. unsigned long tx_average_pps = strip_info->tx_average_pps;
  910. unsigned long sx_average_pps = strip_info->sx_average_pps;
  911. int working = strip_info->working;
  912. int firmware_level = strip_info->firmware_level;
  913. long watchdog_doprobe = strip_info->watchdog_doprobe;
  914. long watchdog_doreset = strip_info->watchdog_doreset;
  915. long gratuitous_arp = strip_info->gratuitous_arp;
  916. long arp_interval = strip_info->arp_interval;
  917. FirmwareVersion firmware_version = strip_info->firmware_version;
  918. SerialNumber serial_number = strip_info->serial_number;
  919. BatteryVoltage battery_voltage = strip_info->battery_voltage;
  920. char *if_name = strip_info->dev->name;
  921. MetricomAddress true_dev_addr = strip_info->true_dev_addr;
  922. MetricomAddress dev_dev_addr =
  923. *(MetricomAddress *) strip_info->dev->dev_addr;
  924. int manual_dev_addr = strip_info->manual_dev_addr;
  925. #ifdef EXT_COUNTERS
  926. unsigned long rx_bytes = strip_info->rx_bytes;
  927. unsigned long tx_bytes = strip_info->tx_bytes;
  928. unsigned long rx_rbytes = strip_info->rx_rbytes;
  929. unsigned long tx_rbytes = strip_info->tx_rbytes;
  930. unsigned long rx_sbytes = strip_info->rx_sbytes;
  931. unsigned long tx_sbytes = strip_info->tx_sbytes;
  932. unsigned long rx_ebytes = strip_info->rx_ebytes;
  933. unsigned long tx_ebytes = strip_info->tx_ebytes;
  934. #endif
  935. seq_printf(seq, "\nInterface name\t\t%s\n", if_name);
  936. seq_printf(seq, " Radio working:\t\t%s\n", working ? "Yes" : "No");
  937. radio_address_to_string(&true_dev_addr, &addr_string);
  938. seq_printf(seq, " Radio address:\t\t%s\n", addr_string.c);
  939. if (manual_dev_addr) {
  940. radio_address_to_string(&dev_dev_addr, &addr_string);
  941. seq_printf(seq, " Device address:\t%s\n", addr_string.c);
  942. }
  943. seq_printf(seq, " Firmware version:\t%s", !working ? "Unknown" :
  944. !firmware_level ? "Should be upgraded" :
  945. firmware_version.c);
  946. if (firmware_level >= ChecksummedMessages)
  947. seq_printf(seq, " (Checksums Enabled)");
  948. seq_printf(seq, "\n");
  949. seq_printf(seq, " Serial number:\t\t%s\n", serial_number.c);
  950. seq_printf(seq, " Battery voltage:\t%s\n", battery_voltage.c);
  951. seq_printf(seq, " Transmit queue (bytes):%d\n", tx_left);
  952. seq_printf(seq, " Receive packet rate: %ld packets per second\n",
  953. rx_average_pps / 8);
  954. seq_printf(seq, " Transmit packet rate: %ld packets per second\n",
  955. tx_average_pps / 8);
  956. seq_printf(seq, " Sent packet rate: %ld packets per second\n",
  957. sx_average_pps / 8);
  958. seq_printf(seq, " Next watchdog probe:\t%s\n",
  959. time_delta(temp, watchdog_doprobe));
  960. seq_printf(seq, " Next watchdog reset:\t%s\n",
  961. time_delta(temp, watchdog_doreset));
  962. seq_printf(seq, " Next gratuitous ARP:\t");
  963. if (!memcmp
  964. (strip_info->dev->dev_addr, zero_address.c,
  965. sizeof(zero_address)))
  966. seq_printf(seq, "Disabled\n");
  967. else {
  968. seq_printf(seq, "%s\n", time_delta(temp, gratuitous_arp));
  969. seq_printf(seq, " Next ARP interval:\t%ld seconds\n",
  970. JIFFIE_TO_SEC(arp_interval));
  971. }
  972. if (working) {
  973. #ifdef EXT_COUNTERS
  974. seq_printf(seq, "\n");
  975. seq_printf(seq,
  976. " Total bytes: \trx:\t%lu\ttx:\t%lu\n",
  977. rx_bytes, tx_bytes);
  978. seq_printf(seq,
  979. " thru radio: \trx:\t%lu\ttx:\t%lu\n",
  980. rx_rbytes, tx_rbytes);
  981. seq_printf(seq,
  982. " thru serial port: \trx:\t%lu\ttx:\t%lu\n",
  983. rx_sbytes, tx_sbytes);
  984. seq_printf(seq,
  985. " Total stat/err bytes:\trx:\t%lu\ttx:\t%lu\n",
  986. rx_ebytes, tx_ebytes);
  987. #endif
  988. strip_seq_neighbours(seq, &strip_info->poletops,
  989. "Poletops:");
  990. strip_seq_neighbours(seq, &strip_info->portables,
  991. "Portables:");
  992. }
  993. }
  994. /*
  995. * This function is exports status information from the STRIP driver through
  996. * the /proc file system.
  997. */
  998. static int strip_seq_show(struct seq_file *seq, void *v)
  999. {
  1000. if (v == SEQ_START_TOKEN)
  1001. seq_printf(seq, "strip_version: %s\n", StripVersion);
  1002. else
  1003. strip_seq_status_info(seq, (const struct strip *)v);
  1004. return 0;
  1005. }
  1006. static const struct seq_operations strip_seq_ops = {
  1007. .start = strip_seq_start,
  1008. .next = strip_seq_next,
  1009. .stop = strip_seq_stop,
  1010. .show = strip_seq_show,
  1011. };
  1012. static int strip_seq_open(struct inode *inode, struct file *file)
  1013. {
  1014. return seq_open(file, &strip_seq_ops);
  1015. }
  1016. static const struct file_operations strip_seq_fops = {
  1017. .owner = THIS_MODULE,
  1018. .open = strip_seq_open,
  1019. .read = seq_read,
  1020. .llseek = seq_lseek,
  1021. .release = seq_release,
  1022. };
  1023. #endif
  1024. /************************************************************************/
  1025. /* Sending routines */
  1026. static void ResetRadio(struct strip *strip_info)
  1027. {
  1028. struct tty_struct *tty = strip_info->tty;
  1029. static const char init[] = "ate0q1dt**starmode\r**";
  1030. StringDescriptor s = { init, sizeof(init) - 1 };
  1031. /*
  1032. * If the radio isn't working anymore,
  1033. * we should clear the old status information.
  1034. */
  1035. if (strip_info->working) {
  1036. printk(KERN_INFO "%s: No response: Resetting radio.\n",
  1037. strip_info->dev->name);
  1038. strip_info->firmware_version.c[0] = '\0';
  1039. strip_info->serial_number.c[0] = '\0';
  1040. strip_info->battery_voltage.c[0] = '\0';
  1041. strip_info->portables.num_nodes = 0;
  1042. do_gettimeofday(&strip_info->portables.timestamp);
  1043. strip_info->poletops.num_nodes = 0;
  1044. do_gettimeofday(&strip_info->poletops.timestamp);
  1045. }
  1046. strip_info->pps_timer = jiffies;
  1047. strip_info->rx_pps_count = 0;
  1048. strip_info->tx_pps_count = 0;
  1049. strip_info->sx_pps_count = 0;
  1050. strip_info->rx_average_pps = 0;
  1051. strip_info->tx_average_pps = 0;
  1052. strip_info->sx_average_pps = 0;
  1053. /* Mark radio address as unknown */
  1054. *(MetricomAddress *) & strip_info->true_dev_addr = zero_address;
  1055. if (!strip_info->manual_dev_addr)
  1056. *(MetricomAddress *) strip_info->dev->dev_addr =
  1057. zero_address;
  1058. strip_info->working = FALSE;
  1059. strip_info->firmware_level = NoStructure;
  1060. strip_info->next_command = CompatibilityCommand;
  1061. strip_info->watchdog_doprobe = jiffies + 10 * HZ;
  1062. strip_info->watchdog_doreset = jiffies + 1 * HZ;
  1063. /* If the user has selected a baud rate above 38.4 see what magic we have to do */
  1064. if (strip_info->user_baud > 38400) {
  1065. /*
  1066. * Subtle stuff: Pay attention :-)
  1067. * If the serial port is currently at the user's selected (>38.4) rate,
  1068. * then we temporarily switch to 19.2 and issue the ATS304 command
  1069. * to tell the radio to switch to the user's selected rate.
  1070. * If the serial port is not currently at that rate, that means we just
  1071. * issued the ATS304 command last time through, so this time we restore
  1072. * the user's selected rate and issue the normal starmode reset string.
  1073. */
  1074. if (strip_info->user_baud == tty_get_baud_rate(tty)) {
  1075. static const char b0[] = "ate0q1s304=57600\r";
  1076. static const char b1[] = "ate0q1s304=115200\r";
  1077. static const StringDescriptor baudstring[2] =
  1078. { {b0, sizeof(b0) - 1}
  1079. , {b1, sizeof(b1) - 1}
  1080. };
  1081. set_baud(tty, 19200);
  1082. if (strip_info->user_baud == 57600)
  1083. s = baudstring[0];
  1084. else if (strip_info->user_baud == 115200)
  1085. s = baudstring[1];
  1086. else
  1087. s = baudstring[1]; /* For now */
  1088. } else
  1089. set_baud(tty, strip_info->user_baud);
  1090. }
  1091. tty->ops->write(tty, s.string, s.length);
  1092. #ifdef EXT_COUNTERS
  1093. strip_info->tx_ebytes += s.length;
  1094. #endif
  1095. }
  1096. /*
  1097. * Called by the driver when there's room for more data. If we have
  1098. * more packets to send, we send them here.
  1099. */
  1100. static void strip_write_some_more(struct tty_struct *tty)
  1101. {
  1102. struct strip *strip_info = tty->disc_data;
  1103. /* First make sure we're connected. */
  1104. if (!strip_info || strip_info->magic != STRIP_MAGIC ||
  1105. !netif_running(strip_info->dev))
  1106. return;
  1107. if (strip_info->tx_left > 0) {
  1108. int num_written =
  1109. tty->ops->write(tty, strip_info->tx_head,
  1110. strip_info->tx_left);
  1111. strip_info->tx_left -= num_written;
  1112. strip_info->tx_head += num_written;
  1113. #ifdef EXT_COUNTERS
  1114. strip_info->tx_sbytes += num_written;
  1115. #endif
  1116. } else { /* Else start transmission of another packet */
  1117. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  1118. strip_unlock(strip_info);
  1119. }
  1120. }
  1121. static __u8 *add_checksum(__u8 * buffer, __u8 * end)
  1122. {
  1123. __u16 sum = 0;
  1124. __u8 *p = buffer;
  1125. while (p < end)
  1126. sum += *p++;
  1127. end[3] = hextable[sum & 0xF];
  1128. sum >>= 4;
  1129. end[2] = hextable[sum & 0xF];
  1130. sum >>= 4;
  1131. end[1] = hextable[sum & 0xF];
  1132. sum >>= 4;
  1133. end[0] = hextable[sum & 0xF];
  1134. return (end + 4);
  1135. }
  1136. static unsigned char *strip_make_packet(unsigned char *buffer,
  1137. struct strip *strip_info,
  1138. struct sk_buff *skb)
  1139. {
  1140. __u8 *ptr = buffer;
  1141. __u8 *stuffstate = NULL;
  1142. STRIP_Header *header = (STRIP_Header *) skb->data;
  1143. MetricomAddress haddr = header->dst_addr;
  1144. int len = skb->len - sizeof(STRIP_Header);
  1145. MetricomKey key;
  1146. /*HexDump("strip_make_packet", strip_info, skb->data, skb->data + skb->len); */
  1147. if (header->protocol == htons(ETH_P_IP))
  1148. key = SIP0Key;
  1149. else if (header->protocol == htons(ETH_P_ARP))
  1150. key = ARP0Key;
  1151. else {
  1152. printk(KERN_ERR
  1153. "%s: strip_make_packet: Unknown packet type 0x%04X\n",
  1154. strip_info->dev->name, ntohs(header->protocol));
  1155. return (NULL);
  1156. }
  1157. if (len > strip_info->mtu) {
  1158. printk(KERN_ERR
  1159. "%s: Dropping oversized transmit packet: %d bytes\n",
  1160. strip_info->dev->name, len);
  1161. return (NULL);
  1162. }
  1163. /*
  1164. * If we're sending to ourselves, discard the packet.
  1165. * (Metricom radios choke if they try to send a packet to their own address.)
  1166. */
  1167. if (!memcmp(haddr.c, strip_info->true_dev_addr.c, sizeof(haddr))) {
  1168. printk(KERN_ERR "%s: Dropping packet addressed to self\n",
  1169. strip_info->dev->name);
  1170. return (NULL);
  1171. }
  1172. /*
  1173. * If this is a broadcast packet, send it to our designated Metricom
  1174. * 'broadcast hub' radio (First byte of address being 0xFF means broadcast)
  1175. */
  1176. if (haddr.c[0] == 0xFF) {
  1177. __be32 brd = 0;
  1178. struct in_device *in_dev;
  1179. rcu_read_lock();
  1180. in_dev = __in_dev_get_rcu(strip_info->dev);
  1181. if (in_dev == NULL) {
  1182. rcu_read_unlock();
  1183. return NULL;
  1184. }
  1185. if (in_dev->ifa_list)
  1186. brd = in_dev->ifa_list->ifa_broadcast;
  1187. rcu_read_unlock();
  1188. /* arp_query returns 1 if it succeeds in looking up the address, 0 if it fails */
  1189. if (!arp_query(haddr.c, brd, strip_info->dev)) {
  1190. printk(KERN_ERR
  1191. "%s: Unable to send packet (no broadcast hub configured)\n",
  1192. strip_info->dev->name);
  1193. return (NULL);
  1194. }
  1195. /*
  1196. * If we are the broadcast hub, don't bother sending to ourselves.
  1197. * (Metricom radios choke if they try to send a packet to their own address.)
  1198. */
  1199. if (!memcmp
  1200. (haddr.c, strip_info->true_dev_addr.c, sizeof(haddr)))
  1201. return (NULL);
  1202. }
  1203. *ptr++ = 0x0D;
  1204. *ptr++ = '*';
  1205. *ptr++ = hextable[haddr.c[2] >> 4];
  1206. *ptr++ = hextable[haddr.c[2] & 0xF];
  1207. *ptr++ = hextable[haddr.c[3] >> 4];
  1208. *ptr++ = hextable[haddr.c[3] & 0xF];
  1209. *ptr++ = '-';
  1210. *ptr++ = hextable[haddr.c[4] >> 4];
  1211. *ptr++ = hextable[haddr.c[4] & 0xF];
  1212. *ptr++ = hextable[haddr.c[5] >> 4];
  1213. *ptr++ = hextable[haddr.c[5] & 0xF];
  1214. *ptr++ = '*';
  1215. *ptr++ = key.c[0];
  1216. *ptr++ = key.c[1];
  1217. *ptr++ = key.c[2];
  1218. *ptr++ = key.c[3];
  1219. ptr =
  1220. StuffData(skb->data + sizeof(STRIP_Header), len, ptr,
  1221. &stuffstate);
  1222. if (strip_info->firmware_level >= ChecksummedMessages)
  1223. ptr = add_checksum(buffer + 1, ptr);
  1224. *ptr++ = 0x0D;
  1225. return (ptr);
  1226. }
  1227. static void strip_send(struct strip *strip_info, struct sk_buff *skb)
  1228. {
  1229. MetricomAddress haddr;
  1230. unsigned char *ptr = strip_info->tx_buff;
  1231. int doreset = (long) jiffies - strip_info->watchdog_doreset >= 0;
  1232. int doprobe = (long) jiffies - strip_info->watchdog_doprobe >= 0
  1233. && !doreset;
  1234. __be32 addr, brd;
  1235. /*
  1236. * 1. If we have a packet, encapsulate it and put it in the buffer
  1237. */
  1238. if (skb) {
  1239. char *newptr = strip_make_packet(ptr, strip_info, skb);
  1240. strip_info->tx_pps_count++;
  1241. if (!newptr)
  1242. strip_info->tx_dropped++;
  1243. else {
  1244. ptr = newptr;
  1245. strip_info->sx_pps_count++;
  1246. strip_info->tx_packets++; /* Count another successful packet */
  1247. #ifdef EXT_COUNTERS
  1248. strip_info->tx_bytes += skb->len;
  1249. strip_info->tx_rbytes += ptr - strip_info->tx_buff;
  1250. #endif
  1251. /*DumpData("Sending:", strip_info, strip_info->tx_buff, ptr); */
  1252. /*HexDump("Sending", strip_info, strip_info->tx_buff, ptr); */
  1253. }
  1254. }
  1255. /*
  1256. * 2. If it is time for another tickle, tack it on, after the packet
  1257. */
  1258. if (doprobe) {
  1259. StringDescriptor ts = CommandString[strip_info->next_command];
  1260. #if TICKLE_TIMERS
  1261. {
  1262. struct timeval tv;
  1263. do_gettimeofday(&tv);
  1264. printk(KERN_INFO "**** Sending tickle string %d at %02d.%06d\n",
  1265. strip_info->next_command, tv.tv_sec % 100,
  1266. tv.tv_usec);
  1267. }
  1268. #endif
  1269. if (ptr == strip_info->tx_buff)
  1270. *ptr++ = 0x0D;
  1271. *ptr++ = '*'; /* First send "**" to provoke an error message */
  1272. *ptr++ = '*';
  1273. /* Then add the command */
  1274. memcpy(ptr, ts.string, ts.length);
  1275. /* Add a checksum ? */
  1276. if (strip_info->firmware_level < ChecksummedMessages)
  1277. ptr += ts.length;
  1278. else
  1279. ptr = add_checksum(ptr, ptr + ts.length);
  1280. *ptr++ = 0x0D; /* Terminate the command with a <CR> */
  1281. /* Cycle to next periodic command? */
  1282. if (strip_info->firmware_level >= StructuredMessages)
  1283. if (++strip_info->next_command >=
  1284. ARRAY_SIZE(CommandString))
  1285. strip_info->next_command = 0;
  1286. #ifdef EXT_COUNTERS
  1287. strip_info->tx_ebytes += ts.length;
  1288. #endif
  1289. strip_info->watchdog_doprobe = jiffies + 10 * HZ;
  1290. strip_info->watchdog_doreset = jiffies + 1 * HZ;
  1291. /*printk(KERN_INFO "%s: Routine radio test.\n", strip_info->dev->name); */
  1292. }
  1293. /*
  1294. * 3. Set up the strip_info ready to send the data (if any).
  1295. */
  1296. strip_info->tx_head = strip_info->tx_buff;
  1297. strip_info->tx_left = ptr - strip_info->tx_buff;
  1298. set_bit(TTY_DO_WRITE_WAKEUP, &strip_info->tty->flags);
  1299. /*
  1300. * 4. Debugging check to make sure we're not overflowing the buffer.
  1301. */
  1302. if (strip_info->tx_size - strip_info->tx_left < 20)
  1303. printk(KERN_ERR "%s: Sending%5d bytes;%5d bytes free.\n",
  1304. strip_info->dev->name, strip_info->tx_left,
  1305. strip_info->tx_size - strip_info->tx_left);
  1306. /*
  1307. * 5. If watchdog has expired, reset the radio. Note: if there's data waiting in
  1308. * the buffer, strip_write_some_more will send it after the reset has finished
  1309. */
  1310. if (doreset) {
  1311. ResetRadio(strip_info);
  1312. return;
  1313. }
  1314. if (1) {
  1315. struct in_device *in_dev;
  1316. brd = addr = 0;
  1317. rcu_read_lock();
  1318. in_dev = __in_dev_get_rcu(strip_info->dev);
  1319. if (in_dev) {
  1320. if (in_dev->ifa_list) {
  1321. brd = in_dev->ifa_list->ifa_broadcast;
  1322. addr = in_dev->ifa_list->ifa_local;
  1323. }
  1324. }
  1325. rcu_read_unlock();
  1326. }
  1327. /*
  1328. * 6. If it is time for a periodic ARP, queue one up to be sent.
  1329. * We only do this if:
  1330. * 1. The radio is working
  1331. * 2. It's time to send another periodic ARP
  1332. * 3. We really know what our address is (and it is not manually set to zero)
  1333. * 4. We have a designated broadcast address configured
  1334. * If we queue up an ARP packet when we don't have a designated broadcast
  1335. * address configured, then the packet will just have to be discarded in
  1336. * strip_make_packet. This is not fatal, but it causes misleading information
  1337. * to be displayed in tcpdump. tcpdump will report that periodic APRs are
  1338. * being sent, when in fact they are not, because they are all being dropped
  1339. * in the strip_make_packet routine.
  1340. */
  1341. if (strip_info->working
  1342. && (long) jiffies - strip_info->gratuitous_arp >= 0
  1343. && memcmp(strip_info->dev->dev_addr, zero_address.c,
  1344. sizeof(zero_address))
  1345. && arp_query(haddr.c, brd, strip_info->dev)) {
  1346. /*printk(KERN_INFO "%s: Sending gratuitous ARP with interval %ld\n",
  1347. strip_info->dev->name, strip_info->arp_interval / HZ); */
  1348. strip_info->gratuitous_arp =
  1349. jiffies + strip_info->arp_interval;
  1350. strip_info->arp_interval *= 2;
  1351. if (strip_info->arp_interval > MaxARPInterval)
  1352. strip_info->arp_interval = MaxARPInterval;
  1353. if (addr)
  1354. arp_send(ARPOP_REPLY, ETH_P_ARP, addr, /* Target address of ARP packet is our address */
  1355. strip_info->dev, /* Device to send packet on */
  1356. addr, /* Source IP address this ARP packet comes from */
  1357. NULL, /* Destination HW address is NULL (broadcast it) */
  1358. strip_info->dev->dev_addr, /* Source HW address is our HW address */
  1359. strip_info->dev->dev_addr); /* Target HW address is our HW address (redundant) */
  1360. }
  1361. /*
  1362. * 7. All ready. Start the transmission
  1363. */
  1364. strip_write_some_more(strip_info->tty);
  1365. }
  1366. /* Encapsulate a datagram and kick it into a TTY queue. */
  1367. static int strip_xmit(struct sk_buff *skb, struct net_device *dev)
  1368. {
  1369. struct strip *strip_info = netdev_priv(dev);
  1370. unsigned long flags;
  1371. if (!netif_running(dev)) {
  1372. printk(KERN_ERR "%s: xmit call when iface is down\n",
  1373. dev->name);
  1374. return NETDEV_TX_BUSY;
  1375. }
  1376. netif_stop_queue(dev);
  1377. del_timer(&strip_info->idle_timer);
  1378. if (time_after(jiffies, strip_info->pps_timer + HZ)) {
  1379. unsigned long t = jiffies - strip_info->pps_timer;
  1380. unsigned long rx_pps_count = (strip_info->rx_pps_count * HZ * 8 + t / 2) / t;
  1381. unsigned long tx_pps_count = (strip_info->tx_pps_count * HZ * 8 + t / 2) / t;
  1382. unsigned long sx_pps_count = (strip_info->sx_pps_count * HZ * 8 + t / 2) / t;
  1383. strip_info->pps_timer = jiffies;
  1384. strip_info->rx_pps_count = 0;
  1385. strip_info->tx_pps_count = 0;
  1386. strip_info->sx_pps_count = 0;
  1387. strip_info->rx_average_pps = (strip_info->rx_average_pps + rx_pps_count + 1) / 2;
  1388. strip_info->tx_average_pps = (strip_info->tx_average_pps + tx_pps_count + 1) / 2;
  1389. strip_info->sx_average_pps = (strip_info->sx_average_pps + sx_pps_count + 1) / 2;
  1390. if (rx_pps_count / 8 >= 10)
  1391. printk(KERN_INFO "%s: WARNING: Receiving %ld packets per second.\n",
  1392. strip_info->dev->name, rx_pps_count / 8);
  1393. if (tx_pps_count / 8 >= 10)
  1394. printk(KERN_INFO "%s: WARNING: Tx %ld packets per second.\n",
  1395. strip_info->dev->name, tx_pps_count / 8);
  1396. if (sx_pps_count / 8 >= 10)
  1397. printk(KERN_INFO "%s: WARNING: Sending %ld packets per second.\n",
  1398. strip_info->dev->name, sx_pps_count / 8);
  1399. }
  1400. spin_lock_irqsave(&strip_lock, flags);
  1401. strip_send(strip_info, skb);
  1402. spin_unlock_irqrestore(&strip_lock, flags);
  1403. if (skb)
  1404. dev_kfree_skb(skb);
  1405. return 0;
  1406. }
  1407. /*
  1408. * IdleTask periodically calls strip_xmit, so even when we have no IP packets
  1409. * to send for an extended period of time, the watchdog processing still gets
  1410. * done to ensure that the radio stays in Starmode
  1411. */
  1412. static void strip_IdleTask(unsigned long parameter)
  1413. {
  1414. strip_xmit(NULL, (struct net_device *) parameter);
  1415. }
  1416. /*
  1417. * Create the MAC header for an arbitrary protocol layer
  1418. *
  1419. * saddr!=NULL means use this specific address (n/a for Metricom)
  1420. * saddr==NULL means use default device source address
  1421. * daddr!=NULL means use this destination address
  1422. * daddr==NULL means leave destination address alone
  1423. * (e.g. unresolved arp -- kernel will call
  1424. * rebuild_header later to fill in the address)
  1425. */
  1426. static int strip_header(struct sk_buff *skb, struct net_device *dev,
  1427. unsigned short type, const void *daddr,
  1428. const void *saddr, unsigned len)
  1429. {
  1430. struct strip *strip_info = netdev_priv(dev);
  1431. STRIP_Header *header = (STRIP_Header *) skb_push(skb, sizeof(STRIP_Header));
  1432. /*printk(KERN_INFO "%s: strip_header 0x%04X %s\n", dev->name, type,
  1433. type == ETH_P_IP ? "IP" : type == ETH_P_ARP ? "ARP" : ""); */
  1434. header->src_addr = strip_info->true_dev_addr;
  1435. header->protocol = htons(type);
  1436. /*HexDump("strip_header", netdev_priv(dev), skb->data, skb->data + skb->len); */
  1437. if (!daddr)
  1438. return (-dev->hard_header_len);
  1439. header->dst_addr = *(MetricomAddress *) daddr;
  1440. return (dev->hard_header_len);
  1441. }
  1442. /*
  1443. * Rebuild the MAC header. This is called after an ARP
  1444. * (or in future other address resolution) has completed on this
  1445. * sk_buff. We now let ARP fill in the other fields.
  1446. * I think this should return zero if packet is ready to send,
  1447. * or non-zero if it needs more time to do an address lookup
  1448. */
  1449. static int strip_rebuild_header(struct sk_buff *skb)
  1450. {
  1451. #ifdef CONFIG_INET
  1452. STRIP_Header *header = (STRIP_Header *) skb->data;
  1453. /* Arp find returns zero if if knows the address, */
  1454. /* or if it doesn't know the address it sends an ARP packet and returns non-zero */
  1455. return arp_find(header->dst_addr.c, skb) ? 1 : 0;
  1456. #else
  1457. return 0;
  1458. #endif
  1459. }
  1460. /************************************************************************/
  1461. /* Receiving routines */
  1462. /*
  1463. * This function parses the response to the ATS300? command,
  1464. * extracting the radio version and serial number.
  1465. */
  1466. static void get_radio_version(struct strip *strip_info, __u8 * ptr, __u8 * end)
  1467. {
  1468. __u8 *p, *value_begin, *value_end;
  1469. int len;
  1470. /* Determine the beginning of the second line of the payload */
  1471. p = ptr;
  1472. while (p < end && *p != 10)
  1473. p++;
  1474. if (p >= end)
  1475. return;
  1476. p++;
  1477. value_begin = p;
  1478. /* Determine the end of line */
  1479. while (p < end && *p != 10)
  1480. p++;
  1481. if (p >= end)
  1482. return;
  1483. value_end = p;
  1484. p++;
  1485. len = value_end - value_begin;
  1486. len = min_t(int, len, sizeof(FirmwareVersion) - 1);
  1487. if (strip_info->firmware_version.c[0] == 0)
  1488. printk(KERN_INFO "%s: Radio Firmware: %.*s\n",
  1489. strip_info->dev->name, len, value_begin);
  1490. sprintf(strip_info->firmware_version.c, "%.*s", len, value_begin);
  1491. /* Look for the first colon */
  1492. while (p < end && *p != ':')
  1493. p++;
  1494. if (p >= end)
  1495. return;
  1496. /* Skip over the space */
  1497. p += 2;
  1498. len = sizeof(SerialNumber) - 1;
  1499. if (p + len <= end) {
  1500. sprintf(strip_info->serial_number.c, "%.*s", len, p);
  1501. } else {
  1502. printk(KERN_DEBUG
  1503. "STRIP: radio serial number shorter (%zd) than expected (%d)\n",
  1504. end - p, len);
  1505. }
  1506. }
  1507. /*
  1508. * This function parses the response to the ATS325? command,
  1509. * extracting the radio battery voltage.
  1510. */
  1511. static void get_radio_voltage(struct strip *strip_info, __u8 * ptr, __u8 * end)
  1512. {
  1513. int len;
  1514. len = sizeof(BatteryVoltage) - 1;
  1515. if (ptr + len <= end) {
  1516. sprintf(strip_info->battery_voltage.c, "%.*s", len, ptr);
  1517. } else {
  1518. printk(KERN_DEBUG
  1519. "STRIP: radio voltage string shorter (%zd) than expected (%d)\n",
  1520. end - ptr, len);
  1521. }
  1522. }
  1523. /*
  1524. * This function parses the responses to the AT~LA and ATS311 commands,
  1525. * which list the radio's neighbours.
  1526. */
  1527. static void get_radio_neighbours(MetricomNodeTable * table, __u8 * ptr, __u8 * end)
  1528. {
  1529. table->num_nodes = 0;
  1530. while (ptr < end && table->num_nodes < NODE_TABLE_SIZE) {
  1531. MetricomNode *node = &table->node[table->num_nodes++];
  1532. char *dst = node->c, *limit = dst + sizeof(*node) - 1;
  1533. while (ptr < end && *ptr <= 32)
  1534. ptr++;
  1535. while (ptr < end && dst < limit && *ptr != 10)
  1536. *dst++ = *ptr++;
  1537. *dst++ = 0;
  1538. while (ptr < end && ptr[-1] != 10)
  1539. ptr++;
  1540. }
  1541. do_gettimeofday(&table->timestamp);
  1542. }
  1543. static int get_radio_address(struct strip *strip_info, __u8 * p)
  1544. {
  1545. MetricomAddress addr;
  1546. if (string_to_radio_address(&addr, p))
  1547. return (1);
  1548. /* See if our radio address has changed */
  1549. if (memcmp(strip_info->true_dev_addr.c, addr.c, sizeof(addr))) {
  1550. MetricomAddressString addr_string;
  1551. radio_address_to_string(&addr, &addr_string);
  1552. printk(KERN_INFO "%s: Radio address = %s\n",
  1553. strip_info->dev->name, addr_string.c);
  1554. strip_info->true_dev_addr = addr;
  1555. if (!strip_info->manual_dev_addr)
  1556. *(MetricomAddress *) strip_info->dev->dev_addr =
  1557. addr;
  1558. /* Give the radio a few seconds to get its head straight, then send an arp */
  1559. strip_info->gratuitous_arp = jiffies + 15 * HZ;
  1560. strip_info->arp_interval = 1 * HZ;
  1561. }
  1562. return (0);
  1563. }
  1564. static int verify_checksum(struct strip *strip_info)
  1565. {
  1566. __u8 *p = strip_info->sx_buff;
  1567. __u8 *end = strip_info->sx_buff + strip_info->sx_count - 4;
  1568. u_short sum =
  1569. (READHEX16(end[0]) << 12) | (READHEX16(end[1]) << 8) |
  1570. (READHEX16(end[2]) << 4) | (READHEX16(end[3]));
  1571. while (p < end)
  1572. sum -= *p++;
  1573. if (sum == 0 && strip_info->firmware_level == StructuredMessages) {
  1574. strip_info->firmware_level = ChecksummedMessages;
  1575. printk(KERN_INFO "%s: Radio provides message checksums\n",
  1576. strip_info->dev->name);
  1577. }
  1578. return (sum == 0);
  1579. }
  1580. static void RecvErr(char *msg, struct strip *strip_info)
  1581. {
  1582. __u8 *ptr = strip_info->sx_buff;
  1583. __u8 *end = strip_info->sx_buff + strip_info->sx_count;
  1584. DumpData(msg, strip_info, ptr, end);
  1585. strip_info->rx_errors++;
  1586. }
  1587. static void RecvErr_Message(struct strip *strip_info, __u8 * sendername,
  1588. const __u8 * msg, u_long len)
  1589. {
  1590. if (has_prefix(msg, len, "001")) { /* Not in StarMode! */
  1591. RecvErr("Error Msg:", strip_info);
  1592. printk(KERN_INFO "%s: Radio %s is not in StarMode\n",
  1593. strip_info->dev->name, sendername);
  1594. }
  1595. else if (has_prefix(msg, len, "002")) { /* Remap handle */
  1596. /* We ignore "Remap handle" messages for now */
  1597. }
  1598. else if (has_prefix(msg, len, "003")) { /* Can't resolve name */
  1599. RecvErr("Error Msg:", strip_info);
  1600. printk(KERN_INFO "%s: Destination radio name is unknown\n",
  1601. strip_info->dev->name);
  1602. }
  1603. else if (has_prefix(msg, len, "004")) { /* Name too small or missing */
  1604. strip_info->watchdog_doreset = jiffies + LongTime;
  1605. #if TICKLE_TIMERS
  1606. {
  1607. struct timeval tv;
  1608. do_gettimeofday(&tv);
  1609. printk(KERN_INFO
  1610. "**** Got ERR_004 response at %02d.%06d\n",
  1611. tv.tv_sec % 100, tv.tv_usec);
  1612. }
  1613. #endif
  1614. if (!strip_info->working) {
  1615. strip_info->working = TRUE;
  1616. printk(KERN_INFO "%s: Radio now in starmode\n",
  1617. strip_info->dev->name);
  1618. /*
  1619. * If the radio has just entered a working state, we should do our first
  1620. * probe ASAP, so that we find out our radio address etc. without delay.
  1621. */
  1622. strip_info->watchdog_doprobe = jiffies;
  1623. }
  1624. if (strip_info->firmware_level == NoStructure && sendername) {
  1625. strip_info->firmware_level = StructuredMessages;
  1626. strip_info->next_command = 0; /* Try to enable checksums ASAP */
  1627. printk(KERN_INFO
  1628. "%s: Radio provides structured messages\n",
  1629. strip_info->dev->name);
  1630. }
  1631. if (strip_info->firmware_level >= StructuredMessages) {
  1632. /*
  1633. * If this message has a valid checksum on the end, then the call to verify_checksum
  1634. * will elevate the firmware_level to ChecksummedMessages for us. (The actual return
  1635. * code from verify_checksum is ignored here.)
  1636. */
  1637. verify_checksum(strip_info);
  1638. /*
  1639. * If the radio has structured messages but we don't yet have all our information about it,
  1640. * we should do probes without delay, until we have gathered all the information
  1641. */
  1642. if (!GOT_ALL_RADIO_INFO(strip_info))
  1643. strip_info->watchdog_doprobe = jiffies;
  1644. }
  1645. }
  1646. else if (has_prefix(msg, len, "005")) /* Bad count specification */
  1647. RecvErr("Error Msg:", strip_info);
  1648. else if (has_prefix(msg, len, "006")) /* Header too big */
  1649. RecvErr("Error Msg:", strip_info);
  1650. else if (has_prefix(msg, len, "007")) { /* Body too big */
  1651. RecvErr("Error Msg:", strip_info);
  1652. printk(KERN_ERR
  1653. "%s: Error! Packet size too big for radio.\n",
  1654. strip_info->dev->name);
  1655. }
  1656. else if (has_prefix(msg, len, "008")) { /* Bad character in name */
  1657. RecvErr("Error Msg:", strip_info);
  1658. printk(KERN_ERR
  1659. "%s: Radio name contains illegal character\n",
  1660. strip_info->dev->name);
  1661. }
  1662. else if (has_prefix(msg, len, "009")) /* No count or line terminator */
  1663. RecvErr("Error Msg:", strip_info);
  1664. else if (has_prefix(msg, len, "010")) /* Invalid checksum */
  1665. RecvErr("Error Msg:", strip_info);
  1666. else if (has_prefix(msg, len, "011")) /* Checksum didn't match */
  1667. RecvErr("Error Msg:", strip_info);
  1668. else if (has_prefix(msg, len, "012")) /* Failed to transmit packet */
  1669. RecvErr("Error Msg:", strip_info);
  1670. else
  1671. RecvErr("Error Msg:", strip_info);
  1672. }
  1673. static void process_AT_response(struct strip *strip_info, __u8 * ptr,
  1674. __u8 * end)
  1675. {
  1676. u_long len;
  1677. __u8 *p = ptr;
  1678. while (p < end && p[-1] != 10)
  1679. p++; /* Skip past first newline character */
  1680. /* Now ptr points to the AT command, and p points to the text of the response. */
  1681. len = p - ptr;
  1682. #if TICKLE_TIMERS
  1683. {
  1684. struct timeval tv;
  1685. do_gettimeofday(&tv);
  1686. printk(KERN_INFO "**** Got AT response %.7s at %02d.%06d\n",
  1687. ptr, tv.tv_sec % 100, tv.tv_usec);
  1688. }
  1689. #endif
  1690. if (has_prefix(ptr, len, "ATS300?"))
  1691. get_radio_version(strip_info, p, end);
  1692. else if (has_prefix(ptr, len, "ATS305?"))
  1693. get_radio_address(strip_info, p);
  1694. else if (has_prefix(ptr, len, "ATS311?"))
  1695. get_radio_neighbours(&strip_info->poletops, p, end);
  1696. else if (has_prefix(ptr, len, "ATS319=7"))
  1697. verify_checksum(strip_info);
  1698. else if (has_prefix(ptr, len, "ATS325?"))
  1699. get_radio_voltage(strip_info, p, end);
  1700. else if (has_prefix(ptr, len, "AT~LA"))
  1701. get_radio_neighbours(&strip_info->portables, p, end);
  1702. else
  1703. RecvErr("Unknown AT Response:", strip_info);
  1704. }
  1705. static void process_ACK(struct strip *strip_info, __u8 * ptr, __u8 * end)
  1706. {
  1707. /* Currently we don't do anything with ACKs from the radio */
  1708. }
  1709. static void process_Info(struct strip *strip_info, __u8 * ptr, __u8 * end)
  1710. {
  1711. if (ptr + 16 > end)
  1712. RecvErr("Bad Info Msg:", strip_info);
  1713. }
  1714. static struct net_device *get_strip_dev(struct strip *strip_info)
  1715. {
  1716. /* If our hardware address is *manually set* to zero, and we know our */
  1717. /* real radio hardware address, try to find another strip device that has been */
  1718. /* manually set to that address that we can 'transfer ownership' of this packet to */
  1719. if (strip_info->manual_dev_addr &&
  1720. !memcmp(strip_info->dev->dev_addr, zero_address.c,
  1721. sizeof(zero_address))
  1722. && memcmp(&strip_info->true_dev_addr, zero_address.c,
  1723. sizeof(zero_address))) {
  1724. struct net_device *dev;
  1725. read_lock_bh(&dev_base_lock);
  1726. for_each_netdev(&init_net, dev) {
  1727. if (dev->type == strip_info->dev->type &&
  1728. !memcmp(dev->dev_addr,
  1729. &strip_info->true_dev_addr,
  1730. sizeof(MetricomAddress))) {
  1731. printk(KERN_INFO
  1732. "%s: Transferred packet ownership to %s.\n",
  1733. strip_info->dev->name, dev->name);
  1734. read_unlock_bh(&dev_base_lock);
  1735. return (dev);
  1736. }
  1737. }
  1738. read_unlock_bh(&dev_base_lock);
  1739. }
  1740. return (strip_info->dev);
  1741. }
  1742. /*
  1743. * Send one completely decapsulated datagram to the next layer.
  1744. */
  1745. static void deliver_packet(struct strip *strip_info, STRIP_Header * header,
  1746. __u16 packetlen)
  1747. {
  1748. struct sk_buff *skb = dev_alloc_skb(sizeof(STRIP_Header) + packetlen);
  1749. if (!skb) {
  1750. printk(KERN_ERR "%s: memory squeeze, dropping packet.\n",
  1751. strip_info->dev->name);
  1752. strip_info->rx_dropped++;
  1753. } else {
  1754. memcpy(skb_put(skb, sizeof(STRIP_Header)), header,
  1755. sizeof(STRIP_Header));
  1756. memcpy(skb_put(skb, packetlen), strip_info->rx_buff,
  1757. packetlen);
  1758. skb->dev = get_strip_dev(strip_info);
  1759. skb->protocol = header->protocol;
  1760. skb_reset_mac_header(skb);
  1761. /* Having put a fake header on the front of the sk_buff for the */
  1762. /* benefit of tools like tcpdump, skb_pull now 'consumes' that */
  1763. /* fake header before we hand the packet up to the next layer. */
  1764. skb_pull(skb, sizeof(STRIP_Header));
  1765. /* Finally, hand the packet up to the next layer (e.g. IP or ARP, etc.) */
  1766. strip_info->rx_packets++;
  1767. strip_info->rx_pps_count++;
  1768. #ifdef EXT_COUNTERS
  1769. strip_info->rx_bytes += packetlen;
  1770. #endif
  1771. netif_rx(skb);
  1772. }
  1773. }
  1774. static void process_IP_packet(struct strip *strip_info,
  1775. STRIP_Header * header, __u8 * ptr,
  1776. __u8 * end)
  1777. {
  1778. __u16 packetlen;
  1779. /* Decode start of the IP packet header */
  1780. ptr = UnStuffData(ptr, end, strip_info->rx_buff, 4);
  1781. if (!ptr) {
  1782. RecvErr("IP Packet too short", strip_info);
  1783. return;
  1784. }
  1785. packetlen = ((__u16) strip_info->rx_buff[2] << 8) | strip_info->rx_buff[3];
  1786. if (packetlen > MAX_RECV_MTU) {
  1787. printk(KERN_INFO "%s: Dropping oversized received IP packet: %d bytes\n",
  1788. strip_info->dev->name, packetlen);
  1789. strip_info->rx_dropped++;
  1790. return;
  1791. }
  1792. /*printk(KERN_INFO "%s: Got %d byte IP packet\n", strip_info->dev->name, packetlen); */
  1793. /* Decode remainder of the IP packet */
  1794. ptr =
  1795. UnStuffData(ptr, end, strip_info->rx_buff + 4, packetlen - 4);
  1796. if (!ptr) {
  1797. RecvErr("IP Packet too short", strip_info);
  1798. return;
  1799. }
  1800. if (ptr < end) {
  1801. RecvErr("IP Packet too long", strip_info);
  1802. return;
  1803. }
  1804. header->protocol = htons(ETH_P_IP);
  1805. deliver_packet(strip_info, header, packetlen);
  1806. }
  1807. static void process_ARP_packet(struct strip *strip_info,
  1808. STRIP_Header * header, __u8 * ptr,
  1809. __u8 * end)
  1810. {
  1811. __u16 packetlen;
  1812. struct arphdr *arphdr = (struct arphdr *) strip_info->rx_buff;
  1813. /* Decode start of the ARP packet */
  1814. ptr = UnStuffData(ptr, end, strip_info->rx_buff, 8);
  1815. if (!ptr) {
  1816. RecvErr("ARP Packet too short", strip_info);
  1817. return;
  1818. }
  1819. packetlen = 8 + (arphdr->ar_hln + arphdr->ar_pln) * 2;
  1820. if (packetlen > MAX_RECV_MTU) {
  1821. printk(KERN_INFO
  1822. "%s: Dropping oversized received ARP packet: %d bytes\n",
  1823. strip_info->dev->name, packetlen);
  1824. strip_info->rx_dropped++;
  1825. return;
  1826. }
  1827. /*printk(KERN_INFO "%s: Got %d byte ARP %s\n",
  1828. strip_info->dev->name, packetlen,
  1829. ntohs(arphdr->ar_op) == ARPOP_REQUEST ? "request" : "reply"); */
  1830. /* Decode remainder of the ARP packet */
  1831. ptr =
  1832. UnStuffData(ptr, end, strip_info->rx_buff + 8, packetlen - 8);
  1833. if (!ptr) {
  1834. RecvErr("ARP Packet too short", strip_info);
  1835. return;
  1836. }
  1837. if (ptr < end) {
  1838. RecvErr("ARP Packet too long", strip_info);
  1839. return;
  1840. }
  1841. header->protocol = htons(ETH_P_ARP);
  1842. deliver_packet(strip_info, header, packetlen);
  1843. }
  1844. /*
  1845. * process_text_message processes a <CR>-terminated block of data received
  1846. * from the radio that doesn't begin with a '*' character. All normal
  1847. * Starmode communication messages with the radio begin with a '*',
  1848. * so any text that does not indicates a serial port error, a radio that
  1849. * is in Hayes command mode instead of Starmode, or a radio with really
  1850. * old firmware that doesn't frame its Starmode responses properly.
  1851. */
  1852. static void process_text_message(struct strip *strip_info)
  1853. {
  1854. __u8 *msg = strip_info->sx_buff;
  1855. int len = strip_info->sx_count;
  1856. /* Check for anything that looks like it might be our radio name */
  1857. /* (This is here for backwards compatibility with old firmware) */
  1858. if (len == 9 && get_radio_address(strip_info, msg) == 0)
  1859. return;
  1860. if (text_equal(msg, len, "OK"))
  1861. return; /* Ignore 'OK' responses from prior commands */
  1862. if (text_equal(msg, len, "ERROR"))
  1863. return; /* Ignore 'ERROR' messages */
  1864. if (has_prefix(msg, len, "ate0q1"))
  1865. return; /* Ignore character echo back from the radio */
  1866. /* Catch other error messages */
  1867. /* (This is here for backwards compatibility with old firmware) */
  1868. if (has_prefix(msg, len, "ERR_")) {
  1869. RecvErr_Message(strip_info, NULL, &msg[4], len - 4);
  1870. return;
  1871. }
  1872. RecvErr("No initial *", strip_info);
  1873. }
  1874. /*
  1875. * process_message processes a <CR>-terminated block of data received
  1876. * from the radio. If the radio is not in Starmode or has old firmware,
  1877. * it may be a line of text in response to an AT command. Ideally, with
  1878. * a current radio that's properly in Starmode, all data received should
  1879. * be properly framed and checksummed radio message blocks, containing
  1880. * either a starmode packet, or a other communication from the radio
  1881. * firmware, like "INF_" Info messages and &COMMAND responses.
  1882. */
  1883. static void process_message(struct strip *strip_info)
  1884. {
  1885. STRIP_Header header = { zero_address, zero_address, 0 };
  1886. __u8 *ptr = strip_info->sx_buff;
  1887. __u8 *end = strip_info->sx_buff + strip_info->sx_count;
  1888. __u8 sendername[32], *sptr = sendername;
  1889. MetricomKey key;
  1890. /*HexDump("Receiving", strip_info, ptr, end); */
  1891. /* Check for start of address marker, and then skip over it */
  1892. if (*ptr == '*')
  1893. ptr++;
  1894. else {
  1895. process_text_message(strip_info);
  1896. return;
  1897. }
  1898. /* Copy out the return address */
  1899. while (ptr < end && *ptr != '*'
  1900. && sptr < ARRAY_END(sendername) - 1)
  1901. *sptr++ = *ptr++;
  1902. *sptr = 0; /* Null terminate the sender name */
  1903. /* Check for end of address marker, and skip over it */
  1904. if (ptr >= end || *ptr != '*') {
  1905. RecvErr("No second *", strip_info);
  1906. return;
  1907. }
  1908. ptr++; /* Skip the second '*' */
  1909. /* If the sender name is "&COMMAND", ignore this 'packet' */
  1910. /* (This is here for backwards compatibility with old firmware) */
  1911. if (!strcmp(sendername, "&COMMAND")) {
  1912. strip_info->firmware_level = NoStructure;
  1913. strip_info->next_command = CompatibilityCommand;
  1914. return;
  1915. }
  1916. if (ptr + 4 > end) {
  1917. RecvErr("No proto key", strip_info);
  1918. return;
  1919. }
  1920. /* Get the protocol key out of the buffer */
  1921. key.c[0] = *ptr++;
  1922. key.c[1] = *ptr++;
  1923. key.c[2] = *ptr++;
  1924. key.c[3] = *ptr++;
  1925. /* If we're using checksums, verify the checksum at the end of the packet */
  1926. if (strip_info->firmware_level >= ChecksummedMessages) {
  1927. end -= 4; /* Chop the last four bytes off the packet (they're the checksum) */
  1928. if (ptr > end) {
  1929. RecvErr("Missing Checksum", strip_info);
  1930. return;
  1931. }
  1932. if (!verify_checksum(strip_info)) {
  1933. RecvErr("Bad Checksum", strip_info);
  1934. return;
  1935. }
  1936. }
  1937. /*printk(KERN_INFO "%s: Got packet from \"%s\".\n", strip_info->dev->name, sendername); */
  1938. /*
  1939. * Fill in (pseudo) source and destination addresses in the packet.
  1940. * We assume that the destination address was our address (the radio does not
  1941. * tell us this). If the radio supplies a source address, then we use it.
  1942. */
  1943. header.dst_addr = strip_info->true_dev_addr;
  1944. string_to_radio_address(&header.src_addr, sendername);
  1945. #ifdef EXT_COUNTERS
  1946. if (key.l == SIP0Key.l) {
  1947. strip_info->rx_rbytes += (end - ptr);
  1948. process_IP_packet(strip_info, &header, ptr, end);
  1949. } else if (key.l == ARP0Key.l) {
  1950. strip_info->rx_rbytes += (end - ptr);
  1951. process_ARP_packet(strip_info, &header, ptr, end);
  1952. } else if (key.l == ATR_Key.l) {
  1953. strip_info->rx_ebytes += (end - ptr);
  1954. process_AT_response(strip_info, ptr, end);
  1955. } else if (key.l == ACK_Key.l) {
  1956. strip_info->rx_ebytes += (end - ptr);
  1957. process_ACK(strip_info, ptr, end);
  1958. } else if (key.l == INF_Key.l) {
  1959. strip_info->rx_ebytes += (end - ptr);
  1960. process_Info(strip_info, ptr, end);
  1961. } else if (key.l == ERR_Key.l) {
  1962. strip_info->rx_ebytes += (end - ptr);
  1963. RecvErr_Message(strip_info, sendername, ptr, end - ptr);
  1964. } else
  1965. RecvErr("Unrecognized protocol key", strip_info);
  1966. #else
  1967. if (key.l == SIP0Key.l)
  1968. process_IP_packet(strip_info, &header, ptr, end);
  1969. else if (key.l == ARP0Key.l)
  1970. process_ARP_packet(strip_info, &header, ptr, end);
  1971. else if (key.l == ATR_Key.l)
  1972. process_AT_response(strip_info, ptr, end);
  1973. else if (key.l == ACK_Key.l)
  1974. process_ACK(strip_info, ptr, end);
  1975. else if (key.l == INF_Key.l)
  1976. process_Info(strip_info, ptr, end);
  1977. else if (key.l == ERR_Key.l)
  1978. RecvErr_Message(strip_info, sendername, ptr, end - ptr);
  1979. else
  1980. RecvErr("Unrecognized protocol key", strip_info);
  1981. #endif
  1982. }
  1983. #define TTYERROR(X) ((X) == TTY_BREAK ? "Break" : \
  1984. (X) == TTY_FRAME ? "Framing Error" : \
  1985. (X) == TTY_PARITY ? "Parity Error" : \
  1986. (X) == TTY_OVERRUN ? "Hardware Overrun" : "Unknown Error")
  1987. /*
  1988. * Handle the 'receiver data ready' interrupt.
  1989. * This function is called by the 'tty_io' module in the kernel when
  1990. * a block of STRIP data has been received, which can now be decapsulated
  1991. * and sent on to some IP layer for further processing.
  1992. */
  1993. static void strip_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  1994. char *fp, int count)
  1995. {
  1996. struct strip *strip_info = tty->disc_data;
  1997. const unsigned char *end = cp + count;
  1998. unsigned long flags;
  1999. if (!strip_info || strip_info->magic != STRIP_MAGIC
  2000. || !netif_running(strip_info->dev))
  2001. return;
  2002. spin_lock_irqsave(&strip_lock, flags);
  2003. #if 0
  2004. {
  2005. struct timeval tv;
  2006. do_gettimeofday(&tv);
  2007. printk(KERN_INFO
  2008. "**** strip_receive_buf: %3d bytes at %02d.%06d\n",
  2009. count, tv.tv_sec % 100, tv.tv_usec);
  2010. }
  2011. #endif
  2012. #ifdef EXT_COUNTERS
  2013. strip_info->rx_sbytes += count;
  2014. #endif
  2015. /* Read the characters out of the buffer */
  2016. while (cp < end) {
  2017. if (fp && *fp)
  2018. printk(KERN_INFO "%s: %s on serial port\n",
  2019. strip_info->dev->name, TTYERROR(*fp));
  2020. if (fp && *fp++ && !strip_info->discard) { /* If there's a serial error, record it */
  2021. /* If we have some characters in the buffer, discard them */
  2022. strip_info->discard = strip_info->sx_count;
  2023. strip_info->rx_errors++;
  2024. }
  2025. /* Leading control characters (CR, NL, Tab, etc.) are ignored */
  2026. if (strip_info->sx_count > 0 || *cp >= ' ') {
  2027. if (*cp == 0x0D) { /* If end of packet, decide what to do with it */
  2028. if (strip_info->sx_count > 3000)
  2029. printk(KERN_INFO
  2030. "%s: Cut a %d byte packet (%zd bytes remaining)%s\n",
  2031. strip_info->dev->name,
  2032. strip_info->sx_count,
  2033. end - cp - 1,
  2034. strip_info->
  2035. discard ? " (discarded)" :
  2036. "");
  2037. if (strip_info->sx_count >
  2038. strip_info->sx_size) {
  2039. strip_info->rx_over_errors++;
  2040. printk(KERN_INFO
  2041. "%s: sx_buff overflow (%d bytes total)\n",
  2042. strip_info->dev->name,
  2043. strip_info->sx_count);
  2044. } else if (strip_info->discard)
  2045. printk(KERN_INFO
  2046. "%s: Discarding bad packet (%d/%d)\n",
  2047. strip_info->dev->name,
  2048. strip_info->discard,
  2049. strip_info->sx_count);
  2050. else
  2051. process_message(strip_info);
  2052. strip_info->discard = 0;
  2053. strip_info->sx_count = 0;
  2054. } else {
  2055. /* Make sure we have space in the buffer */
  2056. if (strip_info->sx_count <
  2057. strip_info->sx_size)
  2058. strip_info->sx_buff[strip_info->
  2059. sx_count] =
  2060. *cp;
  2061. strip_info->sx_count++;
  2062. }
  2063. }
  2064. cp++;
  2065. }
  2066. spin_unlock_irqrestore(&strip_lock, flags);
  2067. }
  2068. /************************************************************************/
  2069. /* General control routines */
  2070. static int set_mac_address(struct strip *strip_info,
  2071. MetricomAddress * addr)
  2072. {
  2073. /*
  2074. * We're using a manually specified address if the address is set
  2075. * to anything other than all ones. Setting the address to all ones
  2076. * disables manual mode and goes back to automatic address determination
  2077. * (tracking the true address that the radio has).
  2078. */
  2079. strip_info->manual_dev_addr =
  2080. memcmp(addr->c, broadcast_address.c,
  2081. sizeof(broadcast_address));
  2082. if (strip_info->manual_dev_addr)
  2083. *(MetricomAddress *) strip_info->dev->dev_addr = *addr;
  2084. else
  2085. *(MetricomAddress *) strip_info->dev->dev_addr =
  2086. strip_info->true_dev_addr;
  2087. return 0;
  2088. }
  2089. static int strip_set_mac_address(struct net_device *dev, void *addr)
  2090. {
  2091. struct strip *strip_info = netdev_priv(dev);
  2092. struct sockaddr *sa = addr;
  2093. printk(KERN_INFO "%s: strip_set_dev_mac_address called\n", dev->name);
  2094. set_mac_address(strip_info, (MetricomAddress *) sa->sa_data);
  2095. return 0;
  2096. }
  2097. static struct net_device_stats *strip_get_stats(struct net_device *dev)
  2098. {
  2099. struct strip *strip_info = netdev_priv(dev);
  2100. static struct net_device_stats stats;
  2101. memset(&stats, 0, sizeof(struct net_device_stats));
  2102. stats.rx_packets = strip_info->rx_packets;
  2103. stats.tx_packets = strip_info->tx_packets;
  2104. stats.rx_dropped = strip_info->rx_dropped;
  2105. stats.tx_dropped = strip_info->tx_dropped;
  2106. stats.tx_errors = strip_info->tx_errors;
  2107. stats.rx_errors = strip_info->rx_errors;
  2108. stats.rx_over_errors = strip_info->rx_over_errors;
  2109. return (&stats);
  2110. }
  2111. /************************************************************************/
  2112. /* Opening and closing */
  2113. /*
  2114. * Here's the order things happen:
  2115. * When the user runs "slattach -p strip ..."
  2116. * 1. The TTY module calls strip_open;;
  2117. * 2. strip_open calls strip_alloc
  2118. * 3. strip_alloc calls register_netdev
  2119. * 4. register_netdev calls strip_dev_init
  2120. * 5. then strip_open finishes setting up the strip_info
  2121. *
  2122. * When the user runs "ifconfig st<x> up address netmask ..."
  2123. * 6. strip_open_low gets called
  2124. *
  2125. * When the user runs "ifconfig st<x> down"
  2126. * 7. strip_close_low gets called
  2127. *
  2128. * When the user kills the slattach process
  2129. * 8. strip_close gets called
  2130. * 9. strip_close calls dev_close
  2131. * 10. if the device is still up, then dev_close calls strip_close_low
  2132. * 11. strip_close calls strip_free
  2133. */
  2134. /* Open the low-level part of the STRIP channel. Easy! */
  2135. static int strip_open_low(struct net_device *dev)
  2136. {
  2137. struct strip *strip_info = netdev_priv(dev);
  2138. if (strip_info->tty == NULL)
  2139. return (-ENODEV);
  2140. if (!allocate_buffers(strip_info, dev->mtu))
  2141. return (-ENOMEM);
  2142. strip_info->sx_count = 0;
  2143. strip_info->tx_left = 0;
  2144. strip_info->discard = 0;
  2145. strip_info->working = FALSE;
  2146. strip_info->firmware_level = NoStructure;
  2147. strip_info->next_command = CompatibilityCommand;
  2148. strip_info->user_baud = tty_get_baud_rate(strip_info->tty);
  2149. printk(KERN_INFO "%s: Initializing Radio.\n",
  2150. strip_info->dev->name);
  2151. ResetRadio(strip_info);
  2152. strip_info->idle_timer.expires = jiffies + 1 * HZ;
  2153. add_timer(&strip_info->idle_timer);
  2154. netif_wake_queue(dev);
  2155. return (0);
  2156. }
  2157. /*
  2158. * Close the low-level part of the STRIP channel. Easy!
  2159. */
  2160. static int strip_close_low(struct net_device *dev)
  2161. {
  2162. struct strip *strip_info = netdev_priv(dev);
  2163. if (strip_info->tty == NULL)
  2164. return -EBUSY;
  2165. clear_bit(TTY_DO_WRITE_WAKEUP, &strip_info->tty->flags);
  2166. netif_stop_queue(dev);
  2167. /*
  2168. * Free all STRIP frame buffers.
  2169. */
  2170. kfree(strip_info->rx_buff);
  2171. strip_info->rx_buff = NULL;
  2172. kfree(strip_info->sx_buff);
  2173. strip_info->sx_buff = NULL;
  2174. kfree(strip_info->tx_buff);
  2175. strip_info->tx_buff = NULL;
  2176. del_timer(&strip_info->idle_timer);
  2177. return 0;
  2178. }
  2179. static const struct header_ops strip_header_ops = {
  2180. .create = strip_header,
  2181. .rebuild = strip_rebuild_header,
  2182. };
  2183. static const struct net_device_ops strip_netdev_ops = {
  2184. .ndo_open = strip_open_low,
  2185. .ndo_stop = strip_close_low,
  2186. .ndo_start_xmit = strip_xmit,
  2187. .ndo_set_mac_address = strip_set_mac_address,
  2188. .ndo_get_stats = strip_get_stats,
  2189. .ndo_change_mtu = strip_change_mtu,
  2190. };
  2191. /*
  2192. * This routine is called by DDI when the
  2193. * (dynamically assigned) device is registered
  2194. */
  2195. static void strip_dev_setup(struct net_device *dev)
  2196. {
  2197. /*
  2198. * Finish setting up the DEVICE info.
  2199. */
  2200. dev->trans_start = 0;
  2201. dev->tx_queue_len = 30; /* Drop after 30 frames queued */
  2202. dev->flags = 0;
  2203. dev->mtu = DEFAULT_STRIP_MTU;
  2204. dev->type = ARPHRD_METRICOM; /* dtang */
  2205. dev->hard_header_len = sizeof(STRIP_Header);
  2206. /*
  2207. * netdev_priv(dev) Already holds a pointer to our struct strip
  2208. */
  2209. *(MetricomAddress *)dev->broadcast = broadcast_address;
  2210. dev->dev_addr[0] = 0;
  2211. dev->addr_len = sizeof(MetricomAddress);
  2212. dev->header_ops = &strip_header_ops,
  2213. dev->netdev_ops = &strip_netdev_ops;
  2214. }
  2215. /*
  2216. * Free a STRIP channel.
  2217. */
  2218. static void strip_free(struct strip *strip_info)
  2219. {
  2220. unsigned long flags;
  2221. spin_lock_irqsave(&strip_lock, flags);
  2222. list_del_rcu(&strip_info->list);
  2223. spin_unlock_irqrestore(&strip_lock, flags);
  2224. strip_info->magic = 0;
  2225. free_netdev(strip_info->dev);
  2226. }
  2227. /*
  2228. * Allocate a new free STRIP channel
  2229. */
  2230. static struct strip *strip_alloc(void)
  2231. {
  2232. struct list_head *n;
  2233. unsigned long flags;
  2234. struct net_device *dev;
  2235. struct strip *strip_info;
  2236. dev = alloc_netdev(sizeof(struct strip), "st%d",
  2237. strip_dev_setup);
  2238. if (!dev)
  2239. return NULL; /* If no more memory, return */
  2240. strip_info = netdev_priv(dev);
  2241. strip_info->dev = dev;
  2242. strip_info->magic = STRIP_MAGIC;
  2243. strip_info->tty = NULL;
  2244. strip_info->gratuitous_arp = jiffies + LongTime;
  2245. strip_info->arp_interval = 0;
  2246. init_timer(&strip_info->idle_timer);
  2247. strip_info->idle_timer.data = (long) dev;
  2248. strip_info->idle_timer.function = strip_IdleTask;
  2249. spin_lock_irqsave(&strip_lock, flags);
  2250. rescan:
  2251. /*
  2252. * Search the list to find where to put our new entry
  2253. * (and in the process decide what channel number it is
  2254. * going to be)
  2255. */
  2256. list_for_each(n, &strip_list) {
  2257. struct strip *s = hlist_entry(n, struct strip, list);
  2258. if (s->dev->base_addr == dev->base_addr) {
  2259. ++dev->base_addr;
  2260. goto rescan;
  2261. }
  2262. }
  2263. sprintf(dev->name, "st%ld", dev->base_addr);
  2264. list_add_tail_rcu(&strip_info->list, &strip_list);
  2265. spin_unlock_irqrestore(&strip_lock, flags);
  2266. return strip_info;
  2267. }
  2268. /*
  2269. * Open the high-level part of the STRIP channel.
  2270. * This function is called by the TTY module when the
  2271. * STRIP line discipline is called for. Because we are
  2272. * sure the tty line exists, we only have to link it to
  2273. * a free STRIP channel...
  2274. */
  2275. static int strip_open(struct tty_struct *tty)
  2276. {
  2277. struct strip *strip_info = tty->disc_data;
  2278. /*
  2279. * First make sure we're not already connected.
  2280. */
  2281. if (strip_info && strip_info->magic == STRIP_MAGIC)
  2282. return -EEXIST;
  2283. /*
  2284. * We need a write method.
  2285. */
  2286. if (tty->ops->write == NULL || tty->ops->set_termios == NULL)
  2287. return -EOPNOTSUPP;
  2288. /*
  2289. * OK. Find a free STRIP channel to use.
  2290. */
  2291. if ((strip_info = strip_alloc()) == NULL)
  2292. return -ENFILE;
  2293. /*
  2294. * Register our newly created device so it can be ifconfig'd
  2295. * strip_dev_init() will be called as a side-effect
  2296. */
  2297. if (register_netdev(strip_info->dev) != 0) {
  2298. printk(KERN_ERR "strip: register_netdev() failed.\n");
  2299. strip_free(strip_info);
  2300. return -ENFILE;
  2301. }
  2302. strip_info->tty = tty;
  2303. tty->disc_data = strip_info;
  2304. tty->receive_room = 65536;
  2305. tty_driver_flush_buffer(tty);
  2306. /*
  2307. * Restore default settings
  2308. */
  2309. strip_info->dev->type = ARPHRD_METRICOM; /* dtang */
  2310. /*
  2311. * Set tty options
  2312. */
  2313. tty->termios->c_iflag |= IGNBRK | IGNPAR; /* Ignore breaks and parity errors. */
  2314. tty->termios->c_cflag |= CLOCAL; /* Ignore modem control signals. */
  2315. tty->termios->c_cflag &= ~HUPCL; /* Don't close on hup */
  2316. printk(KERN_INFO "STRIP: device \"%s\" activated\n",
  2317. strip_info->dev->name);
  2318. /*
  2319. * Done. We have linked the TTY line to a channel.
  2320. */
  2321. return (strip_info->dev->base_addr);
  2322. }
  2323. /*
  2324. * Close down a STRIP channel.
  2325. * This means flushing out any pending queues, and then restoring the
  2326. * TTY line discipline to what it was before it got hooked to STRIP
  2327. * (which usually is TTY again).
  2328. */
  2329. static void strip_close(struct tty_struct *tty)
  2330. {
  2331. struct strip *strip_info = tty->disc_data;
  2332. /*
  2333. * First make sure we're connected.
  2334. */
  2335. if (!strip_info || strip_info->magic != STRIP_MAGIC)
  2336. return;
  2337. unregister_netdev(strip_info->dev);
  2338. tty->disc_data = NULL;
  2339. strip_info->tty = NULL;
  2340. printk(KERN_INFO "STRIP: device \"%s\" closed down\n",
  2341. strip_info->dev->name);
  2342. strip_free(strip_info);
  2343. tty->disc_data = NULL;
  2344. }
  2345. /************************************************************************/
  2346. /* Perform I/O control calls on an active STRIP channel. */
  2347. static int strip_ioctl(struct tty_struct *tty, struct file *file,
  2348. unsigned int cmd, unsigned long arg)
  2349. {
  2350. struct strip *strip_info = tty->disc_data;
  2351. /*
  2352. * First make sure we're connected.
  2353. */
  2354. if (!strip_info || strip_info->magic != STRIP_MAGIC)
  2355. return -EINVAL;
  2356. switch (cmd) {
  2357. case SIOCGIFNAME:
  2358. if(copy_to_user((void __user *) arg, strip_info->dev->name, strlen(strip_info->dev->name) + 1))
  2359. return -EFAULT;
  2360. break;
  2361. case SIOCSIFHWADDR:
  2362. {
  2363. MetricomAddress addr;
  2364. //printk(KERN_INFO "%s: SIOCSIFHWADDR\n", strip_info->dev->name);
  2365. if(copy_from_user(&addr, (void __user *) arg, sizeof(MetricomAddress)))
  2366. return -EFAULT;
  2367. return set_mac_address(strip_info, &addr);
  2368. }
  2369. default:
  2370. return tty_mode_ioctl(tty, file, cmd, arg);
  2371. break;
  2372. }
  2373. return 0;
  2374. }
  2375. /************************************************************************/
  2376. /* Initialization */
  2377. static struct tty_ldisc_ops strip_ldisc = {
  2378. .magic = TTY_LDISC_MAGIC,
  2379. .name = "strip",
  2380. .owner = THIS_MODULE,
  2381. .open = strip_open,
  2382. .close = strip_close,
  2383. .ioctl = strip_ioctl,
  2384. .receive_buf = strip_receive_buf,
  2385. .write_wakeup = strip_write_some_more,
  2386. };
  2387. /*
  2388. * Initialize the STRIP driver.
  2389. * This routine is called at boot time, to bootstrap the multi-channel
  2390. * STRIP driver
  2391. */
  2392. static char signon[] __initdata =
  2393. KERN_INFO "STRIP: Version %s (unlimited channels)\n";
  2394. static int __init strip_init_driver(void)
  2395. {
  2396. int status;
  2397. printk(signon, StripVersion);
  2398. /*
  2399. * Fill in our line protocol discipline, and register it
  2400. */
  2401. if ((status = tty_register_ldisc(N_STRIP, &strip_ldisc)))
  2402. printk(KERN_ERR "STRIP: can't register line discipline (err = %d)\n",
  2403. status);
  2404. /*
  2405. * Register the status file with /proc
  2406. */
  2407. proc_net_fops_create(&init_net, "strip", S_IFREG | S_IRUGO, &strip_seq_fops);
  2408. return status;
  2409. }
  2410. module_init(strip_init_driver);
  2411. static const char signoff[] __exitdata =
  2412. KERN_INFO "STRIP: Module Unloaded\n";
  2413. static void __exit strip_exit_driver(void)
  2414. {
  2415. int i;
  2416. struct list_head *p,*n;
  2417. /* module ref count rules assure that all entries are unregistered */
  2418. list_for_each_safe(p, n, &strip_list) {
  2419. struct strip *s = list_entry(p, struct strip, list);
  2420. strip_free(s);
  2421. }
  2422. /* Unregister with the /proc/net file here. */
  2423. proc_net_remove(&init_net, "strip");
  2424. if ((i = tty_unregister_ldisc(N_STRIP)))
  2425. printk(KERN_ERR "STRIP: can't unregister line discipline (err = %d)\n", i);
  2426. printk(signoff);
  2427. }
  2428. module_exit(strip_exit_driver);
  2429. MODULE_AUTHOR("Stuart Cheshire <cheshire@cs.stanford.edu>");
  2430. MODULE_DESCRIPTION("Starmode Radio IP (STRIP) Device Driver");
  2431. MODULE_LICENSE("Dual BSD/GPL");
  2432. MODULE_SUPPORTED_DEVICE("Starmode Radio IP (STRIP) modem");