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