strip.c 83 KB

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