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