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