strip.c 83 KB

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