mpc.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  3. #include <linux/kernel.h>
  4. #include <linux/string.h>
  5. #include <linux/slab.h>
  6. #include <linux/timer.h>
  7. #include <linux/init.h>
  8. #include <linux/bitops.h>
  9. #include <linux/capability.h>
  10. #include <linux/seq_file.h>
  11. /* We are an ethernet device */
  12. #include <linux/if_ether.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/etherdevice.h>
  15. #include <net/sock.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/ip.h>
  18. #include <linux/uaccess.h>
  19. #include <asm/byteorder.h>
  20. #include <net/checksum.h> /* for ip_fast_csum() */
  21. #include <net/arp.h>
  22. #include <net/dst.h>
  23. #include <linux/proc_fs.h>
  24. /* And atm device */
  25. #include <linux/atmdev.h>
  26. #include <linux/atmlec.h>
  27. #include <linux/atmmpc.h>
  28. /* Modular too */
  29. #include <linux/module.h>
  30. #include "lec.h"
  31. #include "mpc.h"
  32. #include "resources.h"
  33. /*
  34. * mpc.c: Implementation of MPOA client kernel part
  35. */
  36. #if 0
  37. #define dprintk(format, args...) \
  38. printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args)
  39. #define dprintk_cont(format, args...) printk(KERN_CONT format, ##args)
  40. #else
  41. #define dprintk(format, args...) \
  42. do { if (0) \
  43. printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args);\
  44. } while (0)
  45. #define dprintk_cont(format, args...) \
  46. do { if (0) printk(KERN_CONT format, ##args); } while (0)
  47. #endif
  48. #if 0
  49. #define ddprintk(format, args...) \
  50. printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args)
  51. #define ddprintk_cont(format, args...) printk(KERN_CONT format, ##args)
  52. #else
  53. #define ddprintk(format, args...) \
  54. do { if (0) \
  55. printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args);\
  56. } while (0)
  57. #define ddprintk_cont(format, args...) \
  58. do { if (0) printk(KERN_CONT format, ##args); } while (0)
  59. #endif
  60. /* mpc_daemon -> kernel */
  61. static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc);
  62. static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc);
  63. static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
  64. static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
  65. static void mps_death(struct k_message *msg, struct mpoa_client *mpc);
  66. static void clean_up(struct k_message *msg, struct mpoa_client *mpc,
  67. int action);
  68. static void MPOA_cache_impos_rcvd(struct k_message *msg,
  69. struct mpoa_client *mpc);
  70. static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg,
  71. struct mpoa_client *mpc);
  72. static void set_mps_mac_addr_rcvd(struct k_message *mesg,
  73. struct mpoa_client *mpc);
  74. static const uint8_t *copy_macs(struct mpoa_client *mpc,
  75. const uint8_t *router_mac,
  76. const uint8_t *tlvs, uint8_t mps_macs,
  77. uint8_t device_type);
  78. static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry);
  79. static void send_set_mps_ctrl_addr(const char *addr, struct mpoa_client *mpc);
  80. static void mpoad_close(struct atm_vcc *vcc);
  81. static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb);
  82. static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb);
  83. static netdev_tx_t mpc_send_packet(struct sk_buff *skb,
  84. struct net_device *dev);
  85. static int mpoa_event_listener(struct notifier_block *mpoa_notifier,
  86. unsigned long event, void *dev);
  87. static void mpc_timer_refresh(void);
  88. static void mpc_cache_check(struct timer_list *unused);
  89. static struct llc_snap_hdr llc_snap_mpoa_ctrl = {
  90. 0xaa, 0xaa, 0x03,
  91. {0x00, 0x00, 0x5e},
  92. {0x00, 0x03} /* For MPOA control PDUs */
  93. };
  94. static struct llc_snap_hdr llc_snap_mpoa_data = {
  95. 0xaa, 0xaa, 0x03,
  96. {0x00, 0x00, 0x00},
  97. {0x08, 0x00} /* This is for IP PDUs only */
  98. };
  99. static struct llc_snap_hdr llc_snap_mpoa_data_tagged = {
  100. 0xaa, 0xaa, 0x03,
  101. {0x00, 0x00, 0x00},
  102. {0x88, 0x4c} /* This is for tagged data PDUs */
  103. };
  104. static struct notifier_block mpoa_notifier = {
  105. mpoa_event_listener,
  106. NULL,
  107. 0
  108. };
  109. struct mpoa_client *mpcs = NULL; /* FIXME */
  110. static struct atm_mpoa_qos *qos_head = NULL;
  111. static DEFINE_TIMER(mpc_timer, mpc_cache_check);
  112. static struct mpoa_client *find_mpc_by_itfnum(int itf)
  113. {
  114. struct mpoa_client *mpc;
  115. mpc = mpcs; /* our global linked list */
  116. while (mpc != NULL) {
  117. if (mpc->dev_num == itf)
  118. return mpc;
  119. mpc = mpc->next;
  120. }
  121. return NULL; /* not found */
  122. }
  123. static struct mpoa_client *find_mpc_by_vcc(struct atm_vcc *vcc)
  124. {
  125. struct mpoa_client *mpc;
  126. mpc = mpcs; /* our global linked list */
  127. while (mpc != NULL) {
  128. if (mpc->mpoad_vcc == vcc)
  129. return mpc;
  130. mpc = mpc->next;
  131. }
  132. return NULL; /* not found */
  133. }
  134. static struct mpoa_client *find_mpc_by_lec(struct net_device *dev)
  135. {
  136. struct mpoa_client *mpc;
  137. mpc = mpcs; /* our global linked list */
  138. while (mpc != NULL) {
  139. if (mpc->dev == dev)
  140. return mpc;
  141. mpc = mpc->next;
  142. }
  143. return NULL; /* not found */
  144. }
  145. /*
  146. * Functions for managing QoS list
  147. */
  148. /*
  149. * Overwrites the old entry or makes a new one.
  150. */
  151. struct atm_mpoa_qos *atm_mpoa_add_qos(__be32 dst_ip, struct atm_qos *qos)
  152. {
  153. struct atm_mpoa_qos *entry;
  154. entry = atm_mpoa_search_qos(dst_ip);
  155. if (entry != NULL) {
  156. entry->qos = *qos;
  157. return entry;
  158. }
  159. entry = kmalloc(sizeof(struct atm_mpoa_qos), GFP_KERNEL);
  160. if (entry == NULL) {
  161. pr_info("mpoa: out of memory\n");
  162. return entry;
  163. }
  164. entry->ipaddr = dst_ip;
  165. entry->qos = *qos;
  166. entry->next = qos_head;
  167. qos_head = entry;
  168. return entry;
  169. }
  170. struct atm_mpoa_qos *atm_mpoa_search_qos(__be32 dst_ip)
  171. {
  172. struct atm_mpoa_qos *qos;
  173. qos = qos_head;
  174. while (qos) {
  175. if (qos->ipaddr == dst_ip)
  176. break;
  177. qos = qos->next;
  178. }
  179. return qos;
  180. }
  181. /*
  182. * Returns 0 for failure
  183. */
  184. int atm_mpoa_delete_qos(struct atm_mpoa_qos *entry)
  185. {
  186. struct atm_mpoa_qos *curr;
  187. if (entry == NULL)
  188. return 0;
  189. if (entry == qos_head) {
  190. qos_head = qos_head->next;
  191. kfree(entry);
  192. return 1;
  193. }
  194. curr = qos_head;
  195. while (curr != NULL) {
  196. if (curr->next == entry) {
  197. curr->next = entry->next;
  198. kfree(entry);
  199. return 1;
  200. }
  201. curr = curr->next;
  202. }
  203. return 0;
  204. }
  205. /* this is buggered - we need locking for qos_head */
  206. void atm_mpoa_disp_qos(struct seq_file *m)
  207. {
  208. struct atm_mpoa_qos *qos;
  209. qos = qos_head;
  210. seq_printf(m, "QoS entries for shortcuts:\n");
  211. seq_printf(m, "IP address\n TX:max_pcr pcr min_pcr max_cdv max_sdu\n RX:max_pcr pcr min_pcr max_cdv max_sdu\n");
  212. while (qos != NULL) {
  213. seq_printf(m, "%pI4\n %-7d %-7d %-7d %-7d %-7d\n %-7d %-7d %-7d %-7d %-7d\n",
  214. &qos->ipaddr,
  215. qos->qos.txtp.max_pcr,
  216. qos->qos.txtp.pcr,
  217. qos->qos.txtp.min_pcr,
  218. qos->qos.txtp.max_cdv,
  219. qos->qos.txtp.max_sdu,
  220. qos->qos.rxtp.max_pcr,
  221. qos->qos.rxtp.pcr,
  222. qos->qos.rxtp.min_pcr,
  223. qos->qos.rxtp.max_cdv,
  224. qos->qos.rxtp.max_sdu);
  225. qos = qos->next;
  226. }
  227. }
  228. static struct net_device *find_lec_by_itfnum(int itf)
  229. {
  230. struct net_device *dev;
  231. char name[IFNAMSIZ];
  232. sprintf(name, "lec%d", itf);
  233. dev = dev_get_by_name(&init_net, name);
  234. return dev;
  235. }
  236. static struct mpoa_client *alloc_mpc(void)
  237. {
  238. struct mpoa_client *mpc;
  239. mpc = kzalloc(sizeof(struct mpoa_client), GFP_KERNEL);
  240. if (mpc == NULL)
  241. return NULL;
  242. rwlock_init(&mpc->ingress_lock);
  243. rwlock_init(&mpc->egress_lock);
  244. mpc->next = mpcs;
  245. atm_mpoa_init_cache(mpc);
  246. mpc->parameters.mpc_p1 = MPC_P1;
  247. mpc->parameters.mpc_p2 = MPC_P2;
  248. memset(mpc->parameters.mpc_p3, 0, sizeof(mpc->parameters.mpc_p3));
  249. mpc->parameters.mpc_p4 = MPC_P4;
  250. mpc->parameters.mpc_p5 = MPC_P5;
  251. mpc->parameters.mpc_p6 = MPC_P6;
  252. mpcs = mpc;
  253. return mpc;
  254. }
  255. /*
  256. *
  257. * start_mpc() puts the MPC on line. All the packets destined
  258. * to the lec underneath us are now being monitored and
  259. * shortcuts will be established.
  260. *
  261. */
  262. static void start_mpc(struct mpoa_client *mpc, struct net_device *dev)
  263. {
  264. dprintk("(%s)\n", mpc->dev->name);
  265. if (!dev->netdev_ops)
  266. pr_info("(%s) not starting\n", dev->name);
  267. else {
  268. mpc->old_ops = dev->netdev_ops;
  269. mpc->new_ops = *mpc->old_ops;
  270. mpc->new_ops.ndo_start_xmit = mpc_send_packet;
  271. dev->netdev_ops = &mpc->new_ops;
  272. }
  273. }
  274. static void stop_mpc(struct mpoa_client *mpc)
  275. {
  276. struct net_device *dev = mpc->dev;
  277. dprintk("(%s)", mpc->dev->name);
  278. /* Lets not nullify lec device's dev->hard_start_xmit */
  279. if (dev->netdev_ops != &mpc->new_ops) {
  280. dprintk_cont(" mpc already stopped, not fatal\n");
  281. return;
  282. }
  283. dprintk_cont("\n");
  284. dev->netdev_ops = mpc->old_ops;
  285. mpc->old_ops = NULL;
  286. /* close_shortcuts(mpc); ??? FIXME */
  287. }
  288. static const char *mpoa_device_type_string(char type) __attribute__ ((unused));
  289. static const char *mpoa_device_type_string(char type)
  290. {
  291. switch (type) {
  292. case NON_MPOA:
  293. return "non-MPOA device";
  294. case MPS:
  295. return "MPS";
  296. case MPC:
  297. return "MPC";
  298. case MPS_AND_MPC:
  299. return "both MPS and MPC";
  300. }
  301. return "unspecified (non-MPOA) device";
  302. }
  303. /*
  304. * lec device calls this via its netdev_priv(dev)->lane2_ops
  305. * ->associate_indicator() when it sees a TLV in LE_ARP packet.
  306. * We fill in the pointer above when we see a LANE2 lec initializing
  307. * See LANE2 spec 3.1.5
  308. *
  309. * Quite a big and ugly function but when you look at it
  310. * all it does is to try to locate and parse MPOA Device
  311. * Type TLV.
  312. * We give our lec a pointer to this function and when the
  313. * lec sees a TLV it uses the pointer to call this function.
  314. *
  315. */
  316. static void lane2_assoc_ind(struct net_device *dev, const u8 *mac_addr,
  317. const u8 *tlvs, u32 sizeoftlvs)
  318. {
  319. uint32_t type;
  320. uint8_t length, mpoa_device_type, number_of_mps_macs;
  321. const uint8_t *end_of_tlvs;
  322. struct mpoa_client *mpc;
  323. mpoa_device_type = number_of_mps_macs = 0; /* silence gcc */
  324. dprintk("(%s) received TLV(s), ", dev->name);
  325. dprintk("total length of all TLVs %d\n", sizeoftlvs);
  326. mpc = find_mpc_by_lec(dev); /* Sampo-Fix: moved here from below */
  327. if (mpc == NULL) {
  328. pr_info("(%s) no mpc\n", dev->name);
  329. return;
  330. }
  331. end_of_tlvs = tlvs + sizeoftlvs;
  332. while (end_of_tlvs - tlvs >= 5) {
  333. type = ((tlvs[0] << 24) | (tlvs[1] << 16) |
  334. (tlvs[2] << 8) | tlvs[3]);
  335. length = tlvs[4];
  336. tlvs += 5;
  337. dprintk(" type 0x%x length %02x\n", type, length);
  338. if (tlvs + length > end_of_tlvs) {
  339. pr_info("TLV value extends past its buffer, aborting parse\n");
  340. return;
  341. }
  342. if (type == 0) {
  343. pr_info("mpoa: (%s) TLV type was 0, returning\n",
  344. dev->name);
  345. return;
  346. }
  347. if (type != TLV_MPOA_DEVICE_TYPE) {
  348. tlvs += length;
  349. continue; /* skip other TLVs */
  350. }
  351. mpoa_device_type = *tlvs++;
  352. number_of_mps_macs = *tlvs++;
  353. dprintk("(%s) MPOA device type '%s', ",
  354. dev->name, mpoa_device_type_string(mpoa_device_type));
  355. if (mpoa_device_type == MPS_AND_MPC &&
  356. length < (42 + number_of_mps_macs*ETH_ALEN)) { /* :) */
  357. pr_info("(%s) short MPOA Device Type TLV\n",
  358. dev->name);
  359. continue;
  360. }
  361. if ((mpoa_device_type == MPS || mpoa_device_type == MPC) &&
  362. length < 22 + number_of_mps_macs*ETH_ALEN) {
  363. pr_info("(%s) short MPOA Device Type TLV\n", dev->name);
  364. continue;
  365. }
  366. if (mpoa_device_type != MPS &&
  367. mpoa_device_type != MPS_AND_MPC) {
  368. dprintk("ignoring non-MPS device ");
  369. if (mpoa_device_type == MPC)
  370. tlvs += 20;
  371. continue; /* we are only interested in MPSs */
  372. }
  373. if (number_of_mps_macs == 0 &&
  374. mpoa_device_type == MPS_AND_MPC) {
  375. pr_info("(%s) MPS_AND_MPC has zero MACs\n", dev->name);
  376. continue; /* someone should read the spec */
  377. }
  378. dprintk_cont("this MPS has %d MAC addresses\n",
  379. number_of_mps_macs);
  380. /*
  381. * ok, now we can go and tell our daemon
  382. * the control address of MPS
  383. */
  384. send_set_mps_ctrl_addr(tlvs, mpc);
  385. tlvs = copy_macs(mpc, mac_addr, tlvs,
  386. number_of_mps_macs, mpoa_device_type);
  387. if (tlvs == NULL)
  388. return;
  389. }
  390. if (end_of_tlvs - tlvs != 0)
  391. pr_info("(%s) ignoring %zd bytes of trailing TLV garbage\n",
  392. dev->name, end_of_tlvs - tlvs);
  393. }
  394. /*
  395. * Store at least advertizing router's MAC address
  396. * plus the possible MAC address(es) to mpc->mps_macs.
  397. * For a freshly allocated MPOA client mpc->mps_macs == 0.
  398. */
  399. static const uint8_t *copy_macs(struct mpoa_client *mpc,
  400. const uint8_t *router_mac,
  401. const uint8_t *tlvs, uint8_t mps_macs,
  402. uint8_t device_type)
  403. {
  404. int num_macs;
  405. num_macs = (mps_macs > 1) ? mps_macs : 1;
  406. if (mpc->number_of_mps_macs != num_macs) { /* need to reallocate? */
  407. if (mpc->number_of_mps_macs != 0)
  408. kfree(mpc->mps_macs);
  409. mpc->number_of_mps_macs = 0;
  410. mpc->mps_macs = kmalloc_array(ETH_ALEN, num_macs, GFP_KERNEL);
  411. if (mpc->mps_macs == NULL) {
  412. pr_info("(%s) out of mem\n", mpc->dev->name);
  413. return NULL;
  414. }
  415. }
  416. ether_addr_copy(mpc->mps_macs, router_mac);
  417. tlvs += 20; if (device_type == MPS_AND_MPC) tlvs += 20;
  418. if (mps_macs > 0)
  419. memcpy(mpc->mps_macs, tlvs, mps_macs*ETH_ALEN);
  420. tlvs += mps_macs*ETH_ALEN;
  421. mpc->number_of_mps_macs = num_macs;
  422. return tlvs;
  423. }
  424. static int send_via_shortcut(struct sk_buff *skb, struct mpoa_client *mpc)
  425. {
  426. in_cache_entry *entry;
  427. struct iphdr *iph;
  428. char *buff;
  429. __be32 ipaddr = 0;
  430. static struct {
  431. struct llc_snap_hdr hdr;
  432. __be32 tag;
  433. } tagged_llc_snap_hdr = {
  434. {0xaa, 0xaa, 0x03, {0x00, 0x00, 0x00}, {0x88, 0x4c}},
  435. 0
  436. };
  437. buff = skb->data + mpc->dev->hard_header_len;
  438. iph = (struct iphdr *)buff;
  439. ipaddr = iph->daddr;
  440. ddprintk("(%s) ipaddr 0x%x\n",
  441. mpc->dev->name, ipaddr);
  442. entry = mpc->in_ops->get(ipaddr, mpc);
  443. if (entry == NULL) {
  444. entry = mpc->in_ops->add_entry(ipaddr, mpc);
  445. if (entry != NULL)
  446. mpc->in_ops->put(entry);
  447. return 1;
  448. }
  449. /* threshold not exceeded or VCC not ready */
  450. if (mpc->in_ops->cache_hit(entry, mpc) != OPEN) {
  451. ddprintk("(%s) cache_hit: returns != OPEN\n",
  452. mpc->dev->name);
  453. mpc->in_ops->put(entry);
  454. return 1;
  455. }
  456. ddprintk("(%s) using shortcut\n",
  457. mpc->dev->name);
  458. /* MPOA spec A.1.4, MPOA client must decrement IP ttl at least by one */
  459. if (iph->ttl <= 1) {
  460. ddprintk("(%s) IP ttl = %u, using LANE\n",
  461. mpc->dev->name, iph->ttl);
  462. mpc->in_ops->put(entry);
  463. return 1;
  464. }
  465. iph->ttl--;
  466. iph->check = 0;
  467. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  468. if (entry->ctrl_info.tag != 0) {
  469. ddprintk("(%s) adding tag 0x%x\n",
  470. mpc->dev->name, entry->ctrl_info.tag);
  471. tagged_llc_snap_hdr.tag = entry->ctrl_info.tag;
  472. skb_pull(skb, ETH_HLEN); /* get rid of Eth header */
  473. skb_push(skb, sizeof(tagged_llc_snap_hdr));
  474. /* add LLC/SNAP header */
  475. skb_copy_to_linear_data(skb, &tagged_llc_snap_hdr,
  476. sizeof(tagged_llc_snap_hdr));
  477. } else {
  478. skb_pull(skb, ETH_HLEN); /* get rid of Eth header */
  479. skb_push(skb, sizeof(struct llc_snap_hdr));
  480. /* add LLC/SNAP header + tag */
  481. skb_copy_to_linear_data(skb, &llc_snap_mpoa_data,
  482. sizeof(struct llc_snap_hdr));
  483. }
  484. atm_account_tx(entry->shortcut, skb);
  485. entry->shortcut->send(entry->shortcut, skb);
  486. entry->packets_fwded++;
  487. mpc->in_ops->put(entry);
  488. return 0;
  489. }
  490. /*
  491. * Probably needs some error checks and locking, not sure...
  492. */
  493. static netdev_tx_t mpc_send_packet(struct sk_buff *skb,
  494. struct net_device *dev)
  495. {
  496. struct mpoa_client *mpc;
  497. struct ethhdr *eth;
  498. int i = 0;
  499. mpc = find_mpc_by_lec(dev); /* this should NEVER fail */
  500. if (mpc == NULL) {
  501. pr_info("(%s) no MPC found\n", dev->name);
  502. goto non_ip;
  503. }
  504. eth = (struct ethhdr *)skb->data;
  505. if (eth->h_proto != htons(ETH_P_IP))
  506. goto non_ip; /* Multi-Protocol Over ATM :-) */
  507. /* Weed out funny packets (e.g., AF_PACKET or raw). */
  508. if (skb->len < ETH_HLEN + sizeof(struct iphdr))
  509. goto non_ip;
  510. skb_set_network_header(skb, ETH_HLEN);
  511. if (skb->len < ETH_HLEN + ip_hdr(skb)->ihl * 4 || ip_hdr(skb)->ihl < 5)
  512. goto non_ip;
  513. while (i < mpc->number_of_mps_macs) {
  514. if (ether_addr_equal(eth->h_dest, mpc->mps_macs + i * ETH_ALEN))
  515. if (send_via_shortcut(skb, mpc) == 0) /* try shortcut */
  516. return NETDEV_TX_OK;
  517. i++;
  518. }
  519. non_ip:
  520. return __netdev_start_xmit(mpc->old_ops, skb, dev, false);
  521. }
  522. static int atm_mpoa_vcc_attach(struct atm_vcc *vcc, void __user *arg)
  523. {
  524. int bytes_left;
  525. struct mpoa_client *mpc;
  526. struct atmmpc_ioc ioc_data;
  527. in_cache_entry *in_entry;
  528. __be32 ipaddr;
  529. bytes_left = copy_from_user(&ioc_data, arg, sizeof(struct atmmpc_ioc));
  530. if (bytes_left != 0) {
  531. pr_info("mpoa:Short read (missed %d bytes) from userland\n",
  532. bytes_left);
  533. return -EFAULT;
  534. }
  535. ipaddr = ioc_data.ipaddr;
  536. if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF)
  537. return -EINVAL;
  538. mpc = find_mpc_by_itfnum(ioc_data.dev_num);
  539. if (mpc == NULL)
  540. return -EINVAL;
  541. if (ioc_data.type == MPC_SOCKET_INGRESS) {
  542. in_entry = mpc->in_ops->get(ipaddr, mpc);
  543. if (in_entry == NULL ||
  544. in_entry->entry_state < INGRESS_RESOLVED) {
  545. pr_info("(%s) did not find RESOLVED entry from ingress cache\n",
  546. mpc->dev->name);
  547. if (in_entry != NULL)
  548. mpc->in_ops->put(in_entry);
  549. return -EINVAL;
  550. }
  551. pr_info("(%s) attaching ingress SVC, entry = %pI4\n",
  552. mpc->dev->name, &in_entry->ctrl_info.in_dst_ip);
  553. in_entry->shortcut = vcc;
  554. mpc->in_ops->put(in_entry);
  555. } else {
  556. pr_info("(%s) attaching egress SVC\n", mpc->dev->name);
  557. }
  558. vcc->proto_data = mpc->dev;
  559. vcc->push = mpc_push;
  560. return 0;
  561. }
  562. /*
  563. *
  564. */
  565. static void mpc_vcc_close(struct atm_vcc *vcc, struct net_device *dev)
  566. {
  567. struct mpoa_client *mpc;
  568. in_cache_entry *in_entry;
  569. eg_cache_entry *eg_entry;
  570. mpc = find_mpc_by_lec(dev);
  571. if (mpc == NULL) {
  572. pr_info("(%s) close for unknown MPC\n", dev->name);
  573. return;
  574. }
  575. dprintk("(%s)\n", dev->name);
  576. in_entry = mpc->in_ops->get_by_vcc(vcc, mpc);
  577. if (in_entry) {
  578. dprintk("(%s) ingress SVC closed ip = %pI4\n",
  579. mpc->dev->name, &in_entry->ctrl_info.in_dst_ip);
  580. in_entry->shortcut = NULL;
  581. mpc->in_ops->put(in_entry);
  582. }
  583. eg_entry = mpc->eg_ops->get_by_vcc(vcc, mpc);
  584. if (eg_entry) {
  585. dprintk("(%s) egress SVC closed\n", mpc->dev->name);
  586. eg_entry->shortcut = NULL;
  587. mpc->eg_ops->put(eg_entry);
  588. }
  589. if (in_entry == NULL && eg_entry == NULL)
  590. dprintk("(%s) unused vcc closed\n", dev->name);
  591. }
  592. static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb)
  593. {
  594. struct net_device *dev = (struct net_device *)vcc->proto_data;
  595. struct sk_buff *new_skb;
  596. eg_cache_entry *eg;
  597. struct mpoa_client *mpc;
  598. __be32 tag;
  599. char *tmp;
  600. ddprintk("(%s)\n", dev->name);
  601. if (skb == NULL) {
  602. dprintk("(%s) null skb, closing VCC\n", dev->name);
  603. mpc_vcc_close(vcc, dev);
  604. return;
  605. }
  606. skb->dev = dev;
  607. if (memcmp(skb->data, &llc_snap_mpoa_ctrl,
  608. sizeof(struct llc_snap_hdr)) == 0) {
  609. struct sock *sk = sk_atm(vcc);
  610. dprintk("(%s) control packet arrived\n", dev->name);
  611. /* Pass control packets to daemon */
  612. skb_queue_tail(&sk->sk_receive_queue, skb);
  613. sk->sk_data_ready(sk);
  614. return;
  615. }
  616. /* data coming over the shortcut */
  617. atm_return(vcc, skb->truesize);
  618. mpc = find_mpc_by_lec(dev);
  619. if (mpc == NULL) {
  620. pr_info("(%s) unknown MPC\n", dev->name);
  621. return;
  622. }
  623. if (memcmp(skb->data, &llc_snap_mpoa_data_tagged,
  624. sizeof(struct llc_snap_hdr)) == 0) { /* MPOA tagged data */
  625. ddprintk("(%s) tagged data packet arrived\n", dev->name);
  626. } else if (memcmp(skb->data, &llc_snap_mpoa_data,
  627. sizeof(struct llc_snap_hdr)) == 0) { /* MPOA data */
  628. pr_info("(%s) Unsupported non-tagged data packet arrived. Purging\n",
  629. dev->name);
  630. dev_kfree_skb_any(skb);
  631. return;
  632. } else {
  633. pr_info("(%s) garbage arrived, purging\n", dev->name);
  634. dev_kfree_skb_any(skb);
  635. return;
  636. }
  637. tmp = skb->data + sizeof(struct llc_snap_hdr);
  638. tag = *(__be32 *)tmp;
  639. eg = mpc->eg_ops->get_by_tag(tag, mpc);
  640. if (eg == NULL) {
  641. pr_info("mpoa: (%s) Didn't find egress cache entry, tag = %u\n",
  642. dev->name, tag);
  643. purge_egress_shortcut(vcc, NULL);
  644. dev_kfree_skb_any(skb);
  645. return;
  646. }
  647. /*
  648. * See if ingress MPC is using shortcut we opened as a return channel.
  649. * This means we have a bi-directional vcc opened by us.
  650. */
  651. if (eg->shortcut == NULL) {
  652. eg->shortcut = vcc;
  653. pr_info("(%s) egress SVC in use\n", dev->name);
  654. }
  655. skb_pull(skb, sizeof(struct llc_snap_hdr) + sizeof(tag));
  656. /* get rid of LLC/SNAP header */
  657. new_skb = skb_realloc_headroom(skb, eg->ctrl_info.DH_length);
  658. /* LLC/SNAP is shorter than MAC header :( */
  659. dev_kfree_skb_any(skb);
  660. if (new_skb == NULL) {
  661. mpc->eg_ops->put(eg);
  662. return;
  663. }
  664. skb_push(new_skb, eg->ctrl_info.DH_length); /* add MAC header */
  665. skb_copy_to_linear_data(new_skb, eg->ctrl_info.DLL_header,
  666. eg->ctrl_info.DH_length);
  667. new_skb->protocol = eth_type_trans(new_skb, dev);
  668. skb_reset_network_header(new_skb);
  669. eg->latest_ip_addr = ip_hdr(new_skb)->saddr;
  670. eg->packets_rcvd++;
  671. mpc->eg_ops->put(eg);
  672. memset(ATM_SKB(new_skb), 0, sizeof(struct atm_skb_data));
  673. netif_rx(new_skb);
  674. }
  675. static const struct atmdev_ops mpc_ops = { /* only send is required */
  676. .close = mpoad_close,
  677. .send = msg_from_mpoad
  678. };
  679. static struct atm_dev mpc_dev = {
  680. .ops = &mpc_ops,
  681. .type = "mpc",
  682. .number = 42,
  683. .lock = __SPIN_LOCK_UNLOCKED(mpc_dev.lock)
  684. /* members not explicitly initialised will be 0 */
  685. };
  686. static int atm_mpoa_mpoad_attach(struct atm_vcc *vcc, int arg)
  687. {
  688. struct mpoa_client *mpc;
  689. struct lec_priv *priv;
  690. int err;
  691. if (mpcs == NULL) {
  692. mpc_timer_refresh();
  693. /* This lets us now how our LECs are doing */
  694. err = register_netdevice_notifier(&mpoa_notifier);
  695. if (err < 0) {
  696. del_timer(&mpc_timer);
  697. return err;
  698. }
  699. }
  700. mpc = find_mpc_by_itfnum(arg);
  701. if (mpc == NULL) {
  702. dprintk("allocating new mpc for itf %d\n", arg);
  703. mpc = alloc_mpc();
  704. if (mpc == NULL)
  705. return -ENOMEM;
  706. mpc->dev_num = arg;
  707. mpc->dev = find_lec_by_itfnum(arg);
  708. /* NULL if there was no lec */
  709. }
  710. if (mpc->mpoad_vcc) {
  711. pr_info("mpoad is already present for itf %d\n", arg);
  712. return -EADDRINUSE;
  713. }
  714. if (mpc->dev) { /* check if the lec is LANE2 capable */
  715. priv = netdev_priv(mpc->dev);
  716. if (priv->lane_version < 2) {
  717. dev_put(mpc->dev);
  718. mpc->dev = NULL;
  719. } else
  720. priv->lane2_ops->associate_indicator = lane2_assoc_ind;
  721. }
  722. mpc->mpoad_vcc = vcc;
  723. vcc->dev = &mpc_dev;
  724. vcc_insert_socket(sk_atm(vcc));
  725. set_bit(ATM_VF_META, &vcc->flags);
  726. set_bit(ATM_VF_READY, &vcc->flags);
  727. if (mpc->dev) {
  728. char empty[ATM_ESA_LEN];
  729. memset(empty, 0, ATM_ESA_LEN);
  730. start_mpc(mpc, mpc->dev);
  731. /* set address if mpcd e.g. gets killed and restarted.
  732. * If we do not do it now we have to wait for the next LE_ARP
  733. */
  734. if (memcmp(mpc->mps_ctrl_addr, empty, ATM_ESA_LEN) != 0)
  735. send_set_mps_ctrl_addr(mpc->mps_ctrl_addr, mpc);
  736. }
  737. __module_get(THIS_MODULE);
  738. return arg;
  739. }
  740. static void send_set_mps_ctrl_addr(const char *addr, struct mpoa_client *mpc)
  741. {
  742. struct k_message mesg;
  743. memcpy(mpc->mps_ctrl_addr, addr, ATM_ESA_LEN);
  744. mesg.type = SET_MPS_CTRL_ADDR;
  745. memcpy(mesg.MPS_ctrl, addr, ATM_ESA_LEN);
  746. msg_to_mpoad(&mesg, mpc);
  747. }
  748. static void mpoad_close(struct atm_vcc *vcc)
  749. {
  750. struct mpoa_client *mpc;
  751. struct sk_buff *skb;
  752. mpc = find_mpc_by_vcc(vcc);
  753. if (mpc == NULL) {
  754. pr_info("did not find MPC\n");
  755. return;
  756. }
  757. if (!mpc->mpoad_vcc) {
  758. pr_info("close for non-present mpoad\n");
  759. return;
  760. }
  761. mpc->mpoad_vcc = NULL;
  762. if (mpc->dev) {
  763. struct lec_priv *priv = netdev_priv(mpc->dev);
  764. priv->lane2_ops->associate_indicator = NULL;
  765. stop_mpc(mpc);
  766. dev_put(mpc->dev);
  767. }
  768. mpc->in_ops->destroy_cache(mpc);
  769. mpc->eg_ops->destroy_cache(mpc);
  770. while ((skb = skb_dequeue(&sk_atm(vcc)->sk_receive_queue))) {
  771. atm_return(vcc, skb->truesize);
  772. kfree_skb(skb);
  773. }
  774. pr_info("(%s) going down\n",
  775. (mpc->dev) ? mpc->dev->name : "<unknown>");
  776. module_put(THIS_MODULE);
  777. }
  778. /*
  779. *
  780. */
  781. static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb)
  782. {
  783. struct mpoa_client *mpc = find_mpc_by_vcc(vcc);
  784. struct k_message *mesg = (struct k_message *)skb->data;
  785. WARN_ON(refcount_sub_and_test(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc));
  786. if (mpc == NULL) {
  787. pr_info("no mpc found\n");
  788. return 0;
  789. }
  790. dprintk("(%s)", mpc->dev ? mpc->dev->name : "<unknown>");
  791. switch (mesg->type) {
  792. case MPOA_RES_REPLY_RCVD:
  793. dprintk_cont("mpoa_res_reply_rcvd\n");
  794. MPOA_res_reply_rcvd(mesg, mpc);
  795. break;
  796. case MPOA_TRIGGER_RCVD:
  797. dprintk_cont("mpoa_trigger_rcvd\n");
  798. MPOA_trigger_rcvd(mesg, mpc);
  799. break;
  800. case INGRESS_PURGE_RCVD:
  801. dprintk_cont("nhrp_purge_rcvd\n");
  802. ingress_purge_rcvd(mesg, mpc);
  803. break;
  804. case EGRESS_PURGE_RCVD:
  805. dprintk_cont("egress_purge_reply_rcvd\n");
  806. egress_purge_rcvd(mesg, mpc);
  807. break;
  808. case MPS_DEATH:
  809. dprintk_cont("mps_death\n");
  810. mps_death(mesg, mpc);
  811. break;
  812. case CACHE_IMPOS_RCVD:
  813. dprintk_cont("cache_impos_rcvd\n");
  814. MPOA_cache_impos_rcvd(mesg, mpc);
  815. break;
  816. case SET_MPC_CTRL_ADDR:
  817. dprintk_cont("set_mpc_ctrl_addr\n");
  818. set_mpc_ctrl_addr_rcvd(mesg, mpc);
  819. break;
  820. case SET_MPS_MAC_ADDR:
  821. dprintk_cont("set_mps_mac_addr\n");
  822. set_mps_mac_addr_rcvd(mesg, mpc);
  823. break;
  824. case CLEAN_UP_AND_EXIT:
  825. dprintk_cont("clean_up_and_exit\n");
  826. clean_up(mesg, mpc, DIE);
  827. break;
  828. case RELOAD:
  829. dprintk_cont("reload\n");
  830. clean_up(mesg, mpc, RELOAD);
  831. break;
  832. case SET_MPC_PARAMS:
  833. dprintk_cont("set_mpc_params\n");
  834. mpc->parameters = mesg->content.params;
  835. break;
  836. default:
  837. dprintk_cont("unknown message %d\n", mesg->type);
  838. break;
  839. }
  840. kfree_skb(skb);
  841. return 0;
  842. }
  843. /* Remember that this function may not do things that sleep */
  844. int msg_to_mpoad(struct k_message *mesg, struct mpoa_client *mpc)
  845. {
  846. struct sk_buff *skb;
  847. struct sock *sk;
  848. if (mpc == NULL || !mpc->mpoad_vcc) {
  849. pr_info("mesg %d to a non-existent mpoad\n", mesg->type);
  850. return -ENXIO;
  851. }
  852. skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
  853. if (skb == NULL)
  854. return -ENOMEM;
  855. skb_put(skb, sizeof(struct k_message));
  856. skb_copy_to_linear_data(skb, mesg, sizeof(*mesg));
  857. atm_force_charge(mpc->mpoad_vcc, skb->truesize);
  858. sk = sk_atm(mpc->mpoad_vcc);
  859. skb_queue_tail(&sk->sk_receive_queue, skb);
  860. sk->sk_data_ready(sk);
  861. return 0;
  862. }
  863. static int mpoa_event_listener(struct notifier_block *mpoa_notifier,
  864. unsigned long event, void *ptr)
  865. {
  866. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  867. struct mpoa_client *mpc;
  868. struct lec_priv *priv;
  869. if (!net_eq(dev_net(dev), &init_net))
  870. return NOTIFY_DONE;
  871. if (strncmp(dev->name, "lec", 3))
  872. return NOTIFY_DONE; /* we are only interested in lec:s */
  873. switch (event) {
  874. case NETDEV_REGISTER: /* a new lec device was allocated */
  875. priv = netdev_priv(dev);
  876. if (priv->lane_version < 2)
  877. break;
  878. priv->lane2_ops->associate_indicator = lane2_assoc_ind;
  879. mpc = find_mpc_by_itfnum(priv->itfnum);
  880. if (mpc == NULL) {
  881. dprintk("allocating new mpc for %s\n", dev->name);
  882. mpc = alloc_mpc();
  883. if (mpc == NULL) {
  884. pr_info("no new mpc");
  885. break;
  886. }
  887. }
  888. mpc->dev_num = priv->itfnum;
  889. mpc->dev = dev;
  890. dev_hold(dev);
  891. dprintk("(%s) was initialized\n", dev->name);
  892. break;
  893. case NETDEV_UNREGISTER:
  894. /* the lec device was deallocated */
  895. mpc = find_mpc_by_lec(dev);
  896. if (mpc == NULL)
  897. break;
  898. dprintk("device (%s) was deallocated\n", dev->name);
  899. stop_mpc(mpc);
  900. dev_put(mpc->dev);
  901. mpc->dev = NULL;
  902. break;
  903. case NETDEV_UP:
  904. /* the dev was ifconfig'ed up */
  905. mpc = find_mpc_by_lec(dev);
  906. if (mpc == NULL)
  907. break;
  908. if (mpc->mpoad_vcc != NULL)
  909. start_mpc(mpc, dev);
  910. break;
  911. case NETDEV_DOWN:
  912. /* the dev was ifconfig'ed down */
  913. /* this means that the flow of packets from the
  914. * upper layer stops
  915. */
  916. mpc = find_mpc_by_lec(dev);
  917. if (mpc == NULL)
  918. break;
  919. if (mpc->mpoad_vcc != NULL)
  920. stop_mpc(mpc);
  921. break;
  922. case NETDEV_REBOOT:
  923. case NETDEV_CHANGE:
  924. case NETDEV_CHANGEMTU:
  925. case NETDEV_CHANGEADDR:
  926. case NETDEV_GOING_DOWN:
  927. break;
  928. default:
  929. break;
  930. }
  931. return NOTIFY_DONE;
  932. }
  933. /*
  934. * Functions which are called after a message is received from mpcd.
  935. * Msg is reused on purpose.
  936. */
  937. static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc)
  938. {
  939. __be32 dst_ip = msg->content.in_info.in_dst_ip;
  940. in_cache_entry *entry;
  941. entry = mpc->in_ops->get(dst_ip, mpc);
  942. if (entry == NULL) {
  943. entry = mpc->in_ops->add_entry(dst_ip, mpc);
  944. entry->entry_state = INGRESS_RESOLVING;
  945. msg->type = SND_MPOA_RES_RQST;
  946. msg->content.in_info = entry->ctrl_info;
  947. msg_to_mpoad(msg, mpc);
  948. entry->reply_wait = ktime_get_seconds();
  949. mpc->in_ops->put(entry);
  950. return;
  951. }
  952. if (entry->entry_state == INGRESS_INVALID) {
  953. entry->entry_state = INGRESS_RESOLVING;
  954. msg->type = SND_MPOA_RES_RQST;
  955. msg->content.in_info = entry->ctrl_info;
  956. msg_to_mpoad(msg, mpc);
  957. entry->reply_wait = ktime_get_seconds();
  958. mpc->in_ops->put(entry);
  959. return;
  960. }
  961. pr_info("(%s) entry already in resolving state\n",
  962. (mpc->dev) ? mpc->dev->name : "<unknown>");
  963. mpc->in_ops->put(entry);
  964. }
  965. /*
  966. * Things get complicated because we have to check if there's an egress
  967. * shortcut with suitable traffic parameters we could use.
  968. */
  969. static void check_qos_and_open_shortcut(struct k_message *msg,
  970. struct mpoa_client *client,
  971. in_cache_entry *entry)
  972. {
  973. __be32 dst_ip = msg->content.in_info.in_dst_ip;
  974. struct atm_mpoa_qos *qos = atm_mpoa_search_qos(dst_ip);
  975. eg_cache_entry *eg_entry = client->eg_ops->get_by_src_ip(dst_ip, client);
  976. if (eg_entry && eg_entry->shortcut) {
  977. if (eg_entry->shortcut->qos.txtp.traffic_class &
  978. msg->qos.txtp.traffic_class &
  979. (qos ? qos->qos.txtp.traffic_class : ATM_UBR | ATM_CBR)) {
  980. if (eg_entry->shortcut->qos.txtp.traffic_class == ATM_UBR)
  981. entry->shortcut = eg_entry->shortcut;
  982. else if (eg_entry->shortcut->qos.txtp.max_pcr > 0)
  983. entry->shortcut = eg_entry->shortcut;
  984. }
  985. if (entry->shortcut) {
  986. dprintk("(%s) using egress SVC to reach %pI4\n",
  987. client->dev->name, &dst_ip);
  988. client->eg_ops->put(eg_entry);
  989. return;
  990. }
  991. }
  992. if (eg_entry != NULL)
  993. client->eg_ops->put(eg_entry);
  994. /* No luck in the egress cache we must open an ingress SVC */
  995. msg->type = OPEN_INGRESS_SVC;
  996. if (qos &&
  997. (qos->qos.txtp.traffic_class == msg->qos.txtp.traffic_class)) {
  998. msg->qos = qos->qos;
  999. pr_info("(%s) trying to get a CBR shortcut\n",
  1000. client->dev->name);
  1001. } else
  1002. memset(&msg->qos, 0, sizeof(struct atm_qos));
  1003. msg_to_mpoad(msg, client);
  1004. }
  1005. static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc)
  1006. {
  1007. __be32 dst_ip = msg->content.in_info.in_dst_ip;
  1008. in_cache_entry *entry = mpc->in_ops->get(dst_ip, mpc);
  1009. dprintk("(%s) ip %pI4\n",
  1010. mpc->dev->name, &dst_ip);
  1011. ddprintk("(%s) entry = %p",
  1012. mpc->dev->name, entry);
  1013. if (entry == NULL) {
  1014. pr_info("(%s) ARGH, received res. reply for an entry that doesn't exist.\n",
  1015. mpc->dev->name);
  1016. return;
  1017. }
  1018. ddprintk_cont(" entry_state = %d ", entry->entry_state);
  1019. if (entry->entry_state == INGRESS_RESOLVED) {
  1020. pr_info("(%s) RESOLVED entry!\n", mpc->dev->name);
  1021. mpc->in_ops->put(entry);
  1022. return;
  1023. }
  1024. entry->ctrl_info = msg->content.in_info;
  1025. entry->time = ktime_get_seconds();
  1026. /* Used in refreshing func from now on */
  1027. entry->reply_wait = ktime_get_seconds();
  1028. entry->refresh_time = 0;
  1029. ddprintk_cont("entry->shortcut = %p\n", entry->shortcut);
  1030. if (entry->entry_state == INGRESS_RESOLVING &&
  1031. entry->shortcut != NULL) {
  1032. entry->entry_state = INGRESS_RESOLVED;
  1033. mpc->in_ops->put(entry);
  1034. return; /* Shortcut already open... */
  1035. }
  1036. if (entry->shortcut != NULL) {
  1037. pr_info("(%s) entry->shortcut != NULL, impossible!\n",
  1038. mpc->dev->name);
  1039. mpc->in_ops->put(entry);
  1040. return;
  1041. }
  1042. check_qos_and_open_shortcut(msg, mpc, entry);
  1043. entry->entry_state = INGRESS_RESOLVED;
  1044. mpc->in_ops->put(entry);
  1045. return;
  1046. }
  1047. static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
  1048. {
  1049. __be32 dst_ip = msg->content.in_info.in_dst_ip;
  1050. __be32 mask = msg->ip_mask;
  1051. in_cache_entry *entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
  1052. if (entry == NULL) {
  1053. pr_info("(%s) purge for a non-existing entry, ip = %pI4\n",
  1054. mpc->dev->name, &dst_ip);
  1055. return;
  1056. }
  1057. do {
  1058. dprintk("(%s) removing an ingress entry, ip = %pI4\n",
  1059. mpc->dev->name, &dst_ip);
  1060. write_lock_bh(&mpc->ingress_lock);
  1061. mpc->in_ops->remove_entry(entry, mpc);
  1062. write_unlock_bh(&mpc->ingress_lock);
  1063. mpc->in_ops->put(entry);
  1064. entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
  1065. } while (entry != NULL);
  1066. }
  1067. static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
  1068. {
  1069. __be32 cache_id = msg->content.eg_info.cache_id;
  1070. eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(cache_id, mpc);
  1071. if (entry == NULL) {
  1072. dprintk("(%s) purge for a non-existing entry\n",
  1073. mpc->dev->name);
  1074. return;
  1075. }
  1076. write_lock_irq(&mpc->egress_lock);
  1077. mpc->eg_ops->remove_entry(entry, mpc);
  1078. write_unlock_irq(&mpc->egress_lock);
  1079. mpc->eg_ops->put(entry);
  1080. }
  1081. static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry)
  1082. {
  1083. struct sock *sk;
  1084. struct k_message *purge_msg;
  1085. struct sk_buff *skb;
  1086. dprintk("entering\n");
  1087. if (vcc == NULL) {
  1088. pr_info("vcc == NULL\n");
  1089. return;
  1090. }
  1091. skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
  1092. if (skb == NULL) {
  1093. pr_info("out of memory\n");
  1094. return;
  1095. }
  1096. skb_put(skb, sizeof(struct k_message));
  1097. memset(skb->data, 0, sizeof(struct k_message));
  1098. purge_msg = (struct k_message *)skb->data;
  1099. purge_msg->type = DATA_PLANE_PURGE;
  1100. if (entry != NULL)
  1101. purge_msg->content.eg_info = entry->ctrl_info;
  1102. atm_force_charge(vcc, skb->truesize);
  1103. sk = sk_atm(vcc);
  1104. skb_queue_tail(&sk->sk_receive_queue, skb);
  1105. sk->sk_data_ready(sk);
  1106. dprintk("exiting\n");
  1107. }
  1108. /*
  1109. * Our MPS died. Tell our daemon to send NHRP data plane purge to each
  1110. * of the egress shortcuts we have.
  1111. */
  1112. static void mps_death(struct k_message *msg, struct mpoa_client *mpc)
  1113. {
  1114. eg_cache_entry *entry;
  1115. dprintk("(%s)\n", mpc->dev->name);
  1116. if (memcmp(msg->MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN)) {
  1117. pr_info("(%s) wrong MPS\n", mpc->dev->name);
  1118. return;
  1119. }
  1120. /* FIXME: This knows too much of the cache structure */
  1121. read_lock_irq(&mpc->egress_lock);
  1122. entry = mpc->eg_cache;
  1123. while (entry != NULL) {
  1124. purge_egress_shortcut(entry->shortcut, entry);
  1125. entry = entry->next;
  1126. }
  1127. read_unlock_irq(&mpc->egress_lock);
  1128. mpc->in_ops->destroy_cache(mpc);
  1129. mpc->eg_ops->destroy_cache(mpc);
  1130. }
  1131. static void MPOA_cache_impos_rcvd(struct k_message *msg,
  1132. struct mpoa_client *mpc)
  1133. {
  1134. uint16_t holding_time;
  1135. eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(msg->content.eg_info.cache_id, mpc);
  1136. holding_time = msg->content.eg_info.holding_time;
  1137. dprintk("(%s) entry = %p, holding_time = %u\n",
  1138. mpc->dev->name, entry, holding_time);
  1139. if (entry == NULL && holding_time) {
  1140. entry = mpc->eg_ops->add_entry(msg, mpc);
  1141. mpc->eg_ops->put(entry);
  1142. return;
  1143. }
  1144. if (holding_time) {
  1145. mpc->eg_ops->update(entry, holding_time);
  1146. return;
  1147. }
  1148. write_lock_irq(&mpc->egress_lock);
  1149. mpc->eg_ops->remove_entry(entry, mpc);
  1150. write_unlock_irq(&mpc->egress_lock);
  1151. mpc->eg_ops->put(entry);
  1152. }
  1153. static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg,
  1154. struct mpoa_client *mpc)
  1155. {
  1156. struct lec_priv *priv;
  1157. int i, retval ;
  1158. uint8_t tlv[4 + 1 + 1 + 1 + ATM_ESA_LEN];
  1159. tlv[0] = 00; tlv[1] = 0xa0; tlv[2] = 0x3e; tlv[3] = 0x2a; /* type */
  1160. tlv[4] = 1 + 1 + ATM_ESA_LEN; /* length */
  1161. tlv[5] = 0x02; /* MPOA client */
  1162. tlv[6] = 0x00; /* number of MPS MAC addresses */
  1163. memcpy(&tlv[7], mesg->MPS_ctrl, ATM_ESA_LEN); /* MPC ctrl ATM addr */
  1164. memcpy(mpc->our_ctrl_addr, mesg->MPS_ctrl, ATM_ESA_LEN);
  1165. dprintk("(%s) setting MPC ctrl ATM address to",
  1166. mpc->dev ? mpc->dev->name : "<unknown>");
  1167. for (i = 7; i < sizeof(tlv); i++)
  1168. dprintk_cont(" %02x", tlv[i]);
  1169. dprintk_cont("\n");
  1170. if (mpc->dev) {
  1171. priv = netdev_priv(mpc->dev);
  1172. retval = priv->lane2_ops->associate_req(mpc->dev,
  1173. mpc->dev->dev_addr,
  1174. tlv, sizeof(tlv));
  1175. if (retval == 0)
  1176. pr_info("(%s) MPOA device type TLV association failed\n",
  1177. mpc->dev->name);
  1178. retval = priv->lane2_ops->resolve(mpc->dev, NULL, 1, NULL, NULL);
  1179. if (retval < 0)
  1180. pr_info("(%s) targetless LE_ARP request failed\n",
  1181. mpc->dev->name);
  1182. }
  1183. }
  1184. static void set_mps_mac_addr_rcvd(struct k_message *msg,
  1185. struct mpoa_client *client)
  1186. {
  1187. if (client->number_of_mps_macs)
  1188. kfree(client->mps_macs);
  1189. client->number_of_mps_macs = 0;
  1190. client->mps_macs = kmemdup(msg->MPS_ctrl, ETH_ALEN, GFP_KERNEL);
  1191. if (client->mps_macs == NULL) {
  1192. pr_info("out of memory\n");
  1193. return;
  1194. }
  1195. client->number_of_mps_macs = 1;
  1196. }
  1197. /*
  1198. * purge egress cache and tell daemon to 'action' (DIE, RELOAD)
  1199. */
  1200. static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action)
  1201. {
  1202. eg_cache_entry *entry;
  1203. msg->type = SND_EGRESS_PURGE;
  1204. /* FIXME: This knows too much of the cache structure */
  1205. read_lock_irq(&mpc->egress_lock);
  1206. entry = mpc->eg_cache;
  1207. while (entry != NULL) {
  1208. msg->content.eg_info = entry->ctrl_info;
  1209. dprintk("cache_id %u\n", entry->ctrl_info.cache_id);
  1210. msg_to_mpoad(msg, mpc);
  1211. entry = entry->next;
  1212. }
  1213. read_unlock_irq(&mpc->egress_lock);
  1214. msg->type = action;
  1215. msg_to_mpoad(msg, mpc);
  1216. }
  1217. static unsigned long checking_time;
  1218. static void mpc_timer_refresh(void)
  1219. {
  1220. mpc_timer.expires = jiffies + (MPC_P2 * HZ);
  1221. checking_time = mpc_timer.expires;
  1222. add_timer(&mpc_timer);
  1223. }
  1224. static void mpc_cache_check(struct timer_list *unused)
  1225. {
  1226. struct mpoa_client *mpc = mpcs;
  1227. static unsigned long previous_resolving_check_time;
  1228. static unsigned long previous_refresh_time;
  1229. while (mpc != NULL) {
  1230. mpc->in_ops->clear_count(mpc);
  1231. mpc->eg_ops->clear_expired(mpc);
  1232. if (checking_time - previous_resolving_check_time >
  1233. mpc->parameters.mpc_p4 * HZ) {
  1234. mpc->in_ops->check_resolving(mpc);
  1235. previous_resolving_check_time = checking_time;
  1236. }
  1237. if (checking_time - previous_refresh_time >
  1238. mpc->parameters.mpc_p5 * HZ) {
  1239. mpc->in_ops->refresh(mpc);
  1240. previous_refresh_time = checking_time;
  1241. }
  1242. mpc = mpc->next;
  1243. }
  1244. mpc_timer_refresh();
  1245. }
  1246. static int atm_mpoa_ioctl(struct socket *sock, unsigned int cmd,
  1247. unsigned long arg)
  1248. {
  1249. int err = 0;
  1250. struct atm_vcc *vcc = ATM_SD(sock);
  1251. if (cmd != ATMMPC_CTRL && cmd != ATMMPC_DATA)
  1252. return -ENOIOCTLCMD;
  1253. if (!capable(CAP_NET_ADMIN))
  1254. return -EPERM;
  1255. switch (cmd) {
  1256. case ATMMPC_CTRL:
  1257. err = atm_mpoa_mpoad_attach(vcc, (int)arg);
  1258. if (err >= 0)
  1259. sock->state = SS_CONNECTED;
  1260. break;
  1261. case ATMMPC_DATA:
  1262. err = atm_mpoa_vcc_attach(vcc, (void __user *)arg);
  1263. break;
  1264. default:
  1265. break;
  1266. }
  1267. return err;
  1268. }
  1269. static struct atm_ioctl atm_ioctl_ops = {
  1270. .owner = THIS_MODULE,
  1271. .ioctl = atm_mpoa_ioctl,
  1272. };
  1273. static __init int atm_mpoa_init(void)
  1274. {
  1275. register_atm_ioctl(&atm_ioctl_ops);
  1276. if (mpc_proc_init() != 0)
  1277. pr_info("failed to initialize /proc/mpoa\n");
  1278. pr_info("mpc.c: initialized\n");
  1279. return 0;
  1280. }
  1281. static void __exit atm_mpoa_cleanup(void)
  1282. {
  1283. struct mpoa_client *mpc, *tmp;
  1284. struct atm_mpoa_qos *qos, *nextqos;
  1285. struct lec_priv *priv;
  1286. mpc_proc_clean();
  1287. del_timer_sync(&mpc_timer);
  1288. unregister_netdevice_notifier(&mpoa_notifier);
  1289. deregister_atm_ioctl(&atm_ioctl_ops);
  1290. mpc = mpcs;
  1291. mpcs = NULL;
  1292. while (mpc != NULL) {
  1293. tmp = mpc->next;
  1294. if (mpc->dev != NULL) {
  1295. stop_mpc(mpc);
  1296. priv = netdev_priv(mpc->dev);
  1297. if (priv->lane2_ops != NULL)
  1298. priv->lane2_ops->associate_indicator = NULL;
  1299. }
  1300. ddprintk("about to clear caches\n");
  1301. mpc->in_ops->destroy_cache(mpc);
  1302. mpc->eg_ops->destroy_cache(mpc);
  1303. ddprintk("caches cleared\n");
  1304. kfree(mpc->mps_macs);
  1305. memset(mpc, 0, sizeof(struct mpoa_client));
  1306. ddprintk("about to kfree %p\n", mpc);
  1307. kfree(mpc);
  1308. ddprintk("next mpc is at %p\n", tmp);
  1309. mpc = tmp;
  1310. }
  1311. qos = qos_head;
  1312. qos_head = NULL;
  1313. while (qos != NULL) {
  1314. nextqos = qos->next;
  1315. dprintk("freeing qos entry %p\n", qos);
  1316. kfree(qos);
  1317. qos = nextqos;
  1318. }
  1319. }
  1320. module_init(atm_mpoa_init);
  1321. module_exit(atm_mpoa_cleanup);
  1322. MODULE_LICENSE("GPL");