lec.c 59 KB

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