strip.c 82 KB

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