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