lec.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * lec.c: Lan Emulation driver
  4. *
  5. * Marko Kiiskila <[email protected]>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  8. #include <linux/slab.h>
  9. #include <linux/kernel.h>
  10. #include <linux/bitops.h>
  11. #include <linux/capability.h>
  12. /* We are ethernet device */
  13. #include <linux/if_ether.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/etherdevice.h>
  16. #include <net/sock.h>
  17. #include <linux/skbuff.h>
  18. #include <linux/ip.h>
  19. #include <asm/byteorder.h>
  20. #include <linux/uaccess.h>
  21. #include <net/arp.h>
  22. #include <net/dst.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/seq_file.h>
  26. /* And atm device */
  27. #include <linux/atmdev.h>
  28. #include <linux/atmlec.h>
  29. /* Proxy LEC knows about bridging */
  30. #if IS_ENABLED(CONFIG_BRIDGE)
  31. #include "../bridge/br_private.h"
  32. static unsigned char bridge_ula_lec[] = { 0x01, 0x80, 0xc2, 0x00, 0x00 };
  33. #endif
  34. /* Modular too */
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. /* Hardening for Spectre-v1 */
  38. #include <linux/nospec.h>
  39. #include "lec.h"
  40. #include "lec_arpc.h"
  41. #include "resources.h"
  42. #define DUMP_PACKETS 0 /*
  43. * 0 = None,
  44. * 1 = 30 first bytes
  45. * 2 = Whole packet
  46. */
  47. #define LEC_UNRES_QUE_LEN 8 /*
  48. * number of tx packets to queue for a
  49. * single destination while waiting for SVC
  50. */
  51. static int lec_open(struct net_device *dev);
  52. static netdev_tx_t lec_start_xmit(struct sk_buff *skb,
  53. struct net_device *dev);
  54. static int lec_close(struct net_device *dev);
  55. static struct lec_arp_table *lec_arp_find(struct lec_priv *priv,
  56. const unsigned char *mac_addr);
  57. static int lec_arp_remove(struct lec_priv *priv,
  58. struct lec_arp_table *to_remove);
  59. /* LANE2 functions */
  60. static void lane2_associate_ind(struct net_device *dev, const u8 *mac_address,
  61. const u8 *tlvs, u32 sizeoftlvs);
  62. static int lane2_resolve(struct net_device *dev, const u8 *dst_mac, int force,
  63. u8 **tlvs, u32 *sizeoftlvs);
  64. static int lane2_associate_req(struct net_device *dev, const u8 *lan_dst,
  65. const u8 *tlvs, u32 sizeoftlvs);
  66. static int lec_addr_delete(struct lec_priv *priv, const unsigned char *atm_addr,
  67. unsigned long permanent);
  68. static void lec_arp_check_empties(struct lec_priv *priv,
  69. struct atm_vcc *vcc, struct sk_buff *skb);
  70. static void lec_arp_destroy(struct lec_priv *priv);
  71. static void lec_arp_init(struct lec_priv *priv);
  72. static struct atm_vcc *lec_arp_resolve(struct lec_priv *priv,
  73. const unsigned char *mac_to_find,
  74. int is_rdesc,
  75. struct lec_arp_table **ret_entry);
  76. static void lec_arp_update(struct lec_priv *priv, const unsigned char *mac_addr,
  77. const unsigned char *atm_addr,
  78. unsigned long remoteflag,
  79. unsigned int targetless_le_arp);
  80. static void lec_flush_complete(struct lec_priv *priv, unsigned long tran_id);
  81. static int lec_mcast_make(struct lec_priv *priv, struct atm_vcc *vcc);
  82. static void lec_set_flush_tran_id(struct lec_priv *priv,
  83. const unsigned char *atm_addr,
  84. unsigned long tran_id);
  85. static void lec_vcc_added(struct lec_priv *priv,
  86. const struct atmlec_ioc *ioc_data,
  87. struct atm_vcc *vcc,
  88. void (*old_push)(struct atm_vcc *vcc,
  89. struct sk_buff *skb));
  90. static void lec_vcc_close(struct lec_priv *priv, struct atm_vcc *vcc);
  91. /* must be done under lec_arp_lock */
  92. static inline void lec_arp_hold(struct lec_arp_table *entry)
  93. {
  94. refcount_inc(&entry->usage);
  95. }
  96. static inline void lec_arp_put(struct lec_arp_table *entry)
  97. {
  98. if (refcount_dec_and_test(&entry->usage))
  99. kfree(entry);
  100. }
  101. static struct lane2_ops lane2_ops = {
  102. .resolve = lane2_resolve, /* spec 3.1.3 */
  103. .associate_req = lane2_associate_req, /* spec 3.1.4 */
  104. .associate_indicator = NULL /* spec 3.1.5 */
  105. };
  106. static unsigned char bus_mac[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  107. /* Device structures */
  108. static struct net_device *dev_lec[MAX_LEC_ITF];
  109. #if IS_ENABLED(CONFIG_BRIDGE)
  110. static void lec_handle_bridge(struct sk_buff *skb, struct net_device *dev)
  111. {
  112. char *buff;
  113. struct lec_priv *priv;
  114. /*
  115. * Check if this is a BPDU. If so, ask zeppelin to send
  116. * LE_TOPOLOGY_REQUEST with the same value of Topology Change bit
  117. * as the Config BPDU has
  118. */
  119. buff = skb->data + skb->dev->hard_header_len;
  120. if (*buff++ == 0x42 && *buff++ == 0x42 && *buff++ == 0x03) {
  121. struct sock *sk;
  122. struct sk_buff *skb2;
  123. struct atmlec_msg *mesg;
  124. skb2 = alloc_skb(sizeof(struct atmlec_msg), GFP_ATOMIC);
  125. if (skb2 == NULL)
  126. return;
  127. skb2->len = sizeof(struct atmlec_msg);
  128. mesg = (struct atmlec_msg *)skb2->data;
  129. mesg->type = l_topology_change;
  130. buff += 4;
  131. mesg->content.normal.flag = *buff & 0x01;
  132. /* 0x01 is topology change */
  133. priv = netdev_priv(dev);
  134. atm_force_charge(priv->lecd, skb2->truesize);
  135. sk = sk_atm(priv->lecd);
  136. skb_queue_tail(&sk->sk_receive_queue, skb2);
  137. sk->sk_data_ready(sk);
  138. }
  139. }
  140. #endif /* IS_ENABLED(CONFIG_BRIDGE) */
  141. /*
  142. * Open/initialize the netdevice. This is called (in the current kernel)
  143. * sometime after booting when the 'ifconfig' program is run.
  144. *
  145. * This routine should set everything up anew at each open, even
  146. * registers that "should" only need to be set once at boot, so that
  147. * there is non-reboot way to recover if something goes wrong.
  148. */
  149. static int lec_open(struct net_device *dev)
  150. {
  151. netif_start_queue(dev);
  152. return 0;
  153. }
  154. static void
  155. lec_send(struct atm_vcc *vcc, struct sk_buff *skb)
  156. {
  157. struct net_device *dev = skb->dev;
  158. ATM_SKB(skb)->vcc = vcc;
  159. atm_account_tx(vcc, skb);
  160. if (vcc->send(vcc, skb) < 0) {
  161. dev->stats.tx_dropped++;
  162. return;
  163. }
  164. dev->stats.tx_packets++;
  165. dev->stats.tx_bytes += skb->len;
  166. }
  167. static void lec_tx_timeout(struct net_device *dev, unsigned int txqueue)
  168. {
  169. pr_info("%s\n", dev->name);
  170. netif_trans_update(dev);
  171. netif_wake_queue(dev);
  172. }
  173. static netdev_tx_t lec_start_xmit(struct sk_buff *skb,
  174. struct net_device *dev)
  175. {
  176. struct sk_buff *skb2;
  177. struct lec_priv *priv = netdev_priv(dev);
  178. struct lecdatahdr_8023 *lec_h;
  179. struct atm_vcc *vcc;
  180. struct lec_arp_table *entry;
  181. unsigned char *dst;
  182. int min_frame_size;
  183. int is_rdesc;
  184. pr_debug("called\n");
  185. if (!priv->lecd) {
  186. pr_info("%s:No lecd attached\n", dev->name);
  187. dev->stats.tx_errors++;
  188. netif_stop_queue(dev);
  189. kfree_skb(skb);
  190. return NETDEV_TX_OK;
  191. }
  192. pr_debug("skbuff head:%lx data:%lx tail:%lx end:%lx\n",
  193. (long)skb->head, (long)skb->data, (long)skb_tail_pointer(skb),
  194. (long)skb_end_pointer(skb));
  195. #if IS_ENABLED(CONFIG_BRIDGE)
  196. if (memcmp(skb->data, bridge_ula_lec, sizeof(bridge_ula_lec)) == 0)
  197. lec_handle_bridge(skb, dev);
  198. #endif
  199. /* Make sure we have room for lec_id */
  200. if (skb_headroom(skb) < 2) {
  201. pr_debug("reallocating skb\n");
  202. skb2 = skb_realloc_headroom(skb, LEC_HEADER_LEN);
  203. if (unlikely(!skb2)) {
  204. kfree_skb(skb);
  205. return NETDEV_TX_OK;
  206. }
  207. consume_skb(skb);
  208. skb = skb2;
  209. }
  210. skb_push(skb, 2);
  211. /* Put le header to place */
  212. lec_h = (struct lecdatahdr_8023 *)skb->data;
  213. lec_h->le_header = htons(priv->lecid);
  214. #if DUMP_PACKETS >= 2
  215. #define MAX_DUMP_SKB 99
  216. #elif DUMP_PACKETS >= 1
  217. #define MAX_DUMP_SKB 30
  218. #endif
  219. #if DUMP_PACKETS >= 1
  220. printk(KERN_DEBUG "%s: send datalen:%ld lecid:%4.4x\n",
  221. dev->name, skb->len, priv->lecid);
  222. print_hex_dump(KERN_DEBUG, "", DUMP_OFFSET, 16, 1,
  223. skb->data, min(skb->len, MAX_DUMP_SKB), true);
  224. #endif /* DUMP_PACKETS >= 1 */
  225. /* Minimum ethernet-frame size */
  226. min_frame_size = LEC_MINIMUM_8023_SIZE;
  227. if (skb->len < min_frame_size) {
  228. if ((skb->len + skb_tailroom(skb)) < min_frame_size) {
  229. skb2 = skb_copy_expand(skb, 0,
  230. min_frame_size - skb->truesize,
  231. GFP_ATOMIC);
  232. dev_kfree_skb(skb);
  233. if (skb2 == NULL) {
  234. dev->stats.tx_dropped++;
  235. return NETDEV_TX_OK;
  236. }
  237. skb = skb2;
  238. }
  239. skb_put(skb, min_frame_size - skb->len);
  240. }
  241. /* Send to right vcc */
  242. is_rdesc = 0;
  243. dst = lec_h->h_dest;
  244. entry = NULL;
  245. vcc = lec_arp_resolve(priv, dst, is_rdesc, &entry);
  246. pr_debug("%s:vcc:%p vcc_flags:%lx, entry:%p\n",
  247. dev->name, vcc, vcc ? vcc->flags : 0, entry);
  248. if (!vcc || !test_bit(ATM_VF_READY, &vcc->flags)) {
  249. if (entry && (entry->tx_wait.qlen < LEC_UNRES_QUE_LEN)) {
  250. pr_debug("%s:queuing packet, MAC address %pM\n",
  251. dev->name, lec_h->h_dest);
  252. skb_queue_tail(&entry->tx_wait, skb);
  253. } else {
  254. pr_debug("%s:tx queue full or no arp entry, dropping, MAC address: %pM\n",
  255. dev->name, lec_h->h_dest);
  256. dev->stats.tx_dropped++;
  257. dev_kfree_skb(skb);
  258. }
  259. goto out;
  260. }
  261. #if DUMP_PACKETS > 0
  262. printk(KERN_DEBUG "%s:sending to vpi:%d vci:%d\n",
  263. dev->name, vcc->vpi, vcc->vci);
  264. #endif /* DUMP_PACKETS > 0 */
  265. while (entry && (skb2 = skb_dequeue(&entry->tx_wait))) {
  266. pr_debug("emptying tx queue, MAC address %pM\n", lec_h->h_dest);
  267. lec_send(vcc, skb2);
  268. }
  269. lec_send(vcc, skb);
  270. if (!atm_may_send(vcc, 0)) {
  271. struct lec_vcc_priv *vpriv = LEC_VCC_PRIV(vcc);
  272. vpriv->xoff = 1;
  273. netif_stop_queue(dev);
  274. /*
  275. * vcc->pop() might have occurred in between, making
  276. * the vcc usuable again. Since xmit is serialized,
  277. * this is the only situation we have to re-test.
  278. */
  279. if (atm_may_send(vcc, 0))
  280. netif_wake_queue(dev);
  281. }
  282. out:
  283. if (entry)
  284. lec_arp_put(entry);
  285. netif_trans_update(dev);
  286. return NETDEV_TX_OK;
  287. }
  288. /* The inverse routine to net_open(). */
  289. static int lec_close(struct net_device *dev)
  290. {
  291. netif_stop_queue(dev);
  292. return 0;
  293. }
  294. static int lec_atm_send(struct atm_vcc *vcc, struct sk_buff *skb)
  295. {
  296. static const u8 zero_addr[ETH_ALEN] = {};
  297. unsigned long flags;
  298. struct net_device *dev = (struct net_device *)vcc->proto_data;
  299. struct lec_priv *priv = netdev_priv(dev);
  300. struct atmlec_msg *mesg;
  301. struct lec_arp_table *entry;
  302. char *tmp; /* FIXME */
  303. WARN_ON(refcount_sub_and_test(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc));
  304. mesg = (struct atmlec_msg *)skb->data;
  305. tmp = skb->data;
  306. tmp += sizeof(struct atmlec_msg);
  307. pr_debug("%s: msg from zeppelin:%d\n", dev->name, mesg->type);
  308. switch (mesg->type) {
  309. case l_set_mac_addr:
  310. eth_hw_addr_set(dev, mesg->content.normal.mac_addr);
  311. break;
  312. case l_del_mac_addr:
  313. eth_hw_addr_set(dev, zero_addr);
  314. break;
  315. case l_addr_delete:
  316. lec_addr_delete(priv, mesg->content.normal.atm_addr,
  317. mesg->content.normal.flag);
  318. break;
  319. case l_topology_change:
  320. priv->topology_change = mesg->content.normal.flag;
  321. break;
  322. case l_flush_complete:
  323. lec_flush_complete(priv, mesg->content.normal.flag);
  324. break;
  325. case l_narp_req: /* LANE2: see 7.1.35 in the lane2 spec */
  326. spin_lock_irqsave(&priv->lec_arp_lock, flags);
  327. entry = lec_arp_find(priv, mesg->content.normal.mac_addr);
  328. lec_arp_remove(priv, entry);
  329. spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
  330. if (mesg->content.normal.no_source_le_narp)
  331. break;
  332. fallthrough;
  333. case l_arp_update:
  334. lec_arp_update(priv, mesg->content.normal.mac_addr,
  335. mesg->content.normal.atm_addr,
  336. mesg->content.normal.flag,
  337. mesg->content.normal.targetless_le_arp);
  338. pr_debug("in l_arp_update\n");
  339. if (mesg->sizeoftlvs != 0) { /* LANE2 3.1.5 */
  340. pr_debug("LANE2 3.1.5, got tlvs, size %d\n",
  341. mesg->sizeoftlvs);
  342. lane2_associate_ind(dev, mesg->content.normal.mac_addr,
  343. tmp, mesg->sizeoftlvs);
  344. }
  345. break;
  346. case l_config:
  347. priv->maximum_unknown_frame_count =
  348. mesg->content.config.maximum_unknown_frame_count;
  349. priv->max_unknown_frame_time =
  350. (mesg->content.config.max_unknown_frame_time * HZ);
  351. priv->max_retry_count = mesg->content.config.max_retry_count;
  352. priv->aging_time = (mesg->content.config.aging_time * HZ);
  353. priv->forward_delay_time =
  354. (mesg->content.config.forward_delay_time * HZ);
  355. priv->arp_response_time =
  356. (mesg->content.config.arp_response_time * HZ);
  357. priv->flush_timeout = (mesg->content.config.flush_timeout * HZ);
  358. priv->path_switching_delay =
  359. (mesg->content.config.path_switching_delay * HZ);
  360. priv->lane_version = mesg->content.config.lane_version;
  361. /* LANE2 */
  362. priv->lane2_ops = NULL;
  363. if (priv->lane_version > 1)
  364. priv->lane2_ops = &lane2_ops;
  365. rtnl_lock();
  366. if (dev_set_mtu(dev, mesg->content.config.mtu))
  367. pr_info("%s: change_mtu to %d failed\n",
  368. dev->name, mesg->content.config.mtu);
  369. rtnl_unlock();
  370. priv->is_proxy = mesg->content.config.is_proxy;
  371. break;
  372. case l_flush_tran_id:
  373. lec_set_flush_tran_id(priv, mesg->content.normal.atm_addr,
  374. mesg->content.normal.flag);
  375. break;
  376. case l_set_lecid:
  377. priv->lecid =
  378. (unsigned short)(0xffff & mesg->content.normal.flag);
  379. break;
  380. case l_should_bridge:
  381. #if IS_ENABLED(CONFIG_BRIDGE)
  382. {
  383. pr_debug("%s: bridge zeppelin asks about %pM\n",
  384. dev->name, mesg->content.proxy.mac_addr);
  385. if (br_fdb_test_addr_hook == NULL)
  386. break;
  387. if (br_fdb_test_addr_hook(dev, mesg->content.proxy.mac_addr)) {
  388. /* hit from bridge table, send LE_ARP_RESPONSE */
  389. struct sk_buff *skb2;
  390. struct sock *sk;
  391. pr_debug("%s: entry found, responding to zeppelin\n",
  392. dev->name);
  393. skb2 = alloc_skb(sizeof(struct atmlec_msg), GFP_ATOMIC);
  394. if (skb2 == NULL)
  395. break;
  396. skb2->len = sizeof(struct atmlec_msg);
  397. skb_copy_to_linear_data(skb2, mesg, sizeof(*mesg));
  398. atm_force_charge(priv->lecd, skb2->truesize);
  399. sk = sk_atm(priv->lecd);
  400. skb_queue_tail(&sk->sk_receive_queue, skb2);
  401. sk->sk_data_ready(sk);
  402. }
  403. }
  404. #endif /* IS_ENABLED(CONFIG_BRIDGE) */
  405. break;
  406. default:
  407. pr_info("%s: Unknown message type %d\n", dev->name, mesg->type);
  408. dev_kfree_skb(skb);
  409. return -EINVAL;
  410. }
  411. dev_kfree_skb(skb);
  412. return 0;
  413. }
  414. static void lec_atm_close(struct atm_vcc *vcc)
  415. {
  416. struct sk_buff *skb;
  417. struct net_device *dev = (struct net_device *)vcc->proto_data;
  418. struct lec_priv *priv = netdev_priv(dev);
  419. priv->lecd = NULL;
  420. /* Do something needful? */
  421. netif_stop_queue(dev);
  422. lec_arp_destroy(priv);
  423. if (skb_peek(&sk_atm(vcc)->sk_receive_queue))
  424. pr_info("%s closing with messages pending\n", dev->name);
  425. while ((skb = skb_dequeue(&sk_atm(vcc)->sk_receive_queue))) {
  426. atm_return(vcc, skb->truesize);
  427. dev_kfree_skb(skb);
  428. }
  429. pr_info("%s: Shut down!\n", dev->name);
  430. module_put(THIS_MODULE);
  431. }
  432. static const struct atmdev_ops lecdev_ops = {
  433. .close = lec_atm_close,
  434. .send = lec_atm_send
  435. };
  436. static struct atm_dev lecatm_dev = {
  437. .ops = &lecdev_ops,
  438. .type = "lec",
  439. .number = 999, /* dummy device number */
  440. .lock = __SPIN_LOCK_UNLOCKED(lecatm_dev.lock)
  441. };
  442. /*
  443. * LANE2: new argument struct sk_buff *data contains
  444. * the LE_ARP based TLVs introduced in the LANE2 spec
  445. */
  446. static int
  447. send_to_lecd(struct lec_priv *priv, atmlec_msg_type type,
  448. const unsigned char *mac_addr, const unsigned char *atm_addr,
  449. struct sk_buff *data)
  450. {
  451. struct sock *sk;
  452. struct sk_buff *skb;
  453. struct atmlec_msg *mesg;
  454. if (!priv || !priv->lecd)
  455. return -1;
  456. skb = alloc_skb(sizeof(struct atmlec_msg), GFP_ATOMIC);
  457. if (!skb)
  458. return -1;
  459. skb->len = sizeof(struct atmlec_msg);
  460. mesg = (struct atmlec_msg *)skb->data;
  461. memset(mesg, 0, sizeof(struct atmlec_msg));
  462. mesg->type = type;
  463. if (data != NULL)
  464. mesg->sizeoftlvs = data->len;
  465. if (mac_addr)
  466. ether_addr_copy(mesg->content.normal.mac_addr, mac_addr);
  467. else
  468. mesg->content.normal.targetless_le_arp = 1;
  469. if (atm_addr)
  470. memcpy(&mesg->content.normal.atm_addr, atm_addr, ATM_ESA_LEN);
  471. atm_force_charge(priv->lecd, skb->truesize);
  472. sk = sk_atm(priv->lecd);
  473. skb_queue_tail(&sk->sk_receive_queue, skb);
  474. sk->sk_data_ready(sk);
  475. if (data != NULL) {
  476. pr_debug("about to send %d bytes of data\n", data->len);
  477. atm_force_charge(priv->lecd, data->truesize);
  478. skb_queue_tail(&sk->sk_receive_queue, data);
  479. sk->sk_data_ready(sk);
  480. }
  481. return 0;
  482. }
  483. static void lec_set_multicast_list(struct net_device *dev)
  484. {
  485. /*
  486. * by default, all multicast frames arrive over the bus.
  487. * eventually support selective multicast service
  488. */
  489. }
  490. static const struct net_device_ops lec_netdev_ops = {
  491. .ndo_open = lec_open,
  492. .ndo_stop = lec_close,
  493. .ndo_start_xmit = lec_start_xmit,
  494. .ndo_tx_timeout = lec_tx_timeout,
  495. .ndo_set_rx_mode = lec_set_multicast_list,
  496. };
  497. static const unsigned char lec_ctrl_magic[] = {
  498. 0xff,
  499. 0x00,
  500. 0x01,
  501. 0x01
  502. };
  503. #define LEC_DATA_DIRECT_8023 2
  504. #define LEC_DATA_DIRECT_8025 3
  505. static int lec_is_data_direct(struct atm_vcc *vcc)
  506. {
  507. return ((vcc->sap.blli[0].l3.tr9577.snap[4] == LEC_DATA_DIRECT_8023) ||
  508. (vcc->sap.blli[0].l3.tr9577.snap[4] == LEC_DATA_DIRECT_8025));
  509. }
  510. static void lec_push(struct atm_vcc *vcc, struct sk_buff *skb)
  511. {
  512. unsigned long flags;
  513. struct net_device *dev = (struct net_device *)vcc->proto_data;
  514. struct lec_priv *priv = netdev_priv(dev);
  515. #if DUMP_PACKETS > 0
  516. printk(KERN_DEBUG "%s: vcc vpi:%d vci:%d\n",
  517. dev->name, vcc->vpi, vcc->vci);
  518. #endif
  519. if (!skb) {
  520. pr_debug("%s: null skb\n", dev->name);
  521. lec_vcc_close(priv, vcc);
  522. return;
  523. }
  524. #if DUMP_PACKETS >= 2
  525. #define MAX_SKB_DUMP 99
  526. #elif DUMP_PACKETS >= 1
  527. #define MAX_SKB_DUMP 30
  528. #endif
  529. #if DUMP_PACKETS > 0
  530. printk(KERN_DEBUG "%s: rcv datalen:%ld lecid:%4.4x\n",
  531. dev->name, skb->len, priv->lecid);
  532. print_hex_dump(KERN_DEBUG, "", DUMP_OFFSET, 16, 1,
  533. skb->data, min(MAX_SKB_DUMP, skb->len), true);
  534. #endif /* DUMP_PACKETS > 0 */
  535. if (memcmp(skb->data, lec_ctrl_magic, 4) == 0) {
  536. /* Control frame, to daemon */
  537. struct sock *sk = sk_atm(vcc);
  538. pr_debug("%s: To daemon\n", dev->name);
  539. skb_queue_tail(&sk->sk_receive_queue, skb);
  540. sk->sk_data_ready(sk);
  541. } else { /* Data frame, queue to protocol handlers */
  542. struct lec_arp_table *entry;
  543. unsigned char *src, *dst;
  544. atm_return(vcc, skb->truesize);
  545. if (*(__be16 *) skb->data == htons(priv->lecid) ||
  546. !priv->lecd || !(dev->flags & IFF_UP)) {
  547. /*
  548. * Probably looping back, or if lecd is missing,
  549. * lecd has gone down
  550. */
  551. pr_debug("Ignoring frame...\n");
  552. dev_kfree_skb(skb);
  553. return;
  554. }
  555. dst = ((struct lecdatahdr_8023 *)skb->data)->h_dest;
  556. /*
  557. * If this is a Data Direct VCC, and the VCC does not match
  558. * the LE_ARP cache entry, delete the LE_ARP cache entry.
  559. */
  560. spin_lock_irqsave(&priv->lec_arp_lock, flags);
  561. if (lec_is_data_direct(vcc)) {
  562. src = ((struct lecdatahdr_8023 *)skb->data)->h_source;
  563. entry = lec_arp_find(priv, src);
  564. if (entry && entry->vcc != vcc) {
  565. lec_arp_remove(priv, entry);
  566. lec_arp_put(entry);
  567. }
  568. }
  569. spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
  570. if (!(dst[0] & 0x01) && /* Never filter Multi/Broadcast */
  571. !priv->is_proxy && /* Proxy wants all the packets */
  572. memcmp(dst, dev->dev_addr, dev->addr_len)) {
  573. dev_kfree_skb(skb);
  574. return;
  575. }
  576. if (!hlist_empty(&priv->lec_arp_empty_ones))
  577. lec_arp_check_empties(priv, vcc, skb);
  578. skb_pull(skb, 2); /* skip lec_id */
  579. skb->protocol = eth_type_trans(skb, dev);
  580. dev->stats.rx_packets++;
  581. dev->stats.rx_bytes += skb->len;
  582. memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
  583. netif_rx(skb);
  584. }
  585. }
  586. static void lec_pop(struct atm_vcc *vcc, struct sk_buff *skb)
  587. {
  588. struct lec_vcc_priv *vpriv = LEC_VCC_PRIV(vcc);
  589. struct net_device *dev = skb->dev;
  590. if (vpriv == NULL) {
  591. pr_info("vpriv = NULL!?!?!?\n");
  592. return;
  593. }
  594. vpriv->old_pop(vcc, skb);
  595. if (vpriv->xoff && atm_may_send(vcc, 0)) {
  596. vpriv->xoff = 0;
  597. if (netif_running(dev) && netif_queue_stopped(dev))
  598. netif_wake_queue(dev);
  599. }
  600. }
  601. static int lec_vcc_attach(struct atm_vcc *vcc, void __user *arg)
  602. {
  603. struct lec_vcc_priv *vpriv;
  604. int bytes_left;
  605. struct atmlec_ioc ioc_data;
  606. /* Lecd must be up in this case */
  607. bytes_left = copy_from_user(&ioc_data, arg, sizeof(struct atmlec_ioc));
  608. if (bytes_left != 0)
  609. pr_info("copy from user failed for %d bytes\n", bytes_left);
  610. if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF)
  611. return -EINVAL;
  612. ioc_data.dev_num = array_index_nospec(ioc_data.dev_num, MAX_LEC_ITF);
  613. if (!dev_lec[ioc_data.dev_num])
  614. return -EINVAL;
  615. vpriv = kmalloc(sizeof(struct lec_vcc_priv), GFP_KERNEL);
  616. if (!vpriv)
  617. return -ENOMEM;
  618. vpriv->xoff = 0;
  619. vpriv->old_pop = vcc->pop;
  620. vcc->user_back = vpriv;
  621. vcc->pop = lec_pop;
  622. lec_vcc_added(netdev_priv(dev_lec[ioc_data.dev_num]),
  623. &ioc_data, vcc, vcc->push);
  624. vcc->proto_data = dev_lec[ioc_data.dev_num];
  625. vcc->push = lec_push;
  626. return 0;
  627. }
  628. static int lec_mcast_attach(struct atm_vcc *vcc, int arg)
  629. {
  630. if (arg < 0 || arg >= MAX_LEC_ITF)
  631. return -EINVAL;
  632. arg = array_index_nospec(arg, MAX_LEC_ITF);
  633. if (!dev_lec[arg])
  634. return -EINVAL;
  635. vcc->proto_data = dev_lec[arg];
  636. return lec_mcast_make(netdev_priv(dev_lec[arg]), vcc);
  637. }
  638. /* Initialize device. */
  639. static int lecd_attach(struct atm_vcc *vcc, int arg)
  640. {
  641. int i;
  642. struct lec_priv *priv;
  643. if (arg < 0)
  644. arg = 0;
  645. if (arg >= MAX_LEC_ITF)
  646. return -EINVAL;
  647. i = array_index_nospec(arg, MAX_LEC_ITF);
  648. if (!dev_lec[i]) {
  649. int size;
  650. size = sizeof(struct lec_priv);
  651. dev_lec[i] = alloc_etherdev(size);
  652. if (!dev_lec[i])
  653. return -ENOMEM;
  654. dev_lec[i]->netdev_ops = &lec_netdev_ops;
  655. dev_lec[i]->max_mtu = 18190;
  656. snprintf(dev_lec[i]->name, IFNAMSIZ, "lec%d", i);
  657. if (register_netdev(dev_lec[i])) {
  658. free_netdev(dev_lec[i]);
  659. return -EINVAL;
  660. }
  661. priv = netdev_priv(dev_lec[i]);
  662. } else {
  663. priv = netdev_priv(dev_lec[i]);
  664. if (priv->lecd)
  665. return -EADDRINUSE;
  666. }
  667. lec_arp_init(priv);
  668. priv->itfnum = i; /* LANE2 addition */
  669. priv->lecd = vcc;
  670. vcc->dev = &lecatm_dev;
  671. vcc_insert_socket(sk_atm(vcc));
  672. vcc->proto_data = dev_lec[i];
  673. set_bit(ATM_VF_META, &vcc->flags);
  674. set_bit(ATM_VF_READY, &vcc->flags);
  675. /* Set default values to these variables */
  676. priv->maximum_unknown_frame_count = 1;
  677. priv->max_unknown_frame_time = (1 * HZ);
  678. priv->vcc_timeout_period = (1200 * HZ);
  679. priv->max_retry_count = 1;
  680. priv->aging_time = (300 * HZ);
  681. priv->forward_delay_time = (15 * HZ);
  682. priv->topology_change = 0;
  683. priv->arp_response_time = (1 * HZ);
  684. priv->flush_timeout = (4 * HZ);
  685. priv->path_switching_delay = (6 * HZ);
  686. if (dev_lec[i]->flags & IFF_UP)
  687. netif_start_queue(dev_lec[i]);
  688. __module_get(THIS_MODULE);
  689. return i;
  690. }
  691. #ifdef CONFIG_PROC_FS
  692. static const char *lec_arp_get_status_string(unsigned char status)
  693. {
  694. static const char *const lec_arp_status_string[] = {
  695. "ESI_UNKNOWN ",
  696. "ESI_ARP_PENDING ",
  697. "ESI_VC_PENDING ",
  698. "<Undefined> ",
  699. "ESI_FLUSH_PENDING ",
  700. "ESI_FORWARD_DIRECT"
  701. };
  702. if (status > ESI_FORWARD_DIRECT)
  703. status = 3; /* ESI_UNDEFINED */
  704. return lec_arp_status_string[status];
  705. }
  706. static void lec_info(struct seq_file *seq, struct lec_arp_table *entry)
  707. {
  708. seq_printf(seq, "%pM ", entry->mac_addr);
  709. seq_printf(seq, "%*phN ", ATM_ESA_LEN, entry->atm_addr);
  710. seq_printf(seq, "%s %4.4x", lec_arp_get_status_string(entry->status),
  711. entry->flags & 0xffff);
  712. if (entry->vcc)
  713. seq_printf(seq, "%3d %3d ", entry->vcc->vpi, entry->vcc->vci);
  714. else
  715. seq_printf(seq, " ");
  716. if (entry->recv_vcc) {
  717. seq_printf(seq, " %3d %3d", entry->recv_vcc->vpi,
  718. entry->recv_vcc->vci);
  719. }
  720. seq_putc(seq, '\n');
  721. }
  722. struct lec_state {
  723. unsigned long flags;
  724. struct lec_priv *locked;
  725. struct hlist_node *node;
  726. struct net_device *dev;
  727. int itf;
  728. int arp_table;
  729. int misc_table;
  730. };
  731. static void *lec_tbl_walk(struct lec_state *state, struct hlist_head *tbl,
  732. loff_t *l)
  733. {
  734. struct hlist_node *e = state->node;
  735. if (!e)
  736. e = tbl->first;
  737. if (e == SEQ_START_TOKEN) {
  738. e = tbl->first;
  739. --*l;
  740. }
  741. for (; e; e = e->next) {
  742. if (--*l < 0)
  743. break;
  744. }
  745. state->node = e;
  746. return (*l < 0) ? state : NULL;
  747. }
  748. static void *lec_arp_walk(struct lec_state *state, loff_t *l,
  749. struct lec_priv *priv)
  750. {
  751. void *v = NULL;
  752. int p;
  753. for (p = state->arp_table; p < LEC_ARP_TABLE_SIZE; p++) {
  754. v = lec_tbl_walk(state, &priv->lec_arp_tables[p], l);
  755. if (v)
  756. break;
  757. }
  758. state->arp_table = p;
  759. return v;
  760. }
  761. static void *lec_misc_walk(struct lec_state *state, loff_t *l,
  762. struct lec_priv *priv)
  763. {
  764. struct hlist_head *lec_misc_tables[] = {
  765. &priv->lec_arp_empty_ones,
  766. &priv->lec_no_forward,
  767. &priv->mcast_fwds
  768. };
  769. void *v = NULL;
  770. int q;
  771. for (q = state->misc_table; q < ARRAY_SIZE(lec_misc_tables); q++) {
  772. v = lec_tbl_walk(state, lec_misc_tables[q], l);
  773. if (v)
  774. break;
  775. }
  776. state->misc_table = q;
  777. return v;
  778. }
  779. static void *lec_priv_walk(struct lec_state *state, loff_t *l,
  780. struct lec_priv *priv)
  781. {
  782. if (!state->locked) {
  783. state->locked = priv;
  784. spin_lock_irqsave(&priv->lec_arp_lock, state->flags);
  785. }
  786. if (!lec_arp_walk(state, l, priv) && !lec_misc_walk(state, l, priv)) {
  787. spin_unlock_irqrestore(&priv->lec_arp_lock, state->flags);
  788. state->locked = NULL;
  789. /* Partial state reset for the next time we get called */
  790. state->arp_table = state->misc_table = 0;
  791. }
  792. return state->locked;
  793. }
  794. static void *lec_itf_walk(struct lec_state *state, loff_t *l)
  795. {
  796. struct net_device *dev;
  797. void *v;
  798. dev = state->dev ? state->dev : dev_lec[state->itf];
  799. v = (dev && netdev_priv(dev)) ?
  800. lec_priv_walk(state, l, netdev_priv(dev)) : NULL;
  801. if (!v && dev) {
  802. dev_put(dev);
  803. /* Partial state reset for the next time we get called */
  804. dev = NULL;
  805. }
  806. state->dev = dev;
  807. return v;
  808. }
  809. static void *lec_get_idx(struct lec_state *state, loff_t l)
  810. {
  811. void *v = NULL;
  812. for (; state->itf < MAX_LEC_ITF; state->itf++) {
  813. v = lec_itf_walk(state, &l);
  814. if (v)
  815. break;
  816. }
  817. return v;
  818. }
  819. static void *lec_seq_start(struct seq_file *seq, loff_t *pos)
  820. {
  821. struct lec_state *state = seq->private;
  822. state->itf = 0;
  823. state->dev = NULL;
  824. state->locked = NULL;
  825. state->arp_table = 0;
  826. state->misc_table = 0;
  827. state->node = SEQ_START_TOKEN;
  828. return *pos ? lec_get_idx(state, *pos) : SEQ_START_TOKEN;
  829. }
  830. static void lec_seq_stop(struct seq_file *seq, void *v)
  831. {
  832. struct lec_state *state = seq->private;
  833. if (state->dev) {
  834. spin_unlock_irqrestore(&state->locked->lec_arp_lock,
  835. state->flags);
  836. dev_put(state->dev);
  837. }
  838. }
  839. static void *lec_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  840. {
  841. struct lec_state *state = seq->private;
  842. ++*pos;
  843. return lec_get_idx(state, 1);
  844. }
  845. static int lec_seq_show(struct seq_file *seq, void *v)
  846. {
  847. static const char lec_banner[] =
  848. "Itf MAC ATM destination"
  849. " Status Flags "
  850. "VPI/VCI Recv VPI/VCI\n";
  851. if (v == SEQ_START_TOKEN)
  852. seq_puts(seq, lec_banner);
  853. else {
  854. struct lec_state *state = seq->private;
  855. struct net_device *dev = state->dev;
  856. struct lec_arp_table *entry = hlist_entry(state->node,
  857. struct lec_arp_table,
  858. next);
  859. seq_printf(seq, "%s ", dev->name);
  860. lec_info(seq, entry);
  861. }
  862. return 0;
  863. }
  864. static const struct seq_operations lec_seq_ops = {
  865. .start = lec_seq_start,
  866. .next = lec_seq_next,
  867. .stop = lec_seq_stop,
  868. .show = lec_seq_show,
  869. };
  870. #endif
  871. static int lane_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  872. {
  873. struct atm_vcc *vcc = ATM_SD(sock);
  874. int err = 0;
  875. switch (cmd) {
  876. case ATMLEC_CTRL:
  877. case ATMLEC_MCAST:
  878. case ATMLEC_DATA:
  879. if (!capable(CAP_NET_ADMIN))
  880. return -EPERM;
  881. break;
  882. default:
  883. return -ENOIOCTLCMD;
  884. }
  885. switch (cmd) {
  886. case ATMLEC_CTRL:
  887. err = lecd_attach(vcc, (int)arg);
  888. if (err >= 0)
  889. sock->state = SS_CONNECTED;
  890. break;
  891. case ATMLEC_MCAST:
  892. err = lec_mcast_attach(vcc, (int)arg);
  893. break;
  894. case ATMLEC_DATA:
  895. err = lec_vcc_attach(vcc, (void __user *)arg);
  896. break;
  897. }
  898. return err;
  899. }
  900. static struct atm_ioctl lane_ioctl_ops = {
  901. .owner = THIS_MODULE,
  902. .ioctl = lane_ioctl,
  903. };
  904. static int __init lane_module_init(void)
  905. {
  906. #ifdef CONFIG_PROC_FS
  907. struct proc_dir_entry *p;
  908. p = proc_create_seq_private("lec", 0444, atm_proc_root, &lec_seq_ops,
  909. sizeof(struct lec_state), NULL);
  910. if (!p) {
  911. pr_err("Unable to initialize /proc/net/atm/lec\n");
  912. return -ENOMEM;
  913. }
  914. #endif
  915. register_atm_ioctl(&lane_ioctl_ops);
  916. pr_info("lec.c: initialized\n");
  917. return 0;
  918. }
  919. static void __exit lane_module_cleanup(void)
  920. {
  921. int i;
  922. #ifdef CONFIG_PROC_FS
  923. remove_proc_entry("lec", atm_proc_root);
  924. #endif
  925. deregister_atm_ioctl(&lane_ioctl_ops);
  926. for (i = 0; i < MAX_LEC_ITF; i++) {
  927. if (dev_lec[i] != NULL) {
  928. unregister_netdev(dev_lec[i]);
  929. free_netdev(dev_lec[i]);
  930. dev_lec[i] = NULL;
  931. }
  932. }
  933. }
  934. module_init(lane_module_init);
  935. module_exit(lane_module_cleanup);
  936. /*
  937. * LANE2: 3.1.3, LE_RESOLVE.request
  938. * Non force allocates memory and fills in *tlvs, fills in *sizeoftlvs.
  939. * If sizeoftlvs == NULL the default TLVs associated with this
  940. * lec will be used.
  941. * If dst_mac == NULL, targetless LE_ARP will be sent
  942. */
  943. static int lane2_resolve(struct net_device *dev, const u8 *dst_mac, int force,
  944. u8 **tlvs, u32 *sizeoftlvs)
  945. {
  946. unsigned long flags;
  947. struct lec_priv *priv = netdev_priv(dev);
  948. struct lec_arp_table *table;
  949. struct sk_buff *skb;
  950. int retval;
  951. if (force == 0) {
  952. spin_lock_irqsave(&priv->lec_arp_lock, flags);
  953. table = lec_arp_find(priv, dst_mac);
  954. spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
  955. if (table == NULL)
  956. return -1;
  957. *tlvs = kmemdup(table->tlvs, table->sizeoftlvs, GFP_ATOMIC);
  958. if (*tlvs == NULL)
  959. return -1;
  960. *sizeoftlvs = table->sizeoftlvs;
  961. return 0;
  962. }
  963. if (sizeoftlvs == NULL)
  964. retval = send_to_lecd(priv, l_arp_xmt, dst_mac, NULL, NULL);
  965. else {
  966. skb = alloc_skb(*sizeoftlvs, GFP_ATOMIC);
  967. if (skb == NULL)
  968. return -1;
  969. skb->len = *sizeoftlvs;
  970. skb_copy_to_linear_data(skb, *tlvs, *sizeoftlvs);
  971. retval = send_to_lecd(priv, l_arp_xmt, dst_mac, NULL, skb);
  972. }
  973. return retval;
  974. }
  975. /*
  976. * LANE2: 3.1.4, LE_ASSOCIATE.request
  977. * Associate the *tlvs with the *lan_dst address.
  978. * Will overwrite any previous association
  979. * Returns 1 for success, 0 for failure (out of memory)
  980. *
  981. */
  982. static int lane2_associate_req(struct net_device *dev, const u8 *lan_dst,
  983. const u8 *tlvs, u32 sizeoftlvs)
  984. {
  985. int retval;
  986. struct sk_buff *skb;
  987. struct lec_priv *priv = netdev_priv(dev);
  988. if (!ether_addr_equal(lan_dst, dev->dev_addr))
  989. return 0; /* not our mac address */
  990. kfree(priv->tlvs); /* NULL if there was no previous association */
  991. priv->tlvs = kmemdup(tlvs, sizeoftlvs, GFP_KERNEL);
  992. if (priv->tlvs == NULL)
  993. return 0;
  994. priv->sizeoftlvs = sizeoftlvs;
  995. skb = alloc_skb(sizeoftlvs, GFP_ATOMIC);
  996. if (skb == NULL)
  997. return 0;
  998. skb->len = sizeoftlvs;
  999. skb_copy_to_linear_data(skb, tlvs, sizeoftlvs);
  1000. retval = send_to_lecd(priv, l_associate_req, NULL, NULL, skb);
  1001. if (retval != 0)
  1002. pr_info("lec.c: lane2_associate_req() failed\n");
  1003. /*
  1004. * If the previous association has changed we must
  1005. * somehow notify other LANE entities about the change
  1006. */
  1007. return 1;
  1008. }
  1009. /*
  1010. * LANE2: 3.1.5, LE_ASSOCIATE.indication
  1011. *
  1012. */
  1013. static void lane2_associate_ind(struct net_device *dev, const u8 *mac_addr,
  1014. const u8 *tlvs, u32 sizeoftlvs)
  1015. {
  1016. #if 0
  1017. int i = 0;
  1018. #endif
  1019. struct lec_priv *priv = netdev_priv(dev);
  1020. #if 0 /*
  1021. * Why have the TLVs in LE_ARP entries
  1022. * since we do not use them? When you
  1023. * uncomment this code, make sure the
  1024. * TLVs get freed when entry is killed
  1025. */
  1026. struct lec_arp_table *entry = lec_arp_find(priv, mac_addr);
  1027. if (entry == NULL)
  1028. return; /* should not happen */
  1029. kfree(entry->tlvs);
  1030. entry->tlvs = kmemdup(tlvs, sizeoftlvs, GFP_KERNEL);
  1031. if (entry->tlvs == NULL)
  1032. return;
  1033. entry->sizeoftlvs = sizeoftlvs;
  1034. #endif
  1035. #if 0
  1036. pr_info("\n");
  1037. pr_info("dump of tlvs, sizeoftlvs=%d\n", sizeoftlvs);
  1038. while (i < sizeoftlvs)
  1039. pr_cont("%02x ", tlvs[i++]);
  1040. pr_cont("\n");
  1041. #endif
  1042. /* tell MPOA about the TLVs we saw */
  1043. if (priv->lane2_ops && priv->lane2_ops->associate_indicator) {
  1044. priv->lane2_ops->associate_indicator(dev, mac_addr,
  1045. tlvs, sizeoftlvs);
  1046. }
  1047. }
  1048. /*
  1049. * Here starts what used to lec_arpc.c
  1050. *
  1051. * lec_arpc.c was added here when making
  1052. * lane client modular. October 1997
  1053. */
  1054. #include <linux/types.h>
  1055. #include <linux/timer.h>
  1056. #include <linux/param.h>
  1057. #include <linux/atomic.h>
  1058. #include <linux/inetdevice.h>
  1059. #include <net/route.h>
  1060. #if 0
  1061. #define pr_debug(format, args...)
  1062. /*
  1063. #define pr_debug printk
  1064. */
  1065. #endif
  1066. #define DEBUG_ARP_TABLE 0
  1067. #define LEC_ARP_REFRESH_INTERVAL (3*HZ)
  1068. static void lec_arp_check_expire(struct work_struct *work);
  1069. static void lec_arp_expire_arp(struct timer_list *t);
  1070. /*
  1071. * Arp table funcs
  1072. */
  1073. #define HASH(ch) (ch & (LEC_ARP_TABLE_SIZE - 1))
  1074. /*
  1075. * Initialization of arp-cache
  1076. */
  1077. static void lec_arp_init(struct lec_priv *priv)
  1078. {
  1079. unsigned short i;
  1080. for (i = 0; i < LEC_ARP_TABLE_SIZE; i++)
  1081. INIT_HLIST_HEAD(&priv->lec_arp_tables[i]);
  1082. INIT_HLIST_HEAD(&priv->lec_arp_empty_ones);
  1083. INIT_HLIST_HEAD(&priv->lec_no_forward);
  1084. INIT_HLIST_HEAD(&priv->mcast_fwds);
  1085. spin_lock_init(&priv->lec_arp_lock);
  1086. INIT_DELAYED_WORK(&priv->lec_arp_work, lec_arp_check_expire);
  1087. schedule_delayed_work(&priv->lec_arp_work, LEC_ARP_REFRESH_INTERVAL);
  1088. }
  1089. static void lec_arp_clear_vccs(struct lec_arp_table *entry)
  1090. {
  1091. if (entry->vcc) {
  1092. struct atm_vcc *vcc = entry->vcc;
  1093. struct lec_vcc_priv *vpriv = LEC_VCC_PRIV(vcc);
  1094. struct net_device *dev = (struct net_device *)vcc->proto_data;
  1095. vcc->pop = vpriv->old_pop;
  1096. if (vpriv->xoff)
  1097. netif_wake_queue(dev);
  1098. kfree(vpriv);
  1099. vcc->user_back = NULL;
  1100. vcc->push = entry->old_push;
  1101. vcc_release_async(vcc, -EPIPE);
  1102. entry->vcc = NULL;
  1103. }
  1104. if (entry->recv_vcc) {
  1105. struct atm_vcc *vcc = entry->recv_vcc;
  1106. struct lec_vcc_priv *vpriv = LEC_VCC_PRIV(vcc);
  1107. kfree(vpriv);
  1108. vcc->user_back = NULL;
  1109. entry->recv_vcc->push = entry->old_recv_push;
  1110. vcc_release_async(entry->recv_vcc, -EPIPE);
  1111. entry->recv_vcc = NULL;
  1112. }
  1113. }
  1114. /*
  1115. * Insert entry to lec_arp_table
  1116. * LANE2: Add to the end of the list to satisfy 8.1.13
  1117. */
  1118. static inline void
  1119. lec_arp_add(struct lec_priv *priv, struct lec_arp_table *entry)
  1120. {
  1121. struct hlist_head *tmp;
  1122. tmp = &priv->lec_arp_tables[HASH(entry->mac_addr[ETH_ALEN - 1])];
  1123. hlist_add_head(&entry->next, tmp);
  1124. pr_debug("Added entry:%pM\n", entry->mac_addr);
  1125. }
  1126. /*
  1127. * Remove entry from lec_arp_table
  1128. */
  1129. static int
  1130. lec_arp_remove(struct lec_priv *priv, struct lec_arp_table *to_remove)
  1131. {
  1132. struct lec_arp_table *entry;
  1133. int i, remove_vcc = 1;
  1134. if (!to_remove)
  1135. return -1;
  1136. hlist_del(&to_remove->next);
  1137. del_timer(&to_remove->timer);
  1138. /*
  1139. * If this is the only MAC connected to this VCC,
  1140. * also tear down the VCC
  1141. */
  1142. if (to_remove->status >= ESI_FLUSH_PENDING) {
  1143. /*
  1144. * ESI_FLUSH_PENDING, ESI_FORWARD_DIRECT
  1145. */
  1146. for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
  1147. hlist_for_each_entry(entry,
  1148. &priv->lec_arp_tables[i], next) {
  1149. if (memcmp(to_remove->atm_addr,
  1150. entry->atm_addr, ATM_ESA_LEN) == 0) {
  1151. remove_vcc = 0;
  1152. break;
  1153. }
  1154. }
  1155. }
  1156. if (remove_vcc)
  1157. lec_arp_clear_vccs(to_remove);
  1158. }
  1159. skb_queue_purge(&to_remove->tx_wait); /* FIXME: good place for this? */
  1160. pr_debug("Removed entry:%pM\n", to_remove->mac_addr);
  1161. return 0;
  1162. }
  1163. #if DEBUG_ARP_TABLE
  1164. static const char *get_status_string(unsigned char st)
  1165. {
  1166. switch (st) {
  1167. case ESI_UNKNOWN:
  1168. return "ESI_UNKNOWN";
  1169. case ESI_ARP_PENDING:
  1170. return "ESI_ARP_PENDING";
  1171. case ESI_VC_PENDING:
  1172. return "ESI_VC_PENDING";
  1173. case ESI_FLUSH_PENDING:
  1174. return "ESI_FLUSH_PENDING";
  1175. case ESI_FORWARD_DIRECT:
  1176. return "ESI_FORWARD_DIRECT";
  1177. }
  1178. return "<UNKNOWN>";
  1179. }
  1180. static void dump_arp_table(struct lec_priv *priv)
  1181. {
  1182. struct lec_arp_table *rulla;
  1183. char buf[256];
  1184. int i, offset;
  1185. pr_info("Dump %p:\n", priv);
  1186. for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
  1187. hlist_for_each_entry(rulla,
  1188. &priv->lec_arp_tables[i], next) {
  1189. offset = 0;
  1190. offset += sprintf(buf, "%d: %p\n", i, rulla);
  1191. offset += sprintf(buf + offset, "Mac: %pM ",
  1192. rulla->mac_addr);
  1193. offset += sprintf(buf + offset, "Atm: %*ph ", ATM_ESA_LEN,
  1194. rulla->atm_addr);
  1195. offset += sprintf(buf + offset,
  1196. "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
  1197. rulla->vcc ? rulla->vcc->vpi : 0,
  1198. rulla->vcc ? rulla->vcc->vci : 0,
  1199. rulla->recv_vcc ? rulla->recv_vcc->
  1200. vpi : 0,
  1201. rulla->recv_vcc ? rulla->recv_vcc->
  1202. vci : 0, rulla->last_used,
  1203. rulla->timestamp, rulla->no_tries);
  1204. offset +=
  1205. sprintf(buf + offset,
  1206. "Flags:%x, Packets_flooded:%x, Status: %s ",
  1207. rulla->flags, rulla->packets_flooded,
  1208. get_status_string(rulla->status));
  1209. pr_info("%s\n", buf);
  1210. }
  1211. }
  1212. if (!hlist_empty(&priv->lec_no_forward))
  1213. pr_info("No forward\n");
  1214. hlist_for_each_entry(rulla, &priv->lec_no_forward, next) {
  1215. offset = 0;
  1216. offset += sprintf(buf + offset, "Mac: %pM ", rulla->mac_addr);
  1217. offset += sprintf(buf + offset, "Atm: %*ph ", ATM_ESA_LEN,
  1218. rulla->atm_addr);
  1219. offset += sprintf(buf + offset,
  1220. "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
  1221. rulla->vcc ? rulla->vcc->vpi : 0,
  1222. rulla->vcc ? rulla->vcc->vci : 0,
  1223. rulla->recv_vcc ? rulla->recv_vcc->vpi : 0,
  1224. rulla->recv_vcc ? rulla->recv_vcc->vci : 0,
  1225. rulla->last_used,
  1226. rulla->timestamp, rulla->no_tries);
  1227. offset += sprintf(buf + offset,
  1228. "Flags:%x, Packets_flooded:%x, Status: %s ",
  1229. rulla->flags, rulla->packets_flooded,
  1230. get_status_string(rulla->status));
  1231. pr_info("%s\n", buf);
  1232. }
  1233. if (!hlist_empty(&priv->lec_arp_empty_ones))
  1234. pr_info("Empty ones\n");
  1235. hlist_for_each_entry(rulla, &priv->lec_arp_empty_ones, next) {
  1236. offset = 0;
  1237. offset += sprintf(buf + offset, "Mac: %pM ", rulla->mac_addr);
  1238. offset += sprintf(buf + offset, "Atm: %*ph ", ATM_ESA_LEN,
  1239. rulla->atm_addr);
  1240. offset += sprintf(buf + offset,
  1241. "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
  1242. rulla->vcc ? rulla->vcc->vpi : 0,
  1243. rulla->vcc ? rulla->vcc->vci : 0,
  1244. rulla->recv_vcc ? rulla->recv_vcc->vpi : 0,
  1245. rulla->recv_vcc ? rulla->recv_vcc->vci : 0,
  1246. rulla->last_used,
  1247. rulla->timestamp, rulla->no_tries);
  1248. offset += sprintf(buf + offset,
  1249. "Flags:%x, Packets_flooded:%x, Status: %s ",
  1250. rulla->flags, rulla->packets_flooded,
  1251. get_status_string(rulla->status));
  1252. pr_info("%s", buf);
  1253. }
  1254. if (!hlist_empty(&priv->mcast_fwds))
  1255. pr_info("Multicast Forward VCCs\n");
  1256. hlist_for_each_entry(rulla, &priv->mcast_fwds, next) {
  1257. offset = 0;
  1258. offset += sprintf(buf + offset, "Mac: %pM ", rulla->mac_addr);
  1259. offset += sprintf(buf + offset, "Atm: %*ph ", ATM_ESA_LEN,
  1260. rulla->atm_addr);
  1261. offset += sprintf(buf + offset,
  1262. "Vcc vpi:%d vci:%d, Recv_vcc vpi:%d vci:%d Last_used:%lx, Timestamp:%lx, No_tries:%d ",
  1263. rulla->vcc ? rulla->vcc->vpi : 0,
  1264. rulla->vcc ? rulla->vcc->vci : 0,
  1265. rulla->recv_vcc ? rulla->recv_vcc->vpi : 0,
  1266. rulla->recv_vcc ? rulla->recv_vcc->vci : 0,
  1267. rulla->last_used,
  1268. rulla->timestamp, rulla->no_tries);
  1269. offset += sprintf(buf + offset,
  1270. "Flags:%x, Packets_flooded:%x, Status: %s ",
  1271. rulla->flags, rulla->packets_flooded,
  1272. get_status_string(rulla->status));
  1273. pr_info("%s\n", buf);
  1274. }
  1275. }
  1276. #else
  1277. #define dump_arp_table(priv) do { } while (0)
  1278. #endif
  1279. /*
  1280. * Destruction of arp-cache
  1281. */
  1282. static void lec_arp_destroy(struct lec_priv *priv)
  1283. {
  1284. unsigned long flags;
  1285. struct hlist_node *next;
  1286. struct lec_arp_table *entry;
  1287. int i;
  1288. cancel_delayed_work_sync(&priv->lec_arp_work);
  1289. /*
  1290. * Remove all entries
  1291. */
  1292. spin_lock_irqsave(&priv->lec_arp_lock, flags);
  1293. for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
  1294. hlist_for_each_entry_safe(entry, next,
  1295. &priv->lec_arp_tables[i], next) {
  1296. lec_arp_remove(priv, entry);
  1297. lec_arp_put(entry);
  1298. }
  1299. INIT_HLIST_HEAD(&priv->lec_arp_tables[i]);
  1300. }
  1301. hlist_for_each_entry_safe(entry, next,
  1302. &priv->lec_arp_empty_ones, next) {
  1303. del_timer_sync(&entry->timer);
  1304. lec_arp_clear_vccs(entry);
  1305. hlist_del(&entry->next);
  1306. lec_arp_put(entry);
  1307. }
  1308. INIT_HLIST_HEAD(&priv->lec_arp_empty_ones);
  1309. hlist_for_each_entry_safe(entry, next,
  1310. &priv->lec_no_forward, next) {
  1311. del_timer_sync(&entry->timer);
  1312. lec_arp_clear_vccs(entry);
  1313. hlist_del(&entry->next);
  1314. lec_arp_put(entry);
  1315. }
  1316. INIT_HLIST_HEAD(&priv->lec_no_forward);
  1317. hlist_for_each_entry_safe(entry, next, &priv->mcast_fwds, next) {
  1318. /* No timer, LANEv2 7.1.20 and 2.3.5.3 */
  1319. lec_arp_clear_vccs(entry);
  1320. hlist_del(&entry->next);
  1321. lec_arp_put(entry);
  1322. }
  1323. INIT_HLIST_HEAD(&priv->mcast_fwds);
  1324. priv->mcast_vcc = NULL;
  1325. spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
  1326. }
  1327. /*
  1328. * Find entry by mac_address
  1329. */
  1330. static struct lec_arp_table *lec_arp_find(struct lec_priv *priv,
  1331. const unsigned char *mac_addr)
  1332. {
  1333. struct hlist_head *head;
  1334. struct lec_arp_table *entry;
  1335. pr_debug("%pM\n", mac_addr);
  1336. head = &priv->lec_arp_tables[HASH(mac_addr[ETH_ALEN - 1])];
  1337. hlist_for_each_entry(entry, head, next) {
  1338. if (ether_addr_equal(mac_addr, entry->mac_addr))
  1339. return entry;
  1340. }
  1341. return NULL;
  1342. }
  1343. static struct lec_arp_table *make_entry(struct lec_priv *priv,
  1344. const unsigned char *mac_addr)
  1345. {
  1346. struct lec_arp_table *to_return;
  1347. to_return = kzalloc(sizeof(struct lec_arp_table), GFP_ATOMIC);
  1348. if (!to_return)
  1349. return NULL;
  1350. ether_addr_copy(to_return->mac_addr, mac_addr);
  1351. INIT_HLIST_NODE(&to_return->next);
  1352. timer_setup(&to_return->timer, lec_arp_expire_arp, 0);
  1353. to_return->last_used = jiffies;
  1354. to_return->priv = priv;
  1355. skb_queue_head_init(&to_return->tx_wait);
  1356. refcount_set(&to_return->usage, 1);
  1357. return to_return;
  1358. }
  1359. /* Arp sent timer expired */
  1360. static void lec_arp_expire_arp(struct timer_list *t)
  1361. {
  1362. struct lec_arp_table *entry;
  1363. entry = from_timer(entry, t, timer);
  1364. pr_debug("\n");
  1365. if (entry->status == ESI_ARP_PENDING) {
  1366. if (entry->no_tries <= entry->priv->max_retry_count) {
  1367. if (entry->is_rdesc)
  1368. send_to_lecd(entry->priv, l_rdesc_arp_xmt,
  1369. entry->mac_addr, NULL, NULL);
  1370. else
  1371. send_to_lecd(entry->priv, l_arp_xmt,
  1372. entry->mac_addr, NULL, NULL);
  1373. entry->no_tries++;
  1374. }
  1375. mod_timer(&entry->timer, jiffies + (1 * HZ));
  1376. }
  1377. }
  1378. /* Unknown/unused vcc expire, remove associated entry */
  1379. static void lec_arp_expire_vcc(struct timer_list *t)
  1380. {
  1381. unsigned long flags;
  1382. struct lec_arp_table *to_remove = from_timer(to_remove, t, timer);
  1383. struct lec_priv *priv = to_remove->priv;
  1384. del_timer(&to_remove->timer);
  1385. pr_debug("%p %p: vpi:%d vci:%d\n",
  1386. to_remove, priv,
  1387. to_remove->vcc ? to_remove->recv_vcc->vpi : 0,
  1388. to_remove->vcc ? to_remove->recv_vcc->vci : 0);
  1389. spin_lock_irqsave(&priv->lec_arp_lock, flags);
  1390. hlist_del(&to_remove->next);
  1391. spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
  1392. lec_arp_clear_vccs(to_remove);
  1393. lec_arp_put(to_remove);
  1394. }
  1395. static bool __lec_arp_check_expire(struct lec_arp_table *entry,
  1396. unsigned long now,
  1397. struct lec_priv *priv)
  1398. {
  1399. unsigned long time_to_check;
  1400. if ((entry->flags) & LEC_REMOTE_FLAG && priv->topology_change)
  1401. time_to_check = priv->forward_delay_time;
  1402. else
  1403. time_to_check = priv->aging_time;
  1404. pr_debug("About to expire: %lx - %lx > %lx\n",
  1405. now, entry->last_used, time_to_check);
  1406. if (time_after(now, entry->last_used + time_to_check) &&
  1407. !(entry->flags & LEC_PERMANENT_FLAG) &&
  1408. !(entry->mac_addr[0] & 0x01)) { /* LANE2: 7.1.20 */
  1409. /* Remove entry */
  1410. pr_debug("Entry timed out\n");
  1411. lec_arp_remove(priv, entry);
  1412. lec_arp_put(entry);
  1413. } else {
  1414. /* Something else */
  1415. if ((entry->status == ESI_VC_PENDING ||
  1416. entry->status == ESI_ARP_PENDING) &&
  1417. time_after_eq(now, entry->timestamp +
  1418. priv->max_unknown_frame_time)) {
  1419. entry->timestamp = jiffies;
  1420. entry->packets_flooded = 0;
  1421. if (entry->status == ESI_VC_PENDING)
  1422. send_to_lecd(priv, l_svc_setup,
  1423. entry->mac_addr,
  1424. entry->atm_addr,
  1425. NULL);
  1426. }
  1427. if (entry->status == ESI_FLUSH_PENDING &&
  1428. time_after_eq(now, entry->timestamp +
  1429. priv->path_switching_delay)) {
  1430. lec_arp_hold(entry);
  1431. return true;
  1432. }
  1433. }
  1434. return false;
  1435. }
  1436. /*
  1437. * Expire entries.
  1438. * 1. Re-set timer
  1439. * 2. For each entry, delete entries that have aged past the age limit.
  1440. * 3. For each entry, depending on the status of the entry, perform
  1441. * the following maintenance.
  1442. * a. If status is ESI_VC_PENDING or ESI_ARP_PENDING then if the
  1443. * tick_count is above the max_unknown_frame_time, clear
  1444. * the tick_count to zero and clear the packets_flooded counter
  1445. * to zero. This supports the packet rate limit per address
  1446. * while flooding unknowns.
  1447. * b. If the status is ESI_FLUSH_PENDING and the tick_count is greater
  1448. * than or equal to the path_switching_delay, change the status
  1449. * to ESI_FORWARD_DIRECT. This causes the flush period to end
  1450. * regardless of the progress of the flush protocol.
  1451. */
  1452. static void lec_arp_check_expire(struct work_struct *work)
  1453. {
  1454. unsigned long flags;
  1455. struct lec_priv *priv =
  1456. container_of(work, struct lec_priv, lec_arp_work.work);
  1457. struct hlist_node *next;
  1458. struct lec_arp_table *entry;
  1459. unsigned long now;
  1460. int i;
  1461. pr_debug("%p\n", priv);
  1462. now = jiffies;
  1463. restart:
  1464. spin_lock_irqsave(&priv->lec_arp_lock, flags);
  1465. for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
  1466. hlist_for_each_entry_safe(entry, next,
  1467. &priv->lec_arp_tables[i], next) {
  1468. if (__lec_arp_check_expire(entry, now, priv)) {
  1469. struct sk_buff *skb;
  1470. struct atm_vcc *vcc = entry->vcc;
  1471. spin_unlock_irqrestore(&priv->lec_arp_lock,
  1472. flags);
  1473. while ((skb = skb_dequeue(&entry->tx_wait)))
  1474. lec_send(vcc, skb);
  1475. entry->last_used = jiffies;
  1476. entry->status = ESI_FORWARD_DIRECT;
  1477. lec_arp_put(entry);
  1478. goto restart;
  1479. }
  1480. }
  1481. }
  1482. spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
  1483. schedule_delayed_work(&priv->lec_arp_work, LEC_ARP_REFRESH_INTERVAL);
  1484. }
  1485. /*
  1486. * Try to find vcc where mac_address is attached.
  1487. *
  1488. */
  1489. static struct atm_vcc *lec_arp_resolve(struct lec_priv *priv,
  1490. const unsigned char *mac_to_find,
  1491. int is_rdesc,
  1492. struct lec_arp_table **ret_entry)
  1493. {
  1494. unsigned long flags;
  1495. struct lec_arp_table *entry;
  1496. struct atm_vcc *found;
  1497. if (mac_to_find[0] & 0x01) {
  1498. switch (priv->lane_version) {
  1499. case 1:
  1500. return priv->mcast_vcc;
  1501. case 2: /* LANE2 wants arp for multicast addresses */
  1502. if (ether_addr_equal(mac_to_find, bus_mac))
  1503. return priv->mcast_vcc;
  1504. break;
  1505. default:
  1506. break;
  1507. }
  1508. }
  1509. spin_lock_irqsave(&priv->lec_arp_lock, flags);
  1510. entry = lec_arp_find(priv, mac_to_find);
  1511. if (entry) {
  1512. if (entry->status == ESI_FORWARD_DIRECT) {
  1513. /* Connection Ok */
  1514. entry->last_used = jiffies;
  1515. lec_arp_hold(entry);
  1516. *ret_entry = entry;
  1517. found = entry->vcc;
  1518. goto out;
  1519. }
  1520. /*
  1521. * If the LE_ARP cache entry is still pending, reset count to 0
  1522. * so another LE_ARP request can be made for this frame.
  1523. */
  1524. if (entry->status == ESI_ARP_PENDING)
  1525. entry->no_tries = 0;
  1526. /*
  1527. * Data direct VC not yet set up, check to see if the unknown
  1528. * frame count is greater than the limit. If the limit has
  1529. * not been reached, allow the caller to send packet to
  1530. * BUS.
  1531. */
  1532. if (entry->status != ESI_FLUSH_PENDING &&
  1533. entry->packets_flooded <
  1534. priv->maximum_unknown_frame_count) {
  1535. entry->packets_flooded++;
  1536. pr_debug("Flooding..\n");
  1537. found = priv->mcast_vcc;
  1538. goto out;
  1539. }
  1540. /*
  1541. * We got here because entry->status == ESI_FLUSH_PENDING
  1542. * or BUS flood limit was reached for an entry which is
  1543. * in ESI_ARP_PENDING or ESI_VC_PENDING state.
  1544. */
  1545. lec_arp_hold(entry);
  1546. *ret_entry = entry;
  1547. pr_debug("entry->status %d entry->vcc %p\n", entry->status,
  1548. entry->vcc);
  1549. found = NULL;
  1550. } else {
  1551. /* No matching entry was found */
  1552. entry = make_entry(priv, mac_to_find);
  1553. pr_debug("Making entry\n");
  1554. if (!entry) {
  1555. found = priv->mcast_vcc;
  1556. goto out;
  1557. }
  1558. lec_arp_add(priv, entry);
  1559. /* We want arp-request(s) to be sent */
  1560. entry->packets_flooded = 1;
  1561. entry->status = ESI_ARP_PENDING;
  1562. entry->no_tries = 1;
  1563. entry->last_used = entry->timestamp = jiffies;
  1564. entry->is_rdesc = is_rdesc;
  1565. if (entry->is_rdesc)
  1566. send_to_lecd(priv, l_rdesc_arp_xmt, mac_to_find, NULL,
  1567. NULL);
  1568. else
  1569. send_to_lecd(priv, l_arp_xmt, mac_to_find, NULL, NULL);
  1570. entry->timer.expires = jiffies + (1 * HZ);
  1571. entry->timer.function = lec_arp_expire_arp;
  1572. add_timer(&entry->timer);
  1573. found = priv->mcast_vcc;
  1574. }
  1575. out:
  1576. spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
  1577. return found;
  1578. }
  1579. static int
  1580. lec_addr_delete(struct lec_priv *priv, const unsigned char *atm_addr,
  1581. unsigned long permanent)
  1582. {
  1583. unsigned long flags;
  1584. struct hlist_node *next;
  1585. struct lec_arp_table *entry;
  1586. int i;
  1587. pr_debug("\n");
  1588. spin_lock_irqsave(&priv->lec_arp_lock, flags);
  1589. for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
  1590. hlist_for_each_entry_safe(entry, next,
  1591. &priv->lec_arp_tables[i], next) {
  1592. if (!memcmp(atm_addr, entry->atm_addr, ATM_ESA_LEN) &&
  1593. (permanent ||
  1594. !(entry->flags & LEC_PERMANENT_FLAG))) {
  1595. lec_arp_remove(priv, entry);
  1596. lec_arp_put(entry);
  1597. }
  1598. spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
  1599. return 0;
  1600. }
  1601. }
  1602. spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
  1603. return -1;
  1604. }
  1605. /*
  1606. * Notifies: Response to arp_request (atm_addr != NULL)
  1607. */
  1608. static void
  1609. lec_arp_update(struct lec_priv *priv, const unsigned char *mac_addr,
  1610. const unsigned char *atm_addr, unsigned long remoteflag,
  1611. unsigned int targetless_le_arp)
  1612. {
  1613. unsigned long flags;
  1614. struct hlist_node *next;
  1615. struct lec_arp_table *entry, *tmp;
  1616. int i;
  1617. pr_debug("%smac:%pM\n",
  1618. (targetless_le_arp) ? "targetless " : "", mac_addr);
  1619. spin_lock_irqsave(&priv->lec_arp_lock, flags);
  1620. entry = lec_arp_find(priv, mac_addr);
  1621. if (entry == NULL && targetless_le_arp)
  1622. goto out; /*
  1623. * LANE2: ignore targetless LE_ARPs for which
  1624. * we have no entry in the cache. 7.1.30
  1625. */
  1626. if (!hlist_empty(&priv->lec_arp_empty_ones)) {
  1627. hlist_for_each_entry_safe(entry, next,
  1628. &priv->lec_arp_empty_ones, next) {
  1629. if (memcmp(entry->atm_addr, atm_addr, ATM_ESA_LEN) == 0) {
  1630. hlist_del(&entry->next);
  1631. del_timer(&entry->timer);
  1632. tmp = lec_arp_find(priv, mac_addr);
  1633. if (tmp) {
  1634. del_timer(&tmp->timer);
  1635. tmp->status = ESI_FORWARD_DIRECT;
  1636. memcpy(tmp->atm_addr, atm_addr, ATM_ESA_LEN);
  1637. tmp->vcc = entry->vcc;
  1638. tmp->old_push = entry->old_push;
  1639. tmp->last_used = jiffies;
  1640. del_timer(&entry->timer);
  1641. lec_arp_put(entry);
  1642. entry = tmp;
  1643. } else {
  1644. entry->status = ESI_FORWARD_DIRECT;
  1645. ether_addr_copy(entry->mac_addr,
  1646. mac_addr);
  1647. entry->last_used = jiffies;
  1648. lec_arp_add(priv, entry);
  1649. }
  1650. if (remoteflag)
  1651. entry->flags |= LEC_REMOTE_FLAG;
  1652. else
  1653. entry->flags &= ~LEC_REMOTE_FLAG;
  1654. pr_debug("After update\n");
  1655. dump_arp_table(priv);
  1656. goto out;
  1657. }
  1658. }
  1659. }
  1660. entry = lec_arp_find(priv, mac_addr);
  1661. if (!entry) {
  1662. entry = make_entry(priv, mac_addr);
  1663. if (!entry)
  1664. goto out;
  1665. entry->status = ESI_UNKNOWN;
  1666. lec_arp_add(priv, entry);
  1667. /* Temporary, changes before end of function */
  1668. }
  1669. memcpy(entry->atm_addr, atm_addr, ATM_ESA_LEN);
  1670. del_timer(&entry->timer);
  1671. for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
  1672. hlist_for_each_entry(tmp,
  1673. &priv->lec_arp_tables[i], next) {
  1674. if (entry != tmp &&
  1675. !memcmp(tmp->atm_addr, atm_addr, ATM_ESA_LEN)) {
  1676. /* Vcc to this host exists */
  1677. if (tmp->status > ESI_VC_PENDING) {
  1678. /*
  1679. * ESI_FLUSH_PENDING,
  1680. * ESI_FORWARD_DIRECT
  1681. */
  1682. entry->vcc = tmp->vcc;
  1683. entry->old_push = tmp->old_push;
  1684. }
  1685. entry->status = tmp->status;
  1686. break;
  1687. }
  1688. }
  1689. }
  1690. if (remoteflag)
  1691. entry->flags |= LEC_REMOTE_FLAG;
  1692. else
  1693. entry->flags &= ~LEC_REMOTE_FLAG;
  1694. if (entry->status == ESI_ARP_PENDING || entry->status == ESI_UNKNOWN) {
  1695. entry->status = ESI_VC_PENDING;
  1696. send_to_lecd(priv, l_svc_setup, entry->mac_addr, atm_addr, NULL);
  1697. }
  1698. pr_debug("After update2\n");
  1699. dump_arp_table(priv);
  1700. out:
  1701. spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
  1702. }
  1703. /*
  1704. * Notifies: Vcc setup ready
  1705. */
  1706. static void
  1707. lec_vcc_added(struct lec_priv *priv, const struct atmlec_ioc *ioc_data,
  1708. struct atm_vcc *vcc,
  1709. void (*old_push) (struct atm_vcc *vcc, struct sk_buff *skb))
  1710. {
  1711. unsigned long flags;
  1712. struct lec_arp_table *entry;
  1713. int i, found_entry = 0;
  1714. spin_lock_irqsave(&priv->lec_arp_lock, flags);
  1715. /* Vcc for Multicast Forward. No timer, LANEv2 7.1.20 and 2.3.5.3 */
  1716. if (ioc_data->receive == 2) {
  1717. pr_debug("LEC_ARP: Attaching mcast forward\n");
  1718. #if 0
  1719. entry = lec_arp_find(priv, bus_mac);
  1720. if (!entry) {
  1721. pr_info("LEC_ARP: Multicast entry not found!\n");
  1722. goto out;
  1723. }
  1724. memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
  1725. entry->recv_vcc = vcc;
  1726. entry->old_recv_push = old_push;
  1727. #endif
  1728. entry = make_entry(priv, bus_mac);
  1729. if (entry == NULL)
  1730. goto out;
  1731. del_timer(&entry->timer);
  1732. memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
  1733. entry->recv_vcc = vcc;
  1734. entry->old_recv_push = old_push;
  1735. hlist_add_head(&entry->next, &priv->mcast_fwds);
  1736. goto out;
  1737. } else if (ioc_data->receive == 1) {
  1738. /*
  1739. * Vcc which we don't want to make default vcc,
  1740. * attach it anyway.
  1741. */
  1742. pr_debug("LEC_ARP:Attaching data direct, not default: %*phN\n",
  1743. ATM_ESA_LEN, ioc_data->atm_addr);
  1744. entry = make_entry(priv, bus_mac);
  1745. if (entry == NULL)
  1746. goto out;
  1747. memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
  1748. eth_zero_addr(entry->mac_addr);
  1749. entry->recv_vcc = vcc;
  1750. entry->old_recv_push = old_push;
  1751. entry->status = ESI_UNKNOWN;
  1752. entry->timer.expires = jiffies + priv->vcc_timeout_period;
  1753. entry->timer.function = lec_arp_expire_vcc;
  1754. hlist_add_head(&entry->next, &priv->lec_no_forward);
  1755. add_timer(&entry->timer);
  1756. dump_arp_table(priv);
  1757. goto out;
  1758. }
  1759. pr_debug("LEC_ARP:Attaching data direct, default: %*phN\n",
  1760. ATM_ESA_LEN, ioc_data->atm_addr);
  1761. for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
  1762. hlist_for_each_entry(entry,
  1763. &priv->lec_arp_tables[i], next) {
  1764. if (memcmp
  1765. (ioc_data->atm_addr, entry->atm_addr,
  1766. ATM_ESA_LEN) == 0) {
  1767. pr_debug("LEC_ARP: Attaching data direct\n");
  1768. pr_debug("Currently -> Vcc: %d, Rvcc:%d\n",
  1769. entry->vcc ? entry->vcc->vci : 0,
  1770. entry->recv_vcc ? entry->recv_vcc->
  1771. vci : 0);
  1772. found_entry = 1;
  1773. del_timer(&entry->timer);
  1774. entry->vcc = vcc;
  1775. entry->old_push = old_push;
  1776. if (entry->status == ESI_VC_PENDING) {
  1777. if (priv->maximum_unknown_frame_count
  1778. == 0)
  1779. entry->status =
  1780. ESI_FORWARD_DIRECT;
  1781. else {
  1782. entry->timestamp = jiffies;
  1783. entry->status =
  1784. ESI_FLUSH_PENDING;
  1785. #if 0
  1786. send_to_lecd(priv, l_flush_xmt,
  1787. NULL,
  1788. entry->atm_addr,
  1789. NULL);
  1790. #endif
  1791. }
  1792. } else {
  1793. /*
  1794. * They were forming a connection
  1795. * to us, and we to them. Our
  1796. * ATM address is numerically lower
  1797. * than theirs, so we make connection
  1798. * we formed into default VCC (8.1.11).
  1799. * Connection they made gets torn
  1800. * down. This might confuse some
  1801. * clients. Can be changed if
  1802. * someone reports trouble...
  1803. */
  1804. ;
  1805. }
  1806. }
  1807. }
  1808. }
  1809. if (found_entry) {
  1810. pr_debug("After vcc was added\n");
  1811. dump_arp_table(priv);
  1812. goto out;
  1813. }
  1814. /*
  1815. * Not found, snatch address from first data packet that arrives
  1816. * from this vcc
  1817. */
  1818. entry = make_entry(priv, bus_mac);
  1819. if (!entry)
  1820. goto out;
  1821. entry->vcc = vcc;
  1822. entry->old_push = old_push;
  1823. memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
  1824. eth_zero_addr(entry->mac_addr);
  1825. entry->status = ESI_UNKNOWN;
  1826. hlist_add_head(&entry->next, &priv->lec_arp_empty_ones);
  1827. entry->timer.expires = jiffies + priv->vcc_timeout_period;
  1828. entry->timer.function = lec_arp_expire_vcc;
  1829. add_timer(&entry->timer);
  1830. pr_debug("After vcc was added\n");
  1831. dump_arp_table(priv);
  1832. out:
  1833. spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
  1834. }
  1835. static void lec_flush_complete(struct lec_priv *priv, unsigned long tran_id)
  1836. {
  1837. unsigned long flags;
  1838. struct lec_arp_table *entry;
  1839. int i;
  1840. pr_debug("%lx\n", tran_id);
  1841. restart:
  1842. spin_lock_irqsave(&priv->lec_arp_lock, flags);
  1843. for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
  1844. hlist_for_each_entry(entry,
  1845. &priv->lec_arp_tables[i], next) {
  1846. if (entry->flush_tran_id == tran_id &&
  1847. entry->status == ESI_FLUSH_PENDING) {
  1848. struct sk_buff *skb;
  1849. struct atm_vcc *vcc = entry->vcc;
  1850. lec_arp_hold(entry);
  1851. spin_unlock_irqrestore(&priv->lec_arp_lock,
  1852. flags);
  1853. while ((skb = skb_dequeue(&entry->tx_wait)))
  1854. lec_send(vcc, skb);
  1855. entry->last_used = jiffies;
  1856. entry->status = ESI_FORWARD_DIRECT;
  1857. lec_arp_put(entry);
  1858. pr_debug("LEC_ARP: Flushed\n");
  1859. goto restart;
  1860. }
  1861. }
  1862. }
  1863. spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
  1864. dump_arp_table(priv);
  1865. }
  1866. static void
  1867. lec_set_flush_tran_id(struct lec_priv *priv,
  1868. const unsigned char *atm_addr, unsigned long tran_id)
  1869. {
  1870. unsigned long flags;
  1871. struct lec_arp_table *entry;
  1872. int i;
  1873. spin_lock_irqsave(&priv->lec_arp_lock, flags);
  1874. for (i = 0; i < LEC_ARP_TABLE_SIZE; i++)
  1875. hlist_for_each_entry(entry,
  1876. &priv->lec_arp_tables[i], next) {
  1877. if (!memcmp(atm_addr, entry->atm_addr, ATM_ESA_LEN)) {
  1878. entry->flush_tran_id = tran_id;
  1879. pr_debug("Set flush transaction id to %lx for %p\n",
  1880. tran_id, entry);
  1881. }
  1882. }
  1883. spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
  1884. }
  1885. static int lec_mcast_make(struct lec_priv *priv, struct atm_vcc *vcc)
  1886. {
  1887. unsigned long flags;
  1888. unsigned char mac_addr[] = {
  1889. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  1890. };
  1891. struct lec_arp_table *to_add;
  1892. struct lec_vcc_priv *vpriv;
  1893. int err = 0;
  1894. vpriv = kmalloc(sizeof(struct lec_vcc_priv), GFP_KERNEL);
  1895. if (!vpriv)
  1896. return -ENOMEM;
  1897. vpriv->xoff = 0;
  1898. vpriv->old_pop = vcc->pop;
  1899. vcc->user_back = vpriv;
  1900. vcc->pop = lec_pop;
  1901. spin_lock_irqsave(&priv->lec_arp_lock, flags);
  1902. to_add = make_entry(priv, mac_addr);
  1903. if (!to_add) {
  1904. vcc->pop = vpriv->old_pop;
  1905. kfree(vpriv);
  1906. err = -ENOMEM;
  1907. goto out;
  1908. }
  1909. memcpy(to_add->atm_addr, vcc->remote.sas_addr.prv, ATM_ESA_LEN);
  1910. to_add->status = ESI_FORWARD_DIRECT;
  1911. to_add->flags |= LEC_PERMANENT_FLAG;
  1912. to_add->vcc = vcc;
  1913. to_add->old_push = vcc->push;
  1914. vcc->push = lec_push;
  1915. priv->mcast_vcc = vcc;
  1916. lec_arp_add(priv, to_add);
  1917. out:
  1918. spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
  1919. return err;
  1920. }
  1921. static void lec_vcc_close(struct lec_priv *priv, struct atm_vcc *vcc)
  1922. {
  1923. unsigned long flags;
  1924. struct hlist_node *next;
  1925. struct lec_arp_table *entry;
  1926. int i;
  1927. pr_debug("LEC_ARP: lec_vcc_close vpi:%d vci:%d\n", vcc->vpi, vcc->vci);
  1928. dump_arp_table(priv);
  1929. spin_lock_irqsave(&priv->lec_arp_lock, flags);
  1930. for (i = 0; i < LEC_ARP_TABLE_SIZE; i++) {
  1931. hlist_for_each_entry_safe(entry, next,
  1932. &priv->lec_arp_tables[i], next) {
  1933. if (vcc == entry->vcc) {
  1934. lec_arp_remove(priv, entry);
  1935. lec_arp_put(entry);
  1936. if (priv->mcast_vcc == vcc)
  1937. priv->mcast_vcc = NULL;
  1938. }
  1939. }
  1940. }
  1941. hlist_for_each_entry_safe(entry, next,
  1942. &priv->lec_arp_empty_ones, next) {
  1943. if (entry->vcc == vcc) {
  1944. lec_arp_clear_vccs(entry);
  1945. del_timer(&entry->timer);
  1946. hlist_del(&entry->next);
  1947. lec_arp_put(entry);
  1948. }
  1949. }
  1950. hlist_for_each_entry_safe(entry, next,
  1951. &priv->lec_no_forward, next) {
  1952. if (entry->recv_vcc == vcc) {
  1953. lec_arp_clear_vccs(entry);
  1954. del_timer(&entry->timer);
  1955. hlist_del(&entry->next);
  1956. lec_arp_put(entry);
  1957. }
  1958. }
  1959. hlist_for_each_entry_safe(entry, next, &priv->mcast_fwds, next) {
  1960. if (entry->recv_vcc == vcc) {
  1961. lec_arp_clear_vccs(entry);
  1962. /* No timer, LANEv2 7.1.20 and 2.3.5.3 */
  1963. hlist_del(&entry->next);
  1964. lec_arp_put(entry);
  1965. }
  1966. }
  1967. spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
  1968. dump_arp_table(priv);
  1969. }
  1970. static void
  1971. lec_arp_check_empties(struct lec_priv *priv,
  1972. struct atm_vcc *vcc, struct sk_buff *skb)
  1973. {
  1974. unsigned long flags;
  1975. struct hlist_node *next;
  1976. struct lec_arp_table *entry, *tmp;
  1977. struct lecdatahdr_8023 *hdr = (struct lecdatahdr_8023 *)skb->data;
  1978. unsigned char *src = hdr->h_source;
  1979. spin_lock_irqsave(&priv->lec_arp_lock, flags);
  1980. hlist_for_each_entry_safe(entry, next,
  1981. &priv->lec_arp_empty_ones, next) {
  1982. if (vcc == entry->vcc) {
  1983. del_timer(&entry->timer);
  1984. ether_addr_copy(entry->mac_addr, src);
  1985. entry->status = ESI_FORWARD_DIRECT;
  1986. entry->last_used = jiffies;
  1987. /* We might have got an entry */
  1988. tmp = lec_arp_find(priv, src);
  1989. if (tmp) {
  1990. lec_arp_remove(priv, tmp);
  1991. lec_arp_put(tmp);
  1992. }
  1993. hlist_del(&entry->next);
  1994. lec_arp_add(priv, entry);
  1995. goto out;
  1996. }
  1997. }
  1998. pr_debug("LEC_ARP: Arp_check_empties: entry not found!\n");
  1999. out:
  2000. spin_unlock_irqrestore(&priv->lec_arp_lock, flags);
  2001. }
  2002. MODULE_LICENSE("GPL");