mpc.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  1. #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
  2. #include <linux/kernel.h>
  3. #include <linux/string.h>
  4. #include <linux/slab.h>
  5. #include <linux/timer.h>
  6. #include <linux/init.h>
  7. #include <linux/bitops.h>
  8. #include <linux/capability.h>
  9. #include <linux/seq_file.h>
  10. /* We are an ethernet device */
  11. #include <linux/if_ether.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/etherdevice.h>
  14. #include <net/sock.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/ip.h>
  17. #include <linux/uaccess.h>
  18. #include <asm/byteorder.h>
  19. #include <net/checksum.h> /* for ip_fast_csum() */
  20. #include <net/arp.h>
  21. #include <net/dst.h>
  22. #include <linux/proc_fs.h>
  23. /* And atm device */
  24. #include <linux/atmdev.h>
  25. #include <linux/atmlec.h>
  26. #include <linux/atmmpc.h>
  27. /* Modular too */
  28. #include <linux/module.h>
  29. #include "lec.h"
  30. #include "mpc.h"
  31. #include "resources.h"
  32. /*
  33. * mpc.c: Implementation of MPOA client kernel part
  34. */
  35. #if 0
  36. #define dprintk(format, args...) \
  37. printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args)
  38. #define dprintk_cont(format, args...) printk(KERN_CONT format, ##args)
  39. #else
  40. #define dprintk(format, args...) \
  41. do { if (0) \
  42. printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args);\
  43. } while (0)
  44. #define dprintk_cont(format, args...) \
  45. do { if (0) printk(KERN_CONT format, ##args); } while (0)
  46. #endif
  47. #if 0
  48. #define ddprintk(format, args...) \
  49. printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args)
  50. #define ddprintk_cont(format, args...) printk(KERN_CONT format, ##args)
  51. #else
  52. #define ddprintk(format, args...) \
  53. do { if (0) \
  54. printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args);\
  55. } while (0)
  56. #define ddprintk_cont(format, args...) \
  57. do { if (0) printk(KERN_CONT format, ##args); } while (0)
  58. #endif
  59. #define MPOA_TAG_LEN 4
  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(unsigned long checking_time);
  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, NULL, 0, 0);
  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. return;
  394. }
  395. /*
  396. * Store at least advertizing router's MAC address
  397. * plus the possible MAC address(es) to mpc->mps_macs.
  398. * For a freshly allocated MPOA client mpc->mps_macs == 0.
  399. */
  400. static const uint8_t *copy_macs(struct mpoa_client *mpc,
  401. const uint8_t *router_mac,
  402. const uint8_t *tlvs, uint8_t mps_macs,
  403. uint8_t device_type)
  404. {
  405. int num_macs;
  406. num_macs = (mps_macs > 1) ? mps_macs : 1;
  407. if (mpc->number_of_mps_macs != num_macs) { /* need to reallocate? */
  408. if (mpc->number_of_mps_macs != 0)
  409. kfree(mpc->mps_macs);
  410. mpc->number_of_mps_macs = 0;
  411. mpc->mps_macs = kmalloc(num_macs * ETH_ALEN, GFP_KERNEL);
  412. if (mpc->mps_macs == NULL) {
  413. pr_info("(%s) out of mem\n", mpc->dev->name);
  414. return NULL;
  415. }
  416. }
  417. memcpy(mpc->mps_macs, router_mac, ETH_ALEN);
  418. tlvs += 20; if (device_type == MPS_AND_MPC) tlvs += 20;
  419. if (mps_macs > 0)
  420. memcpy(mpc->mps_macs, tlvs, mps_macs*ETH_ALEN);
  421. tlvs += mps_macs*ETH_ALEN;
  422. mpc->number_of_mps_macs = num_macs;
  423. return tlvs;
  424. }
  425. static int send_via_shortcut(struct sk_buff *skb, struct mpoa_client *mpc)
  426. {
  427. in_cache_entry *entry;
  428. struct iphdr *iph;
  429. char *buff;
  430. __be32 ipaddr = 0;
  431. static struct {
  432. struct llc_snap_hdr hdr;
  433. __be32 tag;
  434. } tagged_llc_snap_hdr = {
  435. {0xaa, 0xaa, 0x03, {0x00, 0x00, 0x00}, {0x88, 0x4c}},
  436. 0
  437. };
  438. buff = skb->data + mpc->dev->hard_header_len;
  439. iph = (struct iphdr *)buff;
  440. ipaddr = iph->daddr;
  441. ddprintk("(%s) ipaddr 0x%x\n",
  442. mpc->dev->name, ipaddr);
  443. entry = mpc->in_ops->get(ipaddr, mpc);
  444. if (entry == NULL) {
  445. entry = mpc->in_ops->add_entry(ipaddr, mpc);
  446. if (entry != NULL)
  447. mpc->in_ops->put(entry);
  448. return 1;
  449. }
  450. /* threshold not exceeded or VCC not ready */
  451. if (mpc->in_ops->cache_hit(entry, mpc) != OPEN) {
  452. ddprintk("(%s) cache_hit: returns != OPEN\n",
  453. mpc->dev->name);
  454. mpc->in_ops->put(entry);
  455. return 1;
  456. }
  457. ddprintk("(%s) using shortcut\n",
  458. mpc->dev->name);
  459. /* MPOA spec A.1.4, MPOA client must decrement IP ttl at least by one */
  460. if (iph->ttl <= 1) {
  461. ddprintk("(%s) IP ttl = %u, using LANE\n",
  462. mpc->dev->name, iph->ttl);
  463. mpc->in_ops->put(entry);
  464. return 1;
  465. }
  466. iph->ttl--;
  467. iph->check = 0;
  468. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  469. if (entry->ctrl_info.tag != 0) {
  470. ddprintk("(%s) adding tag 0x%x\n",
  471. mpc->dev->name, entry->ctrl_info.tag);
  472. tagged_llc_snap_hdr.tag = entry->ctrl_info.tag;
  473. skb_pull(skb, ETH_HLEN); /* get rid of Eth header */
  474. skb_push(skb, sizeof(tagged_llc_snap_hdr));
  475. /* add LLC/SNAP header */
  476. skb_copy_to_linear_data(skb, &tagged_llc_snap_hdr,
  477. sizeof(tagged_llc_snap_hdr));
  478. } else {
  479. skb_pull(skb, ETH_HLEN); /* get rid of Eth header */
  480. skb_push(skb, sizeof(struct llc_snap_hdr));
  481. /* add LLC/SNAP header + tag */
  482. skb_copy_to_linear_data(skb, &llc_snap_mpoa_data,
  483. sizeof(struct llc_snap_hdr));
  484. }
  485. atomic_add(skb->truesize, &sk_atm(entry->shortcut)->sk_wmem_alloc);
  486. ATM_SKB(skb)->atm_options = entry->shortcut->atm_options;
  487. entry->shortcut->send(entry->shortcut, skb);
  488. entry->packets_fwded++;
  489. mpc->in_ops->put(entry);
  490. return 0;
  491. }
  492. /*
  493. * Probably needs some error checks and locking, not sure...
  494. */
  495. static netdev_tx_t mpc_send_packet(struct sk_buff *skb,
  496. struct net_device *dev)
  497. {
  498. struct mpoa_client *mpc;
  499. struct ethhdr *eth;
  500. int i = 0;
  501. mpc = find_mpc_by_lec(dev); /* this should NEVER fail */
  502. if (mpc == NULL) {
  503. pr_info("(%s) no MPC found\n", dev->name);
  504. goto non_ip;
  505. }
  506. eth = (struct ethhdr *)skb->data;
  507. if (eth->h_proto != htons(ETH_P_IP))
  508. goto non_ip; /* Multi-Protocol Over ATM :-) */
  509. /* Weed out funny packets (e.g., AF_PACKET or raw). */
  510. if (skb->len < ETH_HLEN + sizeof(struct iphdr))
  511. goto non_ip;
  512. skb_set_network_header(skb, ETH_HLEN);
  513. if (skb->len < ETH_HLEN + ip_hdr(skb)->ihl * 4 || ip_hdr(skb)->ihl < 5)
  514. goto non_ip;
  515. while (i < mpc->number_of_mps_macs) {
  516. if (!compare_ether_addr(eth->h_dest,
  517. (mpc->mps_macs + i*ETH_ALEN)))
  518. if (send_via_shortcut(skb, mpc) == 0) /* try shortcut */
  519. return NETDEV_TX_OK;
  520. i++;
  521. }
  522. non_ip:
  523. return mpc->old_ops->ndo_start_xmit(skb, dev);
  524. }
  525. static int atm_mpoa_vcc_attach(struct atm_vcc *vcc, void __user *arg)
  526. {
  527. int bytes_left;
  528. struct mpoa_client *mpc;
  529. struct atmmpc_ioc ioc_data;
  530. in_cache_entry *in_entry;
  531. __be32 ipaddr;
  532. bytes_left = copy_from_user(&ioc_data, arg, sizeof(struct atmmpc_ioc));
  533. if (bytes_left != 0) {
  534. pr_info("mpoa:Short read (missed %d bytes) from userland\n",
  535. bytes_left);
  536. return -EFAULT;
  537. }
  538. ipaddr = ioc_data.ipaddr;
  539. if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF)
  540. return -EINVAL;
  541. mpc = find_mpc_by_itfnum(ioc_data.dev_num);
  542. if (mpc == NULL)
  543. return -EINVAL;
  544. if (ioc_data.type == MPC_SOCKET_INGRESS) {
  545. in_entry = mpc->in_ops->get(ipaddr, mpc);
  546. if (in_entry == NULL ||
  547. in_entry->entry_state < INGRESS_RESOLVED) {
  548. pr_info("(%s) did not find RESOLVED entry from ingress cache\n",
  549. mpc->dev->name);
  550. if (in_entry != NULL)
  551. mpc->in_ops->put(in_entry);
  552. return -EINVAL;
  553. }
  554. pr_info("(%s) attaching ingress SVC, entry = %pI4\n",
  555. mpc->dev->name, &in_entry->ctrl_info.in_dst_ip);
  556. in_entry->shortcut = vcc;
  557. mpc->in_ops->put(in_entry);
  558. } else {
  559. pr_info("(%s) attaching egress SVC\n", mpc->dev->name);
  560. }
  561. vcc->proto_data = mpc->dev;
  562. vcc->push = mpc_push;
  563. return 0;
  564. }
  565. /*
  566. *
  567. */
  568. static void mpc_vcc_close(struct atm_vcc *vcc, struct net_device *dev)
  569. {
  570. struct mpoa_client *mpc;
  571. in_cache_entry *in_entry;
  572. eg_cache_entry *eg_entry;
  573. mpc = find_mpc_by_lec(dev);
  574. if (mpc == NULL) {
  575. pr_info("(%s) close for unknown MPC\n", dev->name);
  576. return;
  577. }
  578. dprintk("(%s)\n", dev->name);
  579. in_entry = mpc->in_ops->get_by_vcc(vcc, mpc);
  580. if (in_entry) {
  581. dprintk("(%s) ingress SVC closed ip = %pI4\n",
  582. mpc->dev->name, &in_entry->ctrl_info.in_dst_ip);
  583. in_entry->shortcut = NULL;
  584. mpc->in_ops->put(in_entry);
  585. }
  586. eg_entry = mpc->eg_ops->get_by_vcc(vcc, mpc);
  587. if (eg_entry) {
  588. dprintk("(%s) egress SVC closed\n", mpc->dev->name);
  589. eg_entry->shortcut = NULL;
  590. mpc->eg_ops->put(eg_entry);
  591. }
  592. if (in_entry == NULL && eg_entry == NULL)
  593. dprintk("(%s) unused vcc closed\n", dev->name);
  594. return;
  595. }
  596. static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb)
  597. {
  598. struct net_device *dev = (struct net_device *)vcc->proto_data;
  599. struct sk_buff *new_skb;
  600. eg_cache_entry *eg;
  601. struct mpoa_client *mpc;
  602. __be32 tag;
  603. char *tmp;
  604. ddprintk("(%s)\n", dev->name);
  605. if (skb == NULL) {
  606. dprintk("(%s) null skb, closing VCC\n", dev->name);
  607. mpc_vcc_close(vcc, dev);
  608. return;
  609. }
  610. skb->dev = dev;
  611. if (memcmp(skb->data, &llc_snap_mpoa_ctrl,
  612. sizeof(struct llc_snap_hdr)) == 0) {
  613. struct sock *sk = sk_atm(vcc);
  614. dprintk("(%s) control packet arrived\n", dev->name);
  615. /* Pass control packets to daemon */
  616. skb_queue_tail(&sk->sk_receive_queue, skb);
  617. sk->sk_data_ready(sk, skb->len);
  618. return;
  619. }
  620. /* data coming over the shortcut */
  621. atm_return(vcc, skb->truesize);
  622. mpc = find_mpc_by_lec(dev);
  623. if (mpc == NULL) {
  624. pr_info("(%s) unknown MPC\n", dev->name);
  625. return;
  626. }
  627. if (memcmp(skb->data, &llc_snap_mpoa_data_tagged,
  628. sizeof(struct llc_snap_hdr)) == 0) { /* MPOA tagged data */
  629. ddprintk("(%s) tagged data packet arrived\n", dev->name);
  630. } else if (memcmp(skb->data, &llc_snap_mpoa_data,
  631. sizeof(struct llc_snap_hdr)) == 0) { /* MPOA data */
  632. pr_info("(%s) Unsupported non-tagged data packet arrived. Purging\n",
  633. dev->name);
  634. dev_kfree_skb_any(skb);
  635. return;
  636. } else {
  637. pr_info("(%s) garbage arrived, purging\n", dev->name);
  638. dev_kfree_skb_any(skb);
  639. return;
  640. }
  641. tmp = skb->data + sizeof(struct llc_snap_hdr);
  642. tag = *(__be32 *)tmp;
  643. eg = mpc->eg_ops->get_by_tag(tag, mpc);
  644. if (eg == NULL) {
  645. pr_info("mpoa: (%s) Didn't find egress cache entry, tag = %u\n",
  646. dev->name, tag);
  647. purge_egress_shortcut(vcc, NULL);
  648. dev_kfree_skb_any(skb);
  649. return;
  650. }
  651. /*
  652. * See if ingress MPC is using shortcut we opened as a return channel.
  653. * This means we have a bi-directional vcc opened by us.
  654. */
  655. if (eg->shortcut == NULL) {
  656. eg->shortcut = vcc;
  657. pr_info("(%s) egress SVC in use\n", dev->name);
  658. }
  659. skb_pull(skb, sizeof(struct llc_snap_hdr) + sizeof(tag));
  660. /* get rid of LLC/SNAP header */
  661. new_skb = skb_realloc_headroom(skb, eg->ctrl_info.DH_length);
  662. /* LLC/SNAP is shorter than MAC header :( */
  663. dev_kfree_skb_any(skb);
  664. if (new_skb == NULL) {
  665. mpc->eg_ops->put(eg);
  666. return;
  667. }
  668. skb_push(new_skb, eg->ctrl_info.DH_length); /* add MAC header */
  669. skb_copy_to_linear_data(new_skb, eg->ctrl_info.DLL_header,
  670. eg->ctrl_info.DH_length);
  671. new_skb->protocol = eth_type_trans(new_skb, dev);
  672. skb_reset_network_header(new_skb);
  673. eg->latest_ip_addr = ip_hdr(new_skb)->saddr;
  674. eg->packets_rcvd++;
  675. mpc->eg_ops->put(eg);
  676. memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
  677. netif_rx(new_skb);
  678. return;
  679. }
  680. static struct atmdev_ops mpc_ops = { /* only send is required */
  681. .close = mpoad_close,
  682. .send = msg_from_mpoad
  683. };
  684. static struct atm_dev mpc_dev = {
  685. .ops = &mpc_ops,
  686. .type = "mpc",
  687. .number = 42,
  688. .lock = __SPIN_LOCK_UNLOCKED(mpc_dev.lock)
  689. /* members not explicitly initialised will be 0 */
  690. };
  691. static int atm_mpoa_mpoad_attach(struct atm_vcc *vcc, int arg)
  692. {
  693. struct mpoa_client *mpc;
  694. struct lec_priv *priv;
  695. int err;
  696. if (mpcs == NULL) {
  697. init_timer(&mpc_timer);
  698. mpc_timer_refresh();
  699. /* This lets us now how our LECs are doing */
  700. err = register_netdevice_notifier(&mpoa_notifier);
  701. if (err < 0) {
  702. del_timer(&mpc_timer);
  703. return err;
  704. }
  705. }
  706. mpc = find_mpc_by_itfnum(arg);
  707. if (mpc == NULL) {
  708. dprintk("allocating new mpc for itf %d\n", arg);
  709. mpc = alloc_mpc();
  710. if (mpc == NULL)
  711. return -ENOMEM;
  712. mpc->dev_num = arg;
  713. mpc->dev = find_lec_by_itfnum(arg);
  714. /* NULL if there was no lec */
  715. }
  716. if (mpc->mpoad_vcc) {
  717. pr_info("mpoad is already present for itf %d\n", arg);
  718. return -EADDRINUSE;
  719. }
  720. if (mpc->dev) { /* check if the lec is LANE2 capable */
  721. priv = netdev_priv(mpc->dev);
  722. if (priv->lane_version < 2) {
  723. dev_put(mpc->dev);
  724. mpc->dev = NULL;
  725. } else
  726. priv->lane2_ops->associate_indicator = lane2_assoc_ind;
  727. }
  728. mpc->mpoad_vcc = vcc;
  729. vcc->dev = &mpc_dev;
  730. vcc_insert_socket(sk_atm(vcc));
  731. set_bit(ATM_VF_META, &vcc->flags);
  732. set_bit(ATM_VF_READY, &vcc->flags);
  733. if (mpc->dev) {
  734. char empty[ATM_ESA_LEN];
  735. memset(empty, 0, ATM_ESA_LEN);
  736. start_mpc(mpc, mpc->dev);
  737. /* set address if mpcd e.g. gets killed and restarted.
  738. * If we do not do it now we have to wait for the next LE_ARP
  739. */
  740. if (memcmp(mpc->mps_ctrl_addr, empty, ATM_ESA_LEN) != 0)
  741. send_set_mps_ctrl_addr(mpc->mps_ctrl_addr, mpc);
  742. }
  743. __module_get(THIS_MODULE);
  744. return arg;
  745. }
  746. static void send_set_mps_ctrl_addr(const char *addr, struct mpoa_client *mpc)
  747. {
  748. struct k_message mesg;
  749. memcpy(mpc->mps_ctrl_addr, addr, ATM_ESA_LEN);
  750. mesg.type = SET_MPS_CTRL_ADDR;
  751. memcpy(mesg.MPS_ctrl, addr, ATM_ESA_LEN);
  752. msg_to_mpoad(&mesg, mpc);
  753. return;
  754. }
  755. static void mpoad_close(struct atm_vcc *vcc)
  756. {
  757. struct mpoa_client *mpc;
  758. struct sk_buff *skb;
  759. mpc = find_mpc_by_vcc(vcc);
  760. if (mpc == NULL) {
  761. pr_info("did not find MPC\n");
  762. return;
  763. }
  764. if (!mpc->mpoad_vcc) {
  765. pr_info("close for non-present mpoad\n");
  766. return;
  767. }
  768. mpc->mpoad_vcc = NULL;
  769. if (mpc->dev) {
  770. struct lec_priv *priv = netdev_priv(mpc->dev);
  771. priv->lane2_ops->associate_indicator = NULL;
  772. stop_mpc(mpc);
  773. dev_put(mpc->dev);
  774. }
  775. mpc->in_ops->destroy_cache(mpc);
  776. mpc->eg_ops->destroy_cache(mpc);
  777. while ((skb = skb_dequeue(&sk_atm(vcc)->sk_receive_queue))) {
  778. atm_return(vcc, skb->truesize);
  779. kfree_skb(skb);
  780. }
  781. pr_info("(%s) going down\n",
  782. (mpc->dev) ? mpc->dev->name : "<unknown>");
  783. module_put(THIS_MODULE);
  784. return;
  785. }
  786. /*
  787. *
  788. */
  789. static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb)
  790. {
  791. struct mpoa_client *mpc = find_mpc_by_vcc(vcc);
  792. struct k_message *mesg = (struct k_message *)skb->data;
  793. atomic_sub(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc);
  794. if (mpc == NULL) {
  795. pr_info("no mpc found\n");
  796. return 0;
  797. }
  798. dprintk("(%s)", mpc->dev ? mpc->dev->name : "<unknown>");
  799. switch (mesg->type) {
  800. case MPOA_RES_REPLY_RCVD:
  801. dprintk_cont("mpoa_res_reply_rcvd\n");
  802. MPOA_res_reply_rcvd(mesg, mpc);
  803. break;
  804. case MPOA_TRIGGER_RCVD:
  805. dprintk_cont("mpoa_trigger_rcvd\n");
  806. MPOA_trigger_rcvd(mesg, mpc);
  807. break;
  808. case INGRESS_PURGE_RCVD:
  809. dprintk_cont("nhrp_purge_rcvd\n");
  810. ingress_purge_rcvd(mesg, mpc);
  811. break;
  812. case EGRESS_PURGE_RCVD:
  813. dprintk_cont("egress_purge_reply_rcvd\n");
  814. egress_purge_rcvd(mesg, mpc);
  815. break;
  816. case MPS_DEATH:
  817. dprintk_cont("mps_death\n");
  818. mps_death(mesg, mpc);
  819. break;
  820. case CACHE_IMPOS_RCVD:
  821. dprintk_cont("cache_impos_rcvd\n");
  822. MPOA_cache_impos_rcvd(mesg, mpc);
  823. break;
  824. case SET_MPC_CTRL_ADDR:
  825. dprintk_cont("set_mpc_ctrl_addr\n");
  826. set_mpc_ctrl_addr_rcvd(mesg, mpc);
  827. break;
  828. case SET_MPS_MAC_ADDR:
  829. dprintk_cont("set_mps_mac_addr\n");
  830. set_mps_mac_addr_rcvd(mesg, mpc);
  831. break;
  832. case CLEAN_UP_AND_EXIT:
  833. dprintk_cont("clean_up_and_exit\n");
  834. clean_up(mesg, mpc, DIE);
  835. break;
  836. case RELOAD:
  837. dprintk_cont("reload\n");
  838. clean_up(mesg, mpc, RELOAD);
  839. break;
  840. case SET_MPC_PARAMS:
  841. dprintk_cont("set_mpc_params\n");
  842. mpc->parameters = mesg->content.params;
  843. break;
  844. default:
  845. dprintk_cont("unknown message %d\n", mesg->type);
  846. break;
  847. }
  848. kfree_skb(skb);
  849. return 0;
  850. }
  851. /* Remember that this function may not do things that sleep */
  852. int msg_to_mpoad(struct k_message *mesg, struct mpoa_client *mpc)
  853. {
  854. struct sk_buff *skb;
  855. struct sock *sk;
  856. if (mpc == NULL || !mpc->mpoad_vcc) {
  857. pr_info("mesg %d to a non-existent mpoad\n", mesg->type);
  858. return -ENXIO;
  859. }
  860. skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
  861. if (skb == NULL)
  862. return -ENOMEM;
  863. skb_put(skb, sizeof(struct k_message));
  864. skb_copy_to_linear_data(skb, mesg, sizeof(*mesg));
  865. atm_force_charge(mpc->mpoad_vcc, skb->truesize);
  866. sk = sk_atm(mpc->mpoad_vcc);
  867. skb_queue_tail(&sk->sk_receive_queue, skb);
  868. sk->sk_data_ready(sk, skb->len);
  869. return 0;
  870. }
  871. static int mpoa_event_listener(struct notifier_block *mpoa_notifier,
  872. unsigned long event, void *dev_ptr)
  873. {
  874. struct net_device *dev;
  875. struct mpoa_client *mpc;
  876. struct lec_priv *priv;
  877. dev = (struct net_device *)dev_ptr;
  878. if (!net_eq(dev_net(dev), &init_net))
  879. return NOTIFY_DONE;
  880. if (dev->name == NULL || strncmp(dev->name, "lec", 3))
  881. return NOTIFY_DONE; /* we are only interested in lec:s */
  882. switch (event) {
  883. case NETDEV_REGISTER: /* a new lec device was allocated */
  884. priv = netdev_priv(dev);
  885. if (priv->lane_version < 2)
  886. break;
  887. priv->lane2_ops->associate_indicator = lane2_assoc_ind;
  888. mpc = find_mpc_by_itfnum(priv->itfnum);
  889. if (mpc == NULL) {
  890. dprintk("allocating new mpc for %s\n", dev->name);
  891. mpc = alloc_mpc();
  892. if (mpc == NULL) {
  893. pr_info("no new mpc");
  894. break;
  895. }
  896. }
  897. mpc->dev_num = priv->itfnum;
  898. mpc->dev = dev;
  899. dev_hold(dev);
  900. dprintk("(%s) was initialized\n", dev->name);
  901. break;
  902. case NETDEV_UNREGISTER:
  903. /* the lec device was deallocated */
  904. mpc = find_mpc_by_lec(dev);
  905. if (mpc == NULL)
  906. break;
  907. dprintk("device (%s) was deallocated\n", dev->name);
  908. stop_mpc(mpc);
  909. dev_put(mpc->dev);
  910. mpc->dev = NULL;
  911. break;
  912. case NETDEV_UP:
  913. /* the dev was ifconfig'ed up */
  914. mpc = find_mpc_by_lec(dev);
  915. if (mpc == NULL)
  916. break;
  917. if (mpc->mpoad_vcc != NULL)
  918. start_mpc(mpc, dev);
  919. break;
  920. case NETDEV_DOWN:
  921. /* the dev was ifconfig'ed down */
  922. /* this means that the flow of packets from the
  923. * upper layer stops
  924. */
  925. mpc = find_mpc_by_lec(dev);
  926. if (mpc == NULL)
  927. break;
  928. if (mpc->mpoad_vcc != NULL)
  929. stop_mpc(mpc);
  930. break;
  931. case NETDEV_REBOOT:
  932. case NETDEV_CHANGE:
  933. case NETDEV_CHANGEMTU:
  934. case NETDEV_CHANGEADDR:
  935. case NETDEV_GOING_DOWN:
  936. break;
  937. default:
  938. break;
  939. }
  940. return NOTIFY_DONE;
  941. }
  942. /*
  943. * Functions which are called after a message is received from mpcd.
  944. * Msg is reused on purpose.
  945. */
  946. static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc)
  947. {
  948. __be32 dst_ip = msg->content.in_info.in_dst_ip;
  949. in_cache_entry *entry;
  950. entry = mpc->in_ops->get(dst_ip, mpc);
  951. if (entry == NULL) {
  952. entry = mpc->in_ops->add_entry(dst_ip, mpc);
  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. do_gettimeofday(&(entry->reply_wait));
  958. mpc->in_ops->put(entry);
  959. return;
  960. }
  961. if (entry->entry_state == INGRESS_INVALID) {
  962. entry->entry_state = INGRESS_RESOLVING;
  963. msg->type = SND_MPOA_RES_RQST;
  964. msg->content.in_info = entry->ctrl_info;
  965. msg_to_mpoad(msg, mpc);
  966. do_gettimeofday(&(entry->reply_wait));
  967. mpc->in_ops->put(entry);
  968. return;
  969. }
  970. pr_info("(%s) entry already in resolving state\n",
  971. (mpc->dev) ? mpc->dev->name : "<unknown>");
  972. mpc->in_ops->put(entry);
  973. return;
  974. }
  975. /*
  976. * Things get complicated because we have to check if there's an egress
  977. * shortcut with suitable traffic parameters we could use.
  978. */
  979. static void check_qos_and_open_shortcut(struct k_message *msg,
  980. struct mpoa_client *client,
  981. in_cache_entry *entry)
  982. {
  983. __be32 dst_ip = msg->content.in_info.in_dst_ip;
  984. struct atm_mpoa_qos *qos = atm_mpoa_search_qos(dst_ip);
  985. eg_cache_entry *eg_entry = client->eg_ops->get_by_src_ip(dst_ip, client);
  986. if (eg_entry && eg_entry->shortcut) {
  987. if (eg_entry->shortcut->qos.txtp.traffic_class &
  988. msg->qos.txtp.traffic_class &
  989. (qos ? qos->qos.txtp.traffic_class : ATM_UBR | ATM_CBR)) {
  990. if (eg_entry->shortcut->qos.txtp.traffic_class == ATM_UBR)
  991. entry->shortcut = eg_entry->shortcut;
  992. else if (eg_entry->shortcut->qos.txtp.max_pcr > 0)
  993. entry->shortcut = eg_entry->shortcut;
  994. }
  995. if (entry->shortcut) {
  996. dprintk("(%s) using egress SVC to reach %pI4\n",
  997. client->dev->name, &dst_ip);
  998. client->eg_ops->put(eg_entry);
  999. return;
  1000. }
  1001. }
  1002. if (eg_entry != NULL)
  1003. client->eg_ops->put(eg_entry);
  1004. /* No luck in the egress cache we must open an ingress SVC */
  1005. msg->type = OPEN_INGRESS_SVC;
  1006. if (qos &&
  1007. (qos->qos.txtp.traffic_class == msg->qos.txtp.traffic_class)) {
  1008. msg->qos = qos->qos;
  1009. pr_info("(%s) trying to get a CBR shortcut\n",
  1010. client->dev->name);
  1011. } else
  1012. memset(&msg->qos, 0, sizeof(struct atm_qos));
  1013. msg_to_mpoad(msg, client);
  1014. return;
  1015. }
  1016. static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc)
  1017. {
  1018. __be32 dst_ip = msg->content.in_info.in_dst_ip;
  1019. in_cache_entry *entry = mpc->in_ops->get(dst_ip, mpc);
  1020. dprintk("(%s) ip %pI4\n",
  1021. mpc->dev->name, &dst_ip);
  1022. ddprintk("(%s) entry = %p",
  1023. mpc->dev->name, entry);
  1024. if (entry == NULL) {
  1025. pr_info("(%s) ARGH, received res. reply for an entry that doesn't exist.\n",
  1026. mpc->dev->name);
  1027. return;
  1028. }
  1029. ddprintk_cont(" entry_state = %d ", entry->entry_state);
  1030. if (entry->entry_state == INGRESS_RESOLVED) {
  1031. pr_info("(%s) RESOLVED entry!\n", mpc->dev->name);
  1032. mpc->in_ops->put(entry);
  1033. return;
  1034. }
  1035. entry->ctrl_info = msg->content.in_info;
  1036. do_gettimeofday(&(entry->tv));
  1037. do_gettimeofday(&(entry->reply_wait)); /* Used in refreshing func from now on */
  1038. entry->refresh_time = 0;
  1039. ddprintk_cont("entry->shortcut = %p\n", entry->shortcut);
  1040. if (entry->entry_state == INGRESS_RESOLVING &&
  1041. entry->shortcut != NULL) {
  1042. entry->entry_state = INGRESS_RESOLVED;
  1043. mpc->in_ops->put(entry);
  1044. return; /* Shortcut already open... */
  1045. }
  1046. if (entry->shortcut != NULL) {
  1047. pr_info("(%s) entry->shortcut != NULL, impossible!\n",
  1048. mpc->dev->name);
  1049. mpc->in_ops->put(entry);
  1050. return;
  1051. }
  1052. check_qos_and_open_shortcut(msg, mpc, entry);
  1053. entry->entry_state = INGRESS_RESOLVED;
  1054. mpc->in_ops->put(entry);
  1055. return;
  1056. }
  1057. static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
  1058. {
  1059. __be32 dst_ip = msg->content.in_info.in_dst_ip;
  1060. __be32 mask = msg->ip_mask;
  1061. in_cache_entry *entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
  1062. if (entry == NULL) {
  1063. pr_info("(%s) purge for a non-existing entry, ip = %pI4\n",
  1064. mpc->dev->name, &dst_ip);
  1065. return;
  1066. }
  1067. do {
  1068. dprintk("(%s) removing an ingress entry, ip = %pI4\n",
  1069. mpc->dev->name, &dst_ip);
  1070. write_lock_bh(&mpc->ingress_lock);
  1071. mpc->in_ops->remove_entry(entry, mpc);
  1072. write_unlock_bh(&mpc->ingress_lock);
  1073. mpc->in_ops->put(entry);
  1074. entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
  1075. } while (entry != NULL);
  1076. return;
  1077. }
  1078. static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
  1079. {
  1080. __be32 cache_id = msg->content.eg_info.cache_id;
  1081. eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(cache_id, mpc);
  1082. if (entry == NULL) {
  1083. dprintk("(%s) purge for a non-existing entry\n",
  1084. mpc->dev->name);
  1085. return;
  1086. }
  1087. write_lock_irq(&mpc->egress_lock);
  1088. mpc->eg_ops->remove_entry(entry, mpc);
  1089. write_unlock_irq(&mpc->egress_lock);
  1090. mpc->eg_ops->put(entry);
  1091. return;
  1092. }
  1093. static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry)
  1094. {
  1095. struct sock *sk;
  1096. struct k_message *purge_msg;
  1097. struct sk_buff *skb;
  1098. dprintk("entering\n");
  1099. if (vcc == NULL) {
  1100. pr_info("vcc == NULL\n");
  1101. return;
  1102. }
  1103. skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
  1104. if (skb == NULL) {
  1105. pr_info("out of memory\n");
  1106. return;
  1107. }
  1108. skb_put(skb, sizeof(struct k_message));
  1109. memset(skb->data, 0, sizeof(struct k_message));
  1110. purge_msg = (struct k_message *)skb->data;
  1111. purge_msg->type = DATA_PLANE_PURGE;
  1112. if (entry != NULL)
  1113. purge_msg->content.eg_info = entry->ctrl_info;
  1114. atm_force_charge(vcc, skb->truesize);
  1115. sk = sk_atm(vcc);
  1116. skb_queue_tail(&sk->sk_receive_queue, skb);
  1117. sk->sk_data_ready(sk, skb->len);
  1118. dprintk("exiting\n");
  1119. return;
  1120. }
  1121. /*
  1122. * Our MPS died. Tell our daemon to send NHRP data plane purge to each
  1123. * of the egress shortcuts we have.
  1124. */
  1125. static void mps_death(struct k_message *msg, struct mpoa_client *mpc)
  1126. {
  1127. eg_cache_entry *entry;
  1128. dprintk("(%s)\n", mpc->dev->name);
  1129. if (memcmp(msg->MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN)) {
  1130. pr_info("(%s) wrong MPS\n", mpc->dev->name);
  1131. return;
  1132. }
  1133. /* FIXME: This knows too much of the cache structure */
  1134. read_lock_irq(&mpc->egress_lock);
  1135. entry = mpc->eg_cache;
  1136. while (entry != NULL) {
  1137. purge_egress_shortcut(entry->shortcut, entry);
  1138. entry = entry->next;
  1139. }
  1140. read_unlock_irq(&mpc->egress_lock);
  1141. mpc->in_ops->destroy_cache(mpc);
  1142. mpc->eg_ops->destroy_cache(mpc);
  1143. return;
  1144. }
  1145. static void MPOA_cache_impos_rcvd(struct k_message *msg,
  1146. struct mpoa_client *mpc)
  1147. {
  1148. uint16_t holding_time;
  1149. eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(msg->content.eg_info.cache_id, mpc);
  1150. holding_time = msg->content.eg_info.holding_time;
  1151. dprintk("(%s) entry = %p, holding_time = %u\n",
  1152. mpc->dev->name, entry, holding_time);
  1153. if (entry == NULL && holding_time) {
  1154. entry = mpc->eg_ops->add_entry(msg, mpc);
  1155. mpc->eg_ops->put(entry);
  1156. return;
  1157. }
  1158. if (holding_time) {
  1159. mpc->eg_ops->update(entry, holding_time);
  1160. return;
  1161. }
  1162. write_lock_irq(&mpc->egress_lock);
  1163. mpc->eg_ops->remove_entry(entry, mpc);
  1164. write_unlock_irq(&mpc->egress_lock);
  1165. mpc->eg_ops->put(entry);
  1166. return;
  1167. }
  1168. static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg,
  1169. struct mpoa_client *mpc)
  1170. {
  1171. struct lec_priv *priv;
  1172. int i, retval ;
  1173. uint8_t tlv[4 + 1 + 1 + 1 + ATM_ESA_LEN];
  1174. tlv[0] = 00; tlv[1] = 0xa0; tlv[2] = 0x3e; tlv[3] = 0x2a; /* type */
  1175. tlv[4] = 1 + 1 + ATM_ESA_LEN; /* length */
  1176. tlv[5] = 0x02; /* MPOA client */
  1177. tlv[6] = 0x00; /* number of MPS MAC addresses */
  1178. memcpy(&tlv[7], mesg->MPS_ctrl, ATM_ESA_LEN); /* MPC ctrl ATM addr */
  1179. memcpy(mpc->our_ctrl_addr, mesg->MPS_ctrl, ATM_ESA_LEN);
  1180. dprintk("(%s) setting MPC ctrl ATM address to",
  1181. mpc->dev ? mpc->dev->name : "<unknown>");
  1182. for (i = 7; i < sizeof(tlv); i++)
  1183. dprintk_cont(" %02x", tlv[i]);
  1184. dprintk_cont("\n");
  1185. if (mpc->dev) {
  1186. priv = netdev_priv(mpc->dev);
  1187. retval = priv->lane2_ops->associate_req(mpc->dev,
  1188. mpc->dev->dev_addr,
  1189. tlv, sizeof(tlv));
  1190. if (retval == 0)
  1191. pr_info("(%s) MPOA device type TLV association failed\n",
  1192. mpc->dev->name);
  1193. retval = priv->lane2_ops->resolve(mpc->dev, NULL, 1, NULL, NULL);
  1194. if (retval < 0)
  1195. pr_info("(%s) targetless LE_ARP request failed\n",
  1196. mpc->dev->name);
  1197. }
  1198. return;
  1199. }
  1200. static void set_mps_mac_addr_rcvd(struct k_message *msg,
  1201. struct mpoa_client *client)
  1202. {
  1203. if (client->number_of_mps_macs)
  1204. kfree(client->mps_macs);
  1205. client->number_of_mps_macs = 0;
  1206. client->mps_macs = kmemdup(msg->MPS_ctrl, ETH_ALEN, GFP_KERNEL);
  1207. if (client->mps_macs == NULL) {
  1208. pr_info("out of memory\n");
  1209. return;
  1210. }
  1211. client->number_of_mps_macs = 1;
  1212. return;
  1213. }
  1214. /*
  1215. * purge egress cache and tell daemon to 'action' (DIE, RELOAD)
  1216. */
  1217. static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action)
  1218. {
  1219. eg_cache_entry *entry;
  1220. msg->type = SND_EGRESS_PURGE;
  1221. /* FIXME: This knows too much of the cache structure */
  1222. read_lock_irq(&mpc->egress_lock);
  1223. entry = mpc->eg_cache;
  1224. while (entry != NULL) {
  1225. msg->content.eg_info = entry->ctrl_info;
  1226. dprintk("cache_id %u\n", entry->ctrl_info.cache_id);
  1227. msg_to_mpoad(msg, mpc);
  1228. entry = entry->next;
  1229. }
  1230. read_unlock_irq(&mpc->egress_lock);
  1231. msg->type = action;
  1232. msg_to_mpoad(msg, mpc);
  1233. return;
  1234. }
  1235. static void mpc_timer_refresh(void)
  1236. {
  1237. mpc_timer.expires = jiffies + (MPC_P2 * HZ);
  1238. mpc_timer.data = mpc_timer.expires;
  1239. mpc_timer.function = mpc_cache_check;
  1240. add_timer(&mpc_timer);
  1241. return;
  1242. }
  1243. static void mpc_cache_check(unsigned long checking_time)
  1244. {
  1245. struct mpoa_client *mpc = mpcs;
  1246. static unsigned long previous_resolving_check_time;
  1247. static unsigned long previous_refresh_time;
  1248. while (mpc != NULL) {
  1249. mpc->in_ops->clear_count(mpc);
  1250. mpc->eg_ops->clear_expired(mpc);
  1251. if (checking_time - previous_resolving_check_time >
  1252. mpc->parameters.mpc_p4 * HZ) {
  1253. mpc->in_ops->check_resolving(mpc);
  1254. previous_resolving_check_time = checking_time;
  1255. }
  1256. if (checking_time - previous_refresh_time >
  1257. mpc->parameters.mpc_p5 * HZ) {
  1258. mpc->in_ops->refresh(mpc);
  1259. previous_refresh_time = checking_time;
  1260. }
  1261. mpc = mpc->next;
  1262. }
  1263. mpc_timer_refresh();
  1264. return;
  1265. }
  1266. static int atm_mpoa_ioctl(struct socket *sock, unsigned int cmd,
  1267. unsigned long arg)
  1268. {
  1269. int err = 0;
  1270. struct atm_vcc *vcc = ATM_SD(sock);
  1271. if (cmd != ATMMPC_CTRL && cmd != ATMMPC_DATA)
  1272. return -ENOIOCTLCMD;
  1273. if (!capable(CAP_NET_ADMIN))
  1274. return -EPERM;
  1275. switch (cmd) {
  1276. case ATMMPC_CTRL:
  1277. err = atm_mpoa_mpoad_attach(vcc, (int)arg);
  1278. if (err >= 0)
  1279. sock->state = SS_CONNECTED;
  1280. break;
  1281. case ATMMPC_DATA:
  1282. err = atm_mpoa_vcc_attach(vcc, (void __user *)arg);
  1283. break;
  1284. default:
  1285. break;
  1286. }
  1287. return err;
  1288. }
  1289. static struct atm_ioctl atm_ioctl_ops = {
  1290. .owner = THIS_MODULE,
  1291. .ioctl = atm_mpoa_ioctl,
  1292. };
  1293. static __init int atm_mpoa_init(void)
  1294. {
  1295. register_atm_ioctl(&atm_ioctl_ops);
  1296. if (mpc_proc_init() != 0)
  1297. pr_info("failed to initialize /proc/mpoa\n");
  1298. pr_info("mpc.c: " __DATE__ " " __TIME__ " initialized\n");
  1299. return 0;
  1300. }
  1301. static void __exit atm_mpoa_cleanup(void)
  1302. {
  1303. struct mpoa_client *mpc, *tmp;
  1304. struct atm_mpoa_qos *qos, *nextqos;
  1305. struct lec_priv *priv;
  1306. mpc_proc_clean();
  1307. del_timer(&mpc_timer);
  1308. unregister_netdevice_notifier(&mpoa_notifier);
  1309. deregister_atm_ioctl(&atm_ioctl_ops);
  1310. mpc = mpcs;
  1311. mpcs = NULL;
  1312. while (mpc != NULL) {
  1313. tmp = mpc->next;
  1314. if (mpc->dev != NULL) {
  1315. stop_mpc(mpc);
  1316. priv = netdev_priv(mpc->dev);
  1317. if (priv->lane2_ops != NULL)
  1318. priv->lane2_ops->associate_indicator = NULL;
  1319. }
  1320. ddprintk("about to clear caches\n");
  1321. mpc->in_ops->destroy_cache(mpc);
  1322. mpc->eg_ops->destroy_cache(mpc);
  1323. ddprintk("caches cleared\n");
  1324. kfree(mpc->mps_macs);
  1325. memset(mpc, 0, sizeof(struct mpoa_client));
  1326. ddprintk("about to kfree %p\n", mpc);
  1327. kfree(mpc);
  1328. ddprintk("next mpc is at %p\n", tmp);
  1329. mpc = tmp;
  1330. }
  1331. qos = qos_head;
  1332. qos_head = NULL;
  1333. while (qos != NULL) {
  1334. nextqos = qos->next;
  1335. dprintk("freeing qos entry %p\n", qos);
  1336. kfree(qos);
  1337. qos = nextqos;
  1338. }
  1339. return;
  1340. }
  1341. module_init(atm_mpoa_init);
  1342. module_exit(atm_mpoa_cleanup);
  1343. MODULE_LICENSE("GPL");