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