strip.c 84 KB

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