mpc.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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. /* mpc_daemon -> kernel */
  60. static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc);
  61. static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc);
  62. static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
  63. static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
  64. static void mps_death(struct k_message *msg, struct mpoa_client *mpc);
  65. static void clean_up(struct k_message *msg, struct mpoa_client *mpc,
  66. int action);
  67. static void MPOA_cache_impos_rcvd(struct k_message *msg,
  68. struct mpoa_client *mpc);
  69. static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg,
  70. struct mpoa_client *mpc);
  71. static void set_mps_mac_addr_rcvd(struct k_message *mesg,
  72. struct mpoa_client *mpc);
  73. static const uint8_t *copy_macs(struct mpoa_client *mpc,
  74. const uint8_t *router_mac,
  75. const uint8_t *tlvs, uint8_t mps_macs,
  76. uint8_t device_type);
  77. static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry);
  78. static void send_set_mps_ctrl_addr(const char *addr, struct mpoa_client *mpc);
  79. static void mpoad_close(struct atm_vcc *vcc);
  80. static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb);
  81. static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb);
  82. static netdev_tx_t mpc_send_packet(struct sk_buff *skb,
  83. struct net_device *dev);
  84. static int mpoa_event_listener(struct notifier_block *mpoa_notifier,
  85. unsigned long event, void *dev);
  86. static void mpc_timer_refresh(void);
  87. static void mpc_cache_check(unsigned long checking_time);
  88. static struct llc_snap_hdr llc_snap_mpoa_ctrl = {
  89. 0xaa, 0xaa, 0x03,
  90. {0x00, 0x00, 0x5e},
  91. {0x00, 0x03} /* For MPOA control PDUs */
  92. };
  93. static struct llc_snap_hdr llc_snap_mpoa_data = {
  94. 0xaa, 0xaa, 0x03,
  95. {0x00, 0x00, 0x00},
  96. {0x08, 0x00} /* This is for IP PDUs only */
  97. };
  98. static struct llc_snap_hdr llc_snap_mpoa_data_tagged = {
  99. 0xaa, 0xaa, 0x03,
  100. {0x00, 0x00, 0x00},
  101. {0x88, 0x4c} /* This is for tagged data PDUs */
  102. };
  103. static struct notifier_block mpoa_notifier = {
  104. mpoa_event_listener,
  105. NULL,
  106. 0
  107. };
  108. struct mpoa_client *mpcs = NULL; /* FIXME */
  109. static struct atm_mpoa_qos *qos_head = NULL;
  110. static DEFINE_TIMER(mpc_timer, NULL, 0, 0);
  111. static struct mpoa_client *find_mpc_by_itfnum(int itf)
  112. {
  113. struct mpoa_client *mpc;
  114. mpc = mpcs; /* our global linked list */
  115. while (mpc != NULL) {
  116. if (mpc->dev_num == itf)
  117. return mpc;
  118. mpc = mpc->next;
  119. }
  120. return NULL; /* not found */
  121. }
  122. static struct mpoa_client *find_mpc_by_vcc(struct atm_vcc *vcc)
  123. {
  124. struct mpoa_client *mpc;
  125. mpc = mpcs; /* our global linked list */
  126. while (mpc != NULL) {
  127. if (mpc->mpoad_vcc == vcc)
  128. return mpc;
  129. mpc = mpc->next;
  130. }
  131. return NULL; /* not found */
  132. }
  133. static struct mpoa_client *find_mpc_by_lec(struct net_device *dev)
  134. {
  135. struct mpoa_client *mpc;
  136. mpc = mpcs; /* our global linked list */
  137. while (mpc != NULL) {
  138. if (mpc->dev == dev)
  139. return mpc;
  140. mpc = mpc->next;
  141. }
  142. return NULL; /* not found */
  143. }
  144. /*
  145. * Functions for managing QoS list
  146. */
  147. /*
  148. * Overwrites the old entry or makes a new one.
  149. */
  150. struct atm_mpoa_qos *atm_mpoa_add_qos(__be32 dst_ip, struct atm_qos *qos)
  151. {
  152. struct atm_mpoa_qos *entry;
  153. entry = atm_mpoa_search_qos(dst_ip);
  154. if (entry != NULL) {
  155. entry->qos = *qos;
  156. return entry;
  157. }
  158. entry = kmalloc(sizeof(struct atm_mpoa_qos), GFP_KERNEL);
  159. if (entry == NULL) {
  160. pr_info("mpoa: out of memory\n");
  161. return entry;
  162. }
  163. entry->ipaddr = dst_ip;
  164. entry->qos = *qos;
  165. entry->next = qos_head;
  166. qos_head = entry;
  167. return entry;
  168. }
  169. struct atm_mpoa_qos *atm_mpoa_search_qos(__be32 dst_ip)
  170. {
  171. struct atm_mpoa_qos *qos;
  172. qos = qos_head;
  173. while (qos) {
  174. if (qos->ipaddr == dst_ip)
  175. break;
  176. qos = qos->next;
  177. }
  178. return qos;
  179. }
  180. /*
  181. * Returns 0 for failure
  182. */
  183. int atm_mpoa_delete_qos(struct atm_mpoa_qos *entry)
  184. {
  185. struct atm_mpoa_qos *curr;
  186. if (entry == NULL)
  187. return 0;
  188. if (entry == qos_head) {
  189. qos_head = qos_head->next;
  190. kfree(entry);
  191. return 1;
  192. }
  193. curr = qos_head;
  194. while (curr != NULL) {
  195. if (curr->next == entry) {
  196. curr->next = entry->next;
  197. kfree(entry);
  198. return 1;
  199. }
  200. curr = curr->next;
  201. }
  202. return 0;
  203. }
  204. /* this is buggered - we need locking for qos_head */
  205. void atm_mpoa_disp_qos(struct seq_file *m)
  206. {
  207. struct atm_mpoa_qos *qos;
  208. qos = qos_head;
  209. seq_printf(m, "QoS entries for shortcuts:\n");
  210. 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");
  211. while (qos != NULL) {
  212. seq_printf(m, "%pI4\n %-7d %-7d %-7d %-7d %-7d\n %-7d %-7d %-7d %-7d %-7d\n",
  213. &qos->ipaddr,
  214. qos->qos.txtp.max_pcr,
  215. qos->qos.txtp.pcr,
  216. qos->qos.txtp.min_pcr,
  217. qos->qos.txtp.max_cdv,
  218. qos->qos.txtp.max_sdu,
  219. qos->qos.rxtp.max_pcr,
  220. qos->qos.rxtp.pcr,
  221. qos->qos.rxtp.min_pcr,
  222. qos->qos.rxtp.max_cdv,
  223. qos->qos.rxtp.max_sdu);
  224. qos = qos->next;
  225. }
  226. }
  227. static struct net_device *find_lec_by_itfnum(int itf)
  228. {
  229. struct net_device *dev;
  230. char name[IFNAMSIZ];
  231. sprintf(name, "lec%d", itf);
  232. dev = dev_get_by_name(&init_net, name);
  233. return dev;
  234. }
  235. static struct mpoa_client *alloc_mpc(void)
  236. {
  237. struct mpoa_client *mpc;
  238. mpc = kzalloc(sizeof(struct mpoa_client), GFP_KERNEL);
  239. if (mpc == NULL)
  240. return NULL;
  241. rwlock_init(&mpc->ingress_lock);
  242. rwlock_init(&mpc->egress_lock);
  243. mpc->next = mpcs;
  244. atm_mpoa_init_cache(mpc);
  245. mpc->parameters.mpc_p1 = MPC_P1;
  246. mpc->parameters.mpc_p2 = MPC_P2;
  247. memset(mpc->parameters.mpc_p3, 0, sizeof(mpc->parameters.mpc_p3));
  248. mpc->parameters.mpc_p4 = MPC_P4;
  249. mpc->parameters.mpc_p5 = MPC_P5;
  250. mpc->parameters.mpc_p6 = MPC_P6;
  251. mpcs = mpc;
  252. return mpc;
  253. }
  254. /*
  255. *
  256. * start_mpc() puts the MPC on line. All the packets destined
  257. * to the lec underneath us are now being monitored and
  258. * shortcuts will be established.
  259. *
  260. */
  261. static void start_mpc(struct mpoa_client *mpc, struct net_device *dev)
  262. {
  263. dprintk("(%s)\n", mpc->dev->name);
  264. if (!dev->netdev_ops)
  265. pr_info("(%s) not starting\n", dev->name);
  266. else {
  267. mpc->old_ops = dev->netdev_ops;
  268. mpc->new_ops = *mpc->old_ops;
  269. mpc->new_ops.ndo_start_xmit = mpc_send_packet;
  270. dev->netdev_ops = &mpc->new_ops;
  271. }
  272. }
  273. static void stop_mpc(struct mpoa_client *mpc)
  274. {
  275. struct net_device *dev = mpc->dev;
  276. dprintk("(%s)", mpc->dev->name);
  277. /* Lets not nullify lec device's dev->hard_start_xmit */
  278. if (dev->netdev_ops != &mpc->new_ops) {
  279. dprintk_cont(" mpc already stopped, not fatal\n");
  280. return;
  281. }
  282. dprintk_cont("\n");
  283. dev->netdev_ops = mpc->old_ops;
  284. mpc->old_ops = NULL;
  285. /* close_shortcuts(mpc); ??? FIXME */
  286. }
  287. static const char *mpoa_device_type_string(char type) __attribute__ ((unused));
  288. static const char *mpoa_device_type_string(char type)
  289. {
  290. switch (type) {
  291. case NON_MPOA:
  292. return "non-MPOA device";
  293. case MPS:
  294. return "MPS";
  295. case MPC:
  296. return "MPC";
  297. case MPS_AND_MPC:
  298. return "both MPS and MPC";
  299. }
  300. return "unspecified (non-MPOA) device";
  301. }
  302. /*
  303. * lec device calls this via its netdev_priv(dev)->lane2_ops
  304. * ->associate_indicator() when it sees a TLV in LE_ARP packet.
  305. * We fill in the pointer above when we see a LANE2 lec initializing
  306. * See LANE2 spec 3.1.5
  307. *
  308. * Quite a big and ugly function but when you look at it
  309. * all it does is to try to locate and parse MPOA Device
  310. * Type TLV.
  311. * We give our lec a pointer to this function and when the
  312. * lec sees a TLV it uses the pointer to call this function.
  313. *
  314. */
  315. static void lane2_assoc_ind(struct net_device *dev, const u8 *mac_addr,
  316. const u8 *tlvs, u32 sizeoftlvs)
  317. {
  318. uint32_t type;
  319. uint8_t length, mpoa_device_type, number_of_mps_macs;
  320. const uint8_t *end_of_tlvs;
  321. struct mpoa_client *mpc;
  322. mpoa_device_type = number_of_mps_macs = 0; /* silence gcc */
  323. dprintk("(%s) received TLV(s), ", dev->name);
  324. dprintk("total length of all TLVs %d\n", sizeoftlvs);
  325. mpc = find_mpc_by_lec(dev); /* Sampo-Fix: moved here from below */
  326. if (mpc == NULL) {
  327. pr_info("(%s) no mpc\n", dev->name);
  328. return;
  329. }
  330. end_of_tlvs = tlvs + sizeoftlvs;
  331. while (end_of_tlvs - tlvs >= 5) {
  332. type = ((tlvs[0] << 24) | (tlvs[1] << 16) |
  333. (tlvs[2] << 8) | tlvs[3]);
  334. length = tlvs[4];
  335. tlvs += 5;
  336. dprintk(" type 0x%x length %02x\n", type, length);
  337. if (tlvs + length > end_of_tlvs) {
  338. pr_info("TLV value extends past its buffer, aborting parse\n");
  339. return;
  340. }
  341. if (type == 0) {
  342. pr_info("mpoa: (%s) TLV type was 0, returning\n",
  343. dev->name);
  344. return;
  345. }
  346. if (type != TLV_MPOA_DEVICE_TYPE) {
  347. tlvs += length;
  348. continue; /* skip other TLVs */
  349. }
  350. mpoa_device_type = *tlvs++;
  351. number_of_mps_macs = *tlvs++;
  352. dprintk("(%s) MPOA device type '%s', ",
  353. dev->name, mpoa_device_type_string(mpoa_device_type));
  354. if (mpoa_device_type == MPS_AND_MPC &&
  355. length < (42 + number_of_mps_macs*ETH_ALEN)) { /* :) */
  356. pr_info("(%s) short MPOA Device Type TLV\n",
  357. dev->name);
  358. continue;
  359. }
  360. if ((mpoa_device_type == MPS || mpoa_device_type == MPC) &&
  361. length < 22 + number_of_mps_macs*ETH_ALEN) {
  362. pr_info("(%s) short MPOA Device Type TLV\n", dev->name);
  363. continue;
  364. }
  365. if (mpoa_device_type != MPS &&
  366. mpoa_device_type != MPS_AND_MPC) {
  367. dprintk("ignoring non-MPS device ");
  368. if (mpoa_device_type == MPC)
  369. tlvs += 20;
  370. continue; /* we are only interested in MPSs */
  371. }
  372. if (number_of_mps_macs == 0 &&
  373. mpoa_device_type == MPS_AND_MPC) {
  374. pr_info("(%s) MPS_AND_MPC has zero MACs\n", dev->name);
  375. continue; /* someone should read the spec */
  376. }
  377. dprintk_cont("this MPS has %d MAC addresses\n",
  378. number_of_mps_macs);
  379. /*
  380. * ok, now we can go and tell our daemon
  381. * the control address of MPS
  382. */
  383. send_set_mps_ctrl_addr(tlvs, mpc);
  384. tlvs = copy_macs(mpc, mac_addr, tlvs,
  385. number_of_mps_macs, mpoa_device_type);
  386. if (tlvs == NULL)
  387. return;
  388. }
  389. if (end_of_tlvs - tlvs != 0)
  390. pr_info("(%s) ignoring %zd bytes of trailing TLV garbage\n",
  391. dev->name, end_of_tlvs - tlvs);
  392. }
  393. /*
  394. * Store at least advertizing router's MAC address
  395. * plus the possible MAC address(es) to mpc->mps_macs.
  396. * For a freshly allocated MPOA client mpc->mps_macs == 0.
  397. */
  398. static const uint8_t *copy_macs(struct mpoa_client *mpc,
  399. const uint8_t *router_mac,
  400. const uint8_t *tlvs, uint8_t mps_macs,
  401. uint8_t device_type)
  402. {
  403. int num_macs;
  404. num_macs = (mps_macs > 1) ? mps_macs : 1;
  405. if (mpc->number_of_mps_macs != num_macs) { /* need to reallocate? */
  406. if (mpc->number_of_mps_macs != 0)
  407. kfree(mpc->mps_macs);
  408. mpc->number_of_mps_macs = 0;
  409. mpc->mps_macs = kmalloc(num_macs * ETH_ALEN, GFP_KERNEL);
  410. if (mpc->mps_macs == NULL) {
  411. pr_info("(%s) out of mem\n", mpc->dev->name);
  412. return NULL;
  413. }
  414. }
  415. ether_addr_copy(mpc->mps_macs, router_mac);
  416. tlvs += 20; if (device_type == MPS_AND_MPC) tlvs += 20;
  417. if (mps_macs > 0)
  418. memcpy(mpc->mps_macs, tlvs, mps_macs*ETH_ALEN);
  419. tlvs += mps_macs*ETH_ALEN;
  420. mpc->number_of_mps_macs = num_macs;
  421. return tlvs;
  422. }
  423. static int send_via_shortcut(struct sk_buff *skb, struct mpoa_client *mpc)
  424. {
  425. in_cache_entry *entry;
  426. struct iphdr *iph;
  427. char *buff;
  428. __be32 ipaddr = 0;
  429. static struct {
  430. struct llc_snap_hdr hdr;
  431. __be32 tag;
  432. } tagged_llc_snap_hdr = {
  433. {0xaa, 0xaa, 0x03, {0x00, 0x00, 0x00}, {0x88, 0x4c}},
  434. 0
  435. };
  436. buff = skb->data + mpc->dev->hard_header_len;
  437. iph = (struct iphdr *)buff;
  438. ipaddr = iph->daddr;
  439. ddprintk("(%s) ipaddr 0x%x\n",
  440. mpc->dev->name, ipaddr);
  441. entry = mpc->in_ops->get(ipaddr, mpc);
  442. if (entry == NULL) {
  443. entry = mpc->in_ops->add_entry(ipaddr, mpc);
  444. if (entry != NULL)
  445. mpc->in_ops->put(entry);
  446. return 1;
  447. }
  448. /* threshold not exceeded or VCC not ready */
  449. if (mpc->in_ops->cache_hit(entry, mpc) != OPEN) {
  450. ddprintk("(%s) cache_hit: returns != OPEN\n",
  451. mpc->dev->name);
  452. mpc->in_ops->put(entry);
  453. return 1;
  454. }
  455. ddprintk("(%s) using shortcut\n",
  456. mpc->dev->name);
  457. /* MPOA spec A.1.4, MPOA client must decrement IP ttl at least by one */
  458. if (iph->ttl <= 1) {
  459. ddprintk("(%s) IP ttl = %u, using LANE\n",
  460. mpc->dev->name, iph->ttl);
  461. mpc->in_ops->put(entry);
  462. return 1;
  463. }
  464. iph->ttl--;
  465. iph->check = 0;
  466. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  467. if (entry->ctrl_info.tag != 0) {
  468. ddprintk("(%s) adding tag 0x%x\n",
  469. mpc->dev->name, entry->ctrl_info.tag);
  470. tagged_llc_snap_hdr.tag = entry->ctrl_info.tag;
  471. skb_pull(skb, ETH_HLEN); /* get rid of Eth header */
  472. skb_push(skb, sizeof(tagged_llc_snap_hdr));
  473. /* add LLC/SNAP header */
  474. skb_copy_to_linear_data(skb, &tagged_llc_snap_hdr,
  475. sizeof(tagged_llc_snap_hdr));
  476. } else {
  477. skb_pull(skb, ETH_HLEN); /* get rid of Eth header */
  478. skb_push(skb, sizeof(struct llc_snap_hdr));
  479. /* add LLC/SNAP header + tag */
  480. skb_copy_to_linear_data(skb, &llc_snap_mpoa_data,
  481. sizeof(struct llc_snap_hdr));
  482. }
  483. atm_account_tx(entry->shortcut, skb);
  484. entry->shortcut->send(entry->shortcut, skb);
  485. entry->packets_fwded++;
  486. mpc->in_ops->put(entry);
  487. return 0;
  488. }
  489. /*
  490. * Probably needs some error checks and locking, not sure...
  491. */
  492. static netdev_tx_t mpc_send_packet(struct sk_buff *skb,
  493. struct net_device *dev)
  494. {
  495. struct mpoa_client *mpc;
  496. struct ethhdr *eth;
  497. int i = 0;
  498. mpc = find_mpc_by_lec(dev); /* this should NEVER fail */
  499. if (mpc == NULL) {
  500. pr_info("(%s) no MPC found\n", dev->name);
  501. goto non_ip;
  502. }
  503. eth = (struct ethhdr *)skb->data;
  504. if (eth->h_proto != htons(ETH_P_IP))
  505. goto non_ip; /* Multi-Protocol Over ATM :-) */
  506. /* Weed out funny packets (e.g., AF_PACKET or raw). */
  507. if (skb->len < ETH_HLEN + sizeof(struct iphdr))
  508. goto non_ip;
  509. skb_set_network_header(skb, ETH_HLEN);
  510. if (skb->len < ETH_HLEN + ip_hdr(skb)->ihl * 4 || ip_hdr(skb)->ihl < 5)
  511. goto non_ip;
  512. while (i < mpc->number_of_mps_macs) {
  513. if (ether_addr_equal(eth->h_dest, mpc->mps_macs + i * ETH_ALEN))
  514. if (send_via_shortcut(skb, mpc) == 0) /* try shortcut */
  515. return NETDEV_TX_OK;
  516. i++;
  517. }
  518. non_ip:
  519. return __netdev_start_xmit(mpc->old_ops, skb, dev, false);
  520. }
  521. static int atm_mpoa_vcc_attach(struct atm_vcc *vcc, void __user *arg)
  522. {
  523. int bytes_left;
  524. struct mpoa_client *mpc;
  525. struct atmmpc_ioc ioc_data;
  526. in_cache_entry *in_entry;
  527. __be32 ipaddr;
  528. bytes_left = copy_from_user(&ioc_data, arg, sizeof(struct atmmpc_ioc));
  529. if (bytes_left != 0) {
  530. pr_info("mpoa:Short read (missed %d bytes) from userland\n",
  531. bytes_left);
  532. return -EFAULT;
  533. }
  534. ipaddr = ioc_data.ipaddr;
  535. if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF)
  536. return -EINVAL;
  537. mpc = find_mpc_by_itfnum(ioc_data.dev_num);
  538. if (mpc == NULL)
  539. return -EINVAL;
  540. if (ioc_data.type == MPC_SOCKET_INGRESS) {
  541. in_entry = mpc->in_ops->get(ipaddr, mpc);
  542. if (in_entry == NULL ||
  543. in_entry->entry_state < INGRESS_RESOLVED) {
  544. pr_info("(%s) did not find RESOLVED entry from ingress cache\n",
  545. mpc->dev->name);
  546. if (in_entry != NULL)
  547. mpc->in_ops->put(in_entry);
  548. return -EINVAL;
  549. }
  550. pr_info("(%s) attaching ingress SVC, entry = %pI4\n",
  551. mpc->dev->name, &in_entry->ctrl_info.in_dst_ip);
  552. in_entry->shortcut = vcc;
  553. mpc->in_ops->put(in_entry);
  554. } else {
  555. pr_info("(%s) attaching egress SVC\n", mpc->dev->name);
  556. }
  557. vcc->proto_data = mpc->dev;
  558. vcc->push = mpc_push;
  559. return 0;
  560. }
  561. /*
  562. *
  563. */
  564. static void mpc_vcc_close(struct atm_vcc *vcc, struct net_device *dev)
  565. {
  566. struct mpoa_client *mpc;
  567. in_cache_entry *in_entry;
  568. eg_cache_entry *eg_entry;
  569. mpc = find_mpc_by_lec(dev);
  570. if (mpc == NULL) {
  571. pr_info("(%s) close for unknown MPC\n", dev->name);
  572. return;
  573. }
  574. dprintk("(%s)\n", dev->name);
  575. in_entry = mpc->in_ops->get_by_vcc(vcc, mpc);
  576. if (in_entry) {
  577. dprintk("(%s) ingress SVC closed ip = %pI4\n",
  578. mpc->dev->name, &in_entry->ctrl_info.in_dst_ip);
  579. in_entry->shortcut = NULL;
  580. mpc->in_ops->put(in_entry);
  581. }
  582. eg_entry = mpc->eg_ops->get_by_vcc(vcc, mpc);
  583. if (eg_entry) {
  584. dprintk("(%s) egress SVC closed\n", mpc->dev->name);
  585. eg_entry->shortcut = NULL;
  586. mpc->eg_ops->put(eg_entry);
  587. }
  588. if (in_entry == NULL && eg_entry == NULL)
  589. dprintk("(%s) unused vcc closed\n", dev->name);
  590. }
  591. static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb)
  592. {
  593. struct net_device *dev = (struct net_device *)vcc->proto_data;
  594. struct sk_buff *new_skb;
  595. eg_cache_entry *eg;
  596. struct mpoa_client *mpc;
  597. __be32 tag;
  598. char *tmp;
  599. ddprintk("(%s)\n", dev->name);
  600. if (skb == NULL) {
  601. dprintk("(%s) null skb, closing VCC\n", dev->name);
  602. mpc_vcc_close(vcc, dev);
  603. return;
  604. }
  605. skb->dev = dev;
  606. if (memcmp(skb->data, &llc_snap_mpoa_ctrl,
  607. sizeof(struct llc_snap_hdr)) == 0) {
  608. struct sock *sk = sk_atm(vcc);
  609. dprintk("(%s) control packet arrived\n", dev->name);
  610. /* Pass control packets to daemon */
  611. skb_queue_tail(&sk->sk_receive_queue, skb);
  612. sk->sk_data_ready(sk);
  613. return;
  614. }
  615. /* data coming over the shortcut */
  616. atm_return(vcc, skb->truesize);
  617. mpc = find_mpc_by_lec(dev);
  618. if (mpc == NULL) {
  619. pr_info("(%s) unknown MPC\n", dev->name);
  620. return;
  621. }
  622. if (memcmp(skb->data, &llc_snap_mpoa_data_tagged,
  623. sizeof(struct llc_snap_hdr)) == 0) { /* MPOA tagged data */
  624. ddprintk("(%s) tagged data packet arrived\n", dev->name);
  625. } else if (memcmp(skb->data, &llc_snap_mpoa_data,
  626. sizeof(struct llc_snap_hdr)) == 0) { /* MPOA data */
  627. pr_info("(%s) Unsupported non-tagged data packet arrived. Purging\n",
  628. dev->name);
  629. dev_kfree_skb_any(skb);
  630. return;
  631. } else {
  632. pr_info("(%s) garbage arrived, purging\n", dev->name);
  633. dev_kfree_skb_any(skb);
  634. return;
  635. }
  636. tmp = skb->data + sizeof(struct llc_snap_hdr);
  637. tag = *(__be32 *)tmp;
  638. eg = mpc->eg_ops->get_by_tag(tag, mpc);
  639. if (eg == NULL) {
  640. pr_info("mpoa: (%s) Didn't find egress cache entry, tag = %u\n",
  641. dev->name, tag);
  642. purge_egress_shortcut(vcc, NULL);
  643. dev_kfree_skb_any(skb);
  644. return;
  645. }
  646. /*
  647. * See if ingress MPC is using shortcut we opened as a return channel.
  648. * This means we have a bi-directional vcc opened by us.
  649. */
  650. if (eg->shortcut == NULL) {
  651. eg->shortcut = vcc;
  652. pr_info("(%s) egress SVC in use\n", dev->name);
  653. }
  654. skb_pull(skb, sizeof(struct llc_snap_hdr) + sizeof(tag));
  655. /* get rid of LLC/SNAP header */
  656. new_skb = skb_realloc_headroom(skb, eg->ctrl_info.DH_length);
  657. /* LLC/SNAP is shorter than MAC header :( */
  658. dev_kfree_skb_any(skb);
  659. if (new_skb == NULL) {
  660. mpc->eg_ops->put(eg);
  661. return;
  662. }
  663. skb_push(new_skb, eg->ctrl_info.DH_length); /* add MAC header */
  664. skb_copy_to_linear_data(new_skb, eg->ctrl_info.DLL_header,
  665. eg->ctrl_info.DH_length);
  666. new_skb->protocol = eth_type_trans(new_skb, dev);
  667. skb_reset_network_header(new_skb);
  668. eg->latest_ip_addr = ip_hdr(new_skb)->saddr;
  669. eg->packets_rcvd++;
  670. mpc->eg_ops->put(eg);
  671. memset(ATM_SKB(new_skb), 0, sizeof(struct atm_skb_data));
  672. netif_rx(new_skb);
  673. }
  674. static const struct atmdev_ops mpc_ops = { /* only send is required */
  675. .close = mpoad_close,
  676. .send = msg_from_mpoad
  677. };
  678. static struct atm_dev mpc_dev = {
  679. .ops = &mpc_ops,
  680. .type = "mpc",
  681. .number = 42,
  682. .lock = __SPIN_LOCK_UNLOCKED(mpc_dev.lock)
  683. /* members not explicitly initialised will be 0 */
  684. };
  685. static int atm_mpoa_mpoad_attach(struct atm_vcc *vcc, int arg)
  686. {
  687. struct mpoa_client *mpc;
  688. struct lec_priv *priv;
  689. int err;
  690. if (mpcs == NULL) {
  691. init_timer(&mpc_timer);
  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. do_gettimeofday(&(entry->reply_wait));
  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. do_gettimeofday(&(entry->reply_wait));
  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. do_gettimeofday(&(entry->tv));
  1026. do_gettimeofday(&(entry->reply_wait)); /* Used in refreshing func from now on */
  1027. entry->refresh_time = 0;
  1028. ddprintk_cont("entry->shortcut = %p\n", entry->shortcut);
  1029. if (entry->entry_state == INGRESS_RESOLVING &&
  1030. entry->shortcut != NULL) {
  1031. entry->entry_state = INGRESS_RESOLVED;
  1032. mpc->in_ops->put(entry);
  1033. return; /* Shortcut already open... */
  1034. }
  1035. if (entry->shortcut != NULL) {
  1036. pr_info("(%s) entry->shortcut != NULL, impossible!\n",
  1037. mpc->dev->name);
  1038. mpc->in_ops->put(entry);
  1039. return;
  1040. }
  1041. check_qos_and_open_shortcut(msg, mpc, entry);
  1042. entry->entry_state = INGRESS_RESOLVED;
  1043. mpc->in_ops->put(entry);
  1044. return;
  1045. }
  1046. static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
  1047. {
  1048. __be32 dst_ip = msg->content.in_info.in_dst_ip;
  1049. __be32 mask = msg->ip_mask;
  1050. in_cache_entry *entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
  1051. if (entry == NULL) {
  1052. pr_info("(%s) purge for a non-existing entry, ip = %pI4\n",
  1053. mpc->dev->name, &dst_ip);
  1054. return;
  1055. }
  1056. do {
  1057. dprintk("(%s) removing an ingress entry, ip = %pI4\n",
  1058. mpc->dev->name, &dst_ip);
  1059. write_lock_bh(&mpc->ingress_lock);
  1060. mpc->in_ops->remove_entry(entry, mpc);
  1061. write_unlock_bh(&mpc->ingress_lock);
  1062. mpc->in_ops->put(entry);
  1063. entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
  1064. } while (entry != NULL);
  1065. }
  1066. static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
  1067. {
  1068. __be32 cache_id = msg->content.eg_info.cache_id;
  1069. eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(cache_id, mpc);
  1070. if (entry == NULL) {
  1071. dprintk("(%s) purge for a non-existing entry\n",
  1072. mpc->dev->name);
  1073. return;
  1074. }
  1075. write_lock_irq(&mpc->egress_lock);
  1076. mpc->eg_ops->remove_entry(entry, mpc);
  1077. write_unlock_irq(&mpc->egress_lock);
  1078. mpc->eg_ops->put(entry);
  1079. }
  1080. static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry)
  1081. {
  1082. struct sock *sk;
  1083. struct k_message *purge_msg;
  1084. struct sk_buff *skb;
  1085. dprintk("entering\n");
  1086. if (vcc == NULL) {
  1087. pr_info("vcc == NULL\n");
  1088. return;
  1089. }
  1090. skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
  1091. if (skb == NULL) {
  1092. pr_info("out of memory\n");
  1093. return;
  1094. }
  1095. skb_put(skb, sizeof(struct k_message));
  1096. memset(skb->data, 0, sizeof(struct k_message));
  1097. purge_msg = (struct k_message *)skb->data;
  1098. purge_msg->type = DATA_PLANE_PURGE;
  1099. if (entry != NULL)
  1100. purge_msg->content.eg_info = entry->ctrl_info;
  1101. atm_force_charge(vcc, skb->truesize);
  1102. sk = sk_atm(vcc);
  1103. skb_queue_tail(&sk->sk_receive_queue, skb);
  1104. sk->sk_data_ready(sk);
  1105. dprintk("exiting\n");
  1106. }
  1107. /*
  1108. * Our MPS died. Tell our daemon to send NHRP data plane purge to each
  1109. * of the egress shortcuts we have.
  1110. */
  1111. static void mps_death(struct k_message *msg, struct mpoa_client *mpc)
  1112. {
  1113. eg_cache_entry *entry;
  1114. dprintk("(%s)\n", mpc->dev->name);
  1115. if (memcmp(msg->MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN)) {
  1116. pr_info("(%s) wrong MPS\n", mpc->dev->name);
  1117. return;
  1118. }
  1119. /* FIXME: This knows too much of the cache structure */
  1120. read_lock_irq(&mpc->egress_lock);
  1121. entry = mpc->eg_cache;
  1122. while (entry != NULL) {
  1123. purge_egress_shortcut(entry->shortcut, entry);
  1124. entry = entry->next;
  1125. }
  1126. read_unlock_irq(&mpc->egress_lock);
  1127. mpc->in_ops->destroy_cache(mpc);
  1128. mpc->eg_ops->destroy_cache(mpc);
  1129. }
  1130. static void MPOA_cache_impos_rcvd(struct k_message *msg,
  1131. struct mpoa_client *mpc)
  1132. {
  1133. uint16_t holding_time;
  1134. eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(msg->content.eg_info.cache_id, mpc);
  1135. holding_time = msg->content.eg_info.holding_time;
  1136. dprintk("(%s) entry = %p, holding_time = %u\n",
  1137. mpc->dev->name, entry, holding_time);
  1138. if (entry == NULL && holding_time) {
  1139. entry = mpc->eg_ops->add_entry(msg, mpc);
  1140. mpc->eg_ops->put(entry);
  1141. return;
  1142. }
  1143. if (holding_time) {
  1144. mpc->eg_ops->update(entry, holding_time);
  1145. return;
  1146. }
  1147. write_lock_irq(&mpc->egress_lock);
  1148. mpc->eg_ops->remove_entry(entry, mpc);
  1149. write_unlock_irq(&mpc->egress_lock);
  1150. mpc->eg_ops->put(entry);
  1151. }
  1152. static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg,
  1153. struct mpoa_client *mpc)
  1154. {
  1155. struct lec_priv *priv;
  1156. int i, retval ;
  1157. uint8_t tlv[4 + 1 + 1 + 1 + ATM_ESA_LEN];
  1158. tlv[0] = 00; tlv[1] = 0xa0; tlv[2] = 0x3e; tlv[3] = 0x2a; /* type */
  1159. tlv[4] = 1 + 1 + ATM_ESA_LEN; /* length */
  1160. tlv[5] = 0x02; /* MPOA client */
  1161. tlv[6] = 0x00; /* number of MPS MAC addresses */
  1162. memcpy(&tlv[7], mesg->MPS_ctrl, ATM_ESA_LEN); /* MPC ctrl ATM addr */
  1163. memcpy(mpc->our_ctrl_addr, mesg->MPS_ctrl, ATM_ESA_LEN);
  1164. dprintk("(%s) setting MPC ctrl ATM address to",
  1165. mpc->dev ? mpc->dev->name : "<unknown>");
  1166. for (i = 7; i < sizeof(tlv); i++)
  1167. dprintk_cont(" %02x", tlv[i]);
  1168. dprintk_cont("\n");
  1169. if (mpc->dev) {
  1170. priv = netdev_priv(mpc->dev);
  1171. retval = priv->lane2_ops->associate_req(mpc->dev,
  1172. mpc->dev->dev_addr,
  1173. tlv, sizeof(tlv));
  1174. if (retval == 0)
  1175. pr_info("(%s) MPOA device type TLV association failed\n",
  1176. mpc->dev->name);
  1177. retval = priv->lane2_ops->resolve(mpc->dev, NULL, 1, NULL, NULL);
  1178. if (retval < 0)
  1179. pr_info("(%s) targetless LE_ARP request failed\n",
  1180. mpc->dev->name);
  1181. }
  1182. }
  1183. static void set_mps_mac_addr_rcvd(struct k_message *msg,
  1184. struct mpoa_client *client)
  1185. {
  1186. if (client->number_of_mps_macs)
  1187. kfree(client->mps_macs);
  1188. client->number_of_mps_macs = 0;
  1189. client->mps_macs = kmemdup(msg->MPS_ctrl, ETH_ALEN, GFP_KERNEL);
  1190. if (client->mps_macs == NULL) {
  1191. pr_info("out of memory\n");
  1192. return;
  1193. }
  1194. client->number_of_mps_macs = 1;
  1195. }
  1196. /*
  1197. * purge egress cache and tell daemon to 'action' (DIE, RELOAD)
  1198. */
  1199. static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action)
  1200. {
  1201. eg_cache_entry *entry;
  1202. msg->type = SND_EGRESS_PURGE;
  1203. /* FIXME: This knows too much of the cache structure */
  1204. read_lock_irq(&mpc->egress_lock);
  1205. entry = mpc->eg_cache;
  1206. while (entry != NULL) {
  1207. msg->content.eg_info = entry->ctrl_info;
  1208. dprintk("cache_id %u\n", entry->ctrl_info.cache_id);
  1209. msg_to_mpoad(msg, mpc);
  1210. entry = entry->next;
  1211. }
  1212. read_unlock_irq(&mpc->egress_lock);
  1213. msg->type = action;
  1214. msg_to_mpoad(msg, mpc);
  1215. }
  1216. static void mpc_timer_refresh(void)
  1217. {
  1218. mpc_timer.expires = jiffies + (MPC_P2 * HZ);
  1219. mpc_timer.data = mpc_timer.expires;
  1220. mpc_timer.function = mpc_cache_check;
  1221. add_timer(&mpc_timer);
  1222. }
  1223. static void mpc_cache_check(unsigned long checking_time)
  1224. {
  1225. struct mpoa_client *mpc = mpcs;
  1226. static unsigned long previous_resolving_check_time;
  1227. static unsigned long previous_refresh_time;
  1228. while (mpc != NULL) {
  1229. mpc->in_ops->clear_count(mpc);
  1230. mpc->eg_ops->clear_expired(mpc);
  1231. if (checking_time - previous_resolving_check_time >
  1232. mpc->parameters.mpc_p4 * HZ) {
  1233. mpc->in_ops->check_resolving(mpc);
  1234. previous_resolving_check_time = checking_time;
  1235. }
  1236. if (checking_time - previous_refresh_time >
  1237. mpc->parameters.mpc_p5 * HZ) {
  1238. mpc->in_ops->refresh(mpc);
  1239. previous_refresh_time = checking_time;
  1240. }
  1241. mpc = mpc->next;
  1242. }
  1243. mpc_timer_refresh();
  1244. }
  1245. static int atm_mpoa_ioctl(struct socket *sock, unsigned int cmd,
  1246. unsigned long arg)
  1247. {
  1248. int err = 0;
  1249. struct atm_vcc *vcc = ATM_SD(sock);
  1250. if (cmd != ATMMPC_CTRL && cmd != ATMMPC_DATA)
  1251. return -ENOIOCTLCMD;
  1252. if (!capable(CAP_NET_ADMIN))
  1253. return -EPERM;
  1254. switch (cmd) {
  1255. case ATMMPC_CTRL:
  1256. err = atm_mpoa_mpoad_attach(vcc, (int)arg);
  1257. if (err >= 0)
  1258. sock->state = SS_CONNECTED;
  1259. break;
  1260. case ATMMPC_DATA:
  1261. err = atm_mpoa_vcc_attach(vcc, (void __user *)arg);
  1262. break;
  1263. default:
  1264. break;
  1265. }
  1266. return err;
  1267. }
  1268. static struct atm_ioctl atm_ioctl_ops = {
  1269. .owner = THIS_MODULE,
  1270. .ioctl = atm_mpoa_ioctl,
  1271. };
  1272. static __init int atm_mpoa_init(void)
  1273. {
  1274. register_atm_ioctl(&atm_ioctl_ops);
  1275. if (mpc_proc_init() != 0)
  1276. pr_info("failed to initialize /proc/mpoa\n");
  1277. pr_info("mpc.c: initialized\n");
  1278. return 0;
  1279. }
  1280. static void __exit atm_mpoa_cleanup(void)
  1281. {
  1282. struct mpoa_client *mpc, *tmp;
  1283. struct atm_mpoa_qos *qos, *nextqos;
  1284. struct lec_priv *priv;
  1285. mpc_proc_clean();
  1286. del_timer_sync(&mpc_timer);
  1287. unregister_netdevice_notifier(&mpoa_notifier);
  1288. deregister_atm_ioctl(&atm_ioctl_ops);
  1289. mpc = mpcs;
  1290. mpcs = NULL;
  1291. while (mpc != NULL) {
  1292. tmp = mpc->next;
  1293. if (mpc->dev != NULL) {
  1294. stop_mpc(mpc);
  1295. priv = netdev_priv(mpc->dev);
  1296. if (priv->lane2_ops != NULL)
  1297. priv->lane2_ops->associate_indicator = NULL;
  1298. }
  1299. ddprintk("about to clear caches\n");
  1300. mpc->in_ops->destroy_cache(mpc);
  1301. mpc->eg_ops->destroy_cache(mpc);
  1302. ddprintk("caches cleared\n");
  1303. kfree(mpc->mps_macs);
  1304. memset(mpc, 0, sizeof(struct mpoa_client));
  1305. ddprintk("about to kfree %p\n", mpc);
  1306. kfree(mpc);
  1307. ddprintk("next mpc is at %p\n", tmp);
  1308. mpc = tmp;
  1309. }
  1310. qos = qos_head;
  1311. qos_head = NULL;
  1312. while (qos != NULL) {
  1313. nextqos = qos->next;
  1314. dprintk("freeing qos entry %p\n", qos);
  1315. kfree(qos);
  1316. qos = nextqos;
  1317. }
  1318. }
  1319. module_init(atm_mpoa_init);
  1320. module_exit(atm_mpoa_cleanup);
  1321. MODULE_LICENSE("GPL");