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