strip.c 83 KB

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