lec.c 59 KB

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