more debug output
[linux-2.4.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
7 /* We are an ethernet device */
8 #include <linux/if_ether.h>
9 #include <linux/netdevice.h>
10 #include <linux/etherdevice.h>
11 #include <net/sock.h>
12 #include <linux/skbuff.h>
13 #include <linux/ip.h>
14 #include <asm/byteorder.h>
15 #include <asm/uaccess.h>
16 #include <asm/checksum.h>   /* for ip_fast_csum() */
17 #include <net/arp.h>
18 #include <net/dst.h>
19 #include <linux/proc_fs.h>
20
21 /* And atm device */
22 #include <linux/atmdev.h>
23 #include <linux/atmlec.h>
24 #include <linux/atmmpc.h>
25 /* Modular too */
26 #include <linux/config.h>
27 #include <linux/module.h>
28
29 #include "lec.h"
30 #include "mpc.h"
31 #include "resources.h"
32
33 /*
34  * mpc.c: Implementation of MPOA client kernel part 
35  */
36
37 #if 0
38 #define dprintk printk   /* debug */
39 #else
40 #define dprintk(format,args...)
41 #endif
42
43 #if 0
44 #define ddprintk printk  /* more debug */
45 #else
46 #define ddprintk(format,args...)
47 #endif
48
49
50
51 #define MPOA_TAG_LEN 4
52
53 /* mpc_daemon -> kernel */
54 static void MPOA_trigger_rcvd (struct k_message *msg, struct mpoa_client *mpc);
55 static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc);
56 static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
57 static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
58 static void mps_death(struct k_message *msg, struct mpoa_client *mpc);
59 static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action);
60 static void MPOA_cache_impos_rcvd(struct k_message *msg, struct mpoa_client *mpc);
61 static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg, struct mpoa_client *mpc);
62 static void set_mps_mac_addr_rcvd(struct k_message *mesg, struct mpoa_client *mpc);
63
64 static uint8_t *copy_macs(struct mpoa_client *mpc, uint8_t *router_mac,
65                           uint8_t *tlvs, uint8_t mps_macs, uint8_t device_type);
66 static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry);
67
68 static void send_set_mps_ctrl_addr(char *addr, struct mpoa_client *mpc);
69 static void mpoad_close(struct atm_vcc *vcc);
70 static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb);
71
72 static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb);
73 static int mpc_send_packet(struct sk_buff *skb, struct net_device *dev);
74 static int mpoa_event_listener(struct notifier_block *mpoa_notifier, unsigned long event, void *dev);
75 static void mpc_timer_refresh(void);
76 static void mpc_cache_check( unsigned long checking_time  );
77
78 static struct llc_snap_hdr llc_snap_mpoa_ctrl = {
79         0xaa, 0xaa, 0x03,
80         {0x00, 0x00, 0x5e},
81         {0x00, 0x03}         /* For MPOA control PDUs */
82 };        
83 static struct llc_snap_hdr llc_snap_mpoa_data = {
84         0xaa, 0xaa, 0x03,
85         {0x00, 0x00, 0x00},
86         {0x08, 0x00}         /* This is for IP PDUs only */
87 };        
88 static struct llc_snap_hdr llc_snap_mpoa_data_tagged = {
89         0xaa, 0xaa, 0x03,
90         {0x00, 0x00, 0x00},
91         {0x88, 0x4c}         /* This is for tagged data PDUs */
92 };        
93
94 static struct notifier_block mpoa_notifier = {
95         mpoa_event_listener,
96         NULL,
97         0
98 };
99
100 #ifdef CONFIG_PROC_FS
101 extern int mpc_proc_init(void);
102 extern void mpc_proc_clean(void);
103 #endif
104
105 struct mpoa_client *mpcs = NULL; /* FIXME */
106 static struct atm_mpoa_qos *qos_head = NULL;
107 static struct timer_list mpc_timer;
108
109
110 static struct mpoa_client *find_mpc_by_itfnum(int itf)
111 {
112         struct mpoa_client *mpc;
113         
114         mpc = mpcs;  /* our global linked list */
115         while (mpc != NULL) {
116                 if (mpc->dev_num == itf)
117                         return mpc;
118                 mpc = mpc->next;    
119         }
120
121         return NULL;   /* not found */
122 }
123
124 static struct mpoa_client *find_mpc_by_vcc(struct atm_vcc *vcc)
125 {
126         struct mpoa_client *mpc;
127         
128         mpc = mpcs;  /* our global linked list */
129         while (mpc != NULL) {
130                 if (mpc->mpoad_vcc == vcc)
131                         return mpc;
132                 mpc = mpc->next;
133         }
134
135         return NULL;   /* not found */
136 }
137
138 static struct mpoa_client *find_mpc_by_lec(struct net_device *dev)
139 {
140         struct mpoa_client *mpc;
141         
142         mpc = mpcs;  /* our global linked list */
143         while (mpc != NULL) {
144                 if (mpc->dev == dev)
145                         return mpc;
146                 mpc = mpc->next;
147         }
148
149         return NULL;   /* not found */
150 }
151
152 /*
153  * Functions for managing QoS list
154  */
155
156 /*
157  * Overwrites the old entry or makes a new one.
158  */
159 struct atm_mpoa_qos *atm_mpoa_add_qos(uint32_t dst_ip, struct atm_qos *qos)
160 {
161         struct atm_mpoa_qos *entry;
162
163         entry = atm_mpoa_search_qos(dst_ip);
164         if (entry != NULL) {
165                 entry->qos = *qos;
166                 return entry;
167         }
168
169         entry = kmalloc(sizeof(struct atm_mpoa_qos), GFP_KERNEL);
170         if (entry == NULL) {
171                 printk("mpoa: atm_mpoa_add_qos: out of memory\n");
172                 return entry;
173         }
174
175         entry->ipaddr = dst_ip;
176         entry->qos = *qos;
177
178         entry->next = qos_head;
179         qos_head = entry;
180
181         return entry;
182 }
183
184 struct atm_mpoa_qos *atm_mpoa_search_qos(uint32_t dst_ip)
185 {
186         struct atm_mpoa_qos *qos;
187
188         qos = qos_head;
189         while( qos != NULL ){
190                 if(qos->ipaddr == dst_ip) {
191                         break;
192                 }
193                 qos = qos->next;
194         }
195
196         return qos;
197 }        
198
199 /*
200  * Returns 0 for failure
201  */
202 int atm_mpoa_delete_qos(struct atm_mpoa_qos *entry)
203 {
204
205         struct atm_mpoa_qos *curr;
206
207         if (entry == NULL) return 0;
208         if (entry == qos_head) {
209                 qos_head = qos_head->next;
210                 kfree(entry);
211                 return 1;
212         }
213
214         curr = qos_head;
215         while (curr != NULL) {
216                 if (curr->next == entry) {
217                         curr->next = entry->next;
218                         kfree(entry);
219                         return 1;
220                 }
221                 curr = curr->next;
222         }
223
224         return 0;
225 }
226
227 void atm_mpoa_disp_qos(char *page, int *len)
228 {
229
230         unsigned char *ip;
231         char ipaddr[16];
232         struct atm_mpoa_qos *qos;
233
234         qos = qos_head;
235         *len += sprintf(page + *len, "QoS entries for shortcuts:\n");
236         *len += sprintf(page + *len, "IP address\n  TX:max_pcr pcr     min_pcr max_cdv max_sdu\n  RX:max_pcr pcr     min_pcr max_cdv max_sdu\n");
237
238         ipaddr[sizeof(ipaddr)-1] = '\0';
239         while (qos != NULL) {
240                 ip = (unsigned char *)&qos->ipaddr;
241                 sprintf(ipaddr, "%u.%u.%u.%u", NIPQUAD(ip));
242                 *len += sprintf(page + *len, "%u.%u.%u.%u\n     %-7d %-7d %-7d %-7d %-7d\n     %-7d %-7d %-7d %-7d %-7d\n",
243                                 NIPQUAD(ipaddr),
244                                 qos->qos.txtp.max_pcr, qos->qos.txtp.pcr, qos->qos.txtp.min_pcr, qos->qos.txtp.max_cdv, qos->qos.txtp.max_sdu,
245                                 qos->qos.rxtp.max_pcr, qos->qos.rxtp.pcr, qos->qos.rxtp.min_pcr, qos->qos.rxtp.max_cdv, qos->qos.rxtp.max_sdu);
246                 qos = qos->next;
247         }
248         
249         return;
250 }
251
252 static struct net_device *find_lec_by_itfnum(int itf)
253 {
254         struct net_device *dev;
255         if (!try_atm_lane_ops())
256                 return NULL;
257
258         dev = atm_lane_ops->get_lec(itf);
259         if (atm_lane_ops->owner)
260                 __MOD_DEC_USE_COUNT(atm_lane_ops->owner);
261         return dev;
262 }
263
264 static struct mpoa_client *alloc_mpc(void)
265 {
266         struct mpoa_client *mpc;
267
268         mpc = kmalloc(sizeof (struct mpoa_client), GFP_KERNEL);
269         if (mpc == NULL)
270                 return NULL;
271         memset(mpc, 0, sizeof(struct mpoa_client));
272         mpc->ingress_lock = RW_LOCK_UNLOCKED;
273         mpc->egress_lock  = RW_LOCK_UNLOCKED;
274         mpc->next = mpcs;
275         atm_mpoa_init_cache(mpc);
276
277         mpc->parameters.mpc_p1 = MPC_P1;
278         mpc->parameters.mpc_p2 = MPC_P2;
279         memset(mpc->parameters.mpc_p3,0,sizeof(mpc->parameters.mpc_p3));
280         mpc->parameters.mpc_p4 = MPC_P4;
281         mpc->parameters.mpc_p5 = MPC_P5; 
282         mpc->parameters.mpc_p6 = MPC_P6;
283         
284         mpcs = mpc;
285         
286         return mpc;
287 }
288
289 /*
290  *
291  * start_mpc() puts the MPC on line. All the packets destined
292  * to the lec underneath us are now being monitored and 
293  * shortcuts will be established.
294  *
295  */
296 static void start_mpc(struct mpoa_client *mpc, struct net_device *dev)
297 {
298         
299         dprintk("mpoa: (%s) start_mpc:\n", mpc->dev->name); 
300         if (dev->hard_start_xmit == NULL) {
301                 printk("mpoa: (%s) start_mpc: dev->hard_start_xmit == NULL, not starting\n",
302                        dev->name);
303                 return;
304         }
305         mpc->old_hard_start_xmit = dev->hard_start_xmit;
306         dev->hard_start_xmit = mpc_send_packet;
307
308         return;
309 }
310
311 static void stop_mpc(struct mpoa_client *mpc)
312 {
313         
314         dprintk("mpoa: (%s) stop_mpc:", mpc->dev->name); 
315
316         /* Lets not nullify lec device's dev->hard_start_xmit */
317         if (mpc->dev->hard_start_xmit != mpc_send_packet) {
318                 dprintk(" mpc already stopped, not fatal\n");
319                 return;
320         }
321         dprintk("\n");
322         mpc->dev->hard_start_xmit = mpc->old_hard_start_xmit;
323         mpc->old_hard_start_xmit = NULL;
324         /* close_shortcuts(mpc);    ??? FIXME */
325         
326         return;
327 }
328
329 static const char * __attribute__ ((unused)) mpoa_device_type_string(char type)
330 {
331         switch(type) {
332         case NON_MPOA:
333                 return "non-MPOA device";
334                 break;
335         case MPS:
336                 return "MPS";
337                 break;
338         case MPC:
339                 return "MPC";
340                 break;
341         case MPS_AND_MPC:
342                 return "both MPS and MPC";
343                 break;
344         default:
345                 return "unspecified (non-MPOA) device";
346                 break;
347         }
348
349         return ""; /* not reached */
350 }
351
352 /*
353  * lec device calls this via its dev->priv->lane2_ops->associate_indicator()
354  * when it sees a TLV in LE_ARP packet.
355  * We fill in the pointer above when we see a LANE2 lec initializing
356  * See LANE2 spec 3.1.5
357  *
358  * Quite a big and ugly function but when you look at it
359  * all it does is to try to locate and parse MPOA Device
360  * Type TLV.
361  * We give our lec a pointer to this function and when the
362  * lec sees a TLV it uses the pointer to call this function.
363  *
364  */
365 static void lane2_assoc_ind(struct net_device *dev, uint8_t *mac_addr,
366                             uint8_t *tlvs, uint32_t sizeoftlvs)
367 {
368         uint32_t type;
369         uint8_t length, mpoa_device_type, number_of_mps_macs;
370         uint8_t *end_of_tlvs;
371         struct mpoa_client *mpc;
372         
373         mpoa_device_type = number_of_mps_macs = 0; /* silence gcc */
374         dprintk("mpoa: (%s) lane2_assoc_ind: received TLV(s), ", dev->name);
375         dprintk("total length of all TLVs %d\n", sizeoftlvs);
376         mpc = find_mpc_by_lec(dev); /* Sampo-Fix: moved here from below */
377         if (mpc == NULL) {
378                 printk("mpoa: (%s) lane2_assoc_ind: no mpc\n", dev->name);
379                 return;
380         }
381         end_of_tlvs = tlvs + sizeoftlvs;
382         while (end_of_tlvs - tlvs >= 5) {
383                 type = (tlvs[0] << 24) | (tlvs[1] << 16) | (tlvs[2] << 8) | tlvs[3];
384                 length = tlvs[4];
385                 tlvs += 5;
386                 dprintk("    type 0x%x length %02x\n", type, length);
387                 if (tlvs + length > end_of_tlvs) {
388                         printk("TLV value extends past its buffer, aborting parse\n");
389                         return;
390                 }
391                 
392                 if (type == 0) {
393                         printk("mpoa: (%s) lane2_assoc_ind: TLV type was 0, returning\n", dev->name);
394                         return;
395                 }
396
397                 if (type != TLV_MPOA_DEVICE_TYPE) {
398                         tlvs += length;
399                         continue;  /* skip other TLVs */
400                 }
401                 mpoa_device_type = *tlvs++;
402                 number_of_mps_macs = *tlvs++;
403                 dprintk("mpoa: (%s) MPOA device type '%s', ", dev->name, mpoa_device_type_string(mpoa_device_type));
404                 if (mpoa_device_type == MPS_AND_MPC &&
405                     length < (42 + number_of_mps_macs*ETH_ALEN)) { /* :) */
406                         printk("\nmpoa: (%s) lane2_assoc_ind: short MPOA Device Type TLV\n",
407                                dev->name);
408                         continue;
409                 }
410                 if ((mpoa_device_type == MPS || mpoa_device_type == MPC)
411                     && length < 22 + number_of_mps_macs*ETH_ALEN) {
412                         printk("\nmpoa: (%s) lane2_assoc_ind: short MPOA Device Type TLV\n",
413                                 dev->name);
414                         continue;
415                 }
416                 if (mpoa_device_type != MPS && mpoa_device_type != MPS_AND_MPC) {
417                         dprintk("ignoring non-MPS device\n");
418                         if (mpoa_device_type == MPC) tlvs += 20;
419                         continue;  /* we are only interested in MPSs */
420                 }
421                 if (number_of_mps_macs == 0 && mpoa_device_type == MPS_AND_MPC) {
422                         printk("\nmpoa: (%s) lane2_assoc_ind: MPS_AND_MPC has zero MACs\n", dev->name);
423                         continue;  /* someone should read the spec */
424                 }
425                 dprintk("this MPS has %d MAC addresses\n", number_of_mps_macs);
426                 
427                 /* ok, now we can go and tell our daemon the control address of MPS */
428                 send_set_mps_ctrl_addr(tlvs, mpc);
429                 
430                 tlvs = copy_macs(mpc, mac_addr, tlvs, number_of_mps_macs, mpoa_device_type);
431                 if (tlvs == NULL) return;
432         }
433         if (end_of_tlvs - tlvs != 0)
434                 printk("mpoa: (%s) lane2_assoc_ind: ignoring %d bytes of trailing TLV carbage\n",
435                        dev->name, end_of_tlvs - tlvs);
436         return;
437 }
438
439 /*
440  * Store at least advertizing router's MAC address
441  * plus the possible MAC address(es) to mpc->mps_macs.
442  * For a freshly allocated MPOA client mpc->mps_macs == 0.
443  */
444 static uint8_t *copy_macs(struct mpoa_client *mpc, uint8_t *router_mac,
445                           uint8_t *tlvs, uint8_t mps_macs, uint8_t device_type)
446 {
447         int num_macs;
448         num_macs = (mps_macs > 1) ? mps_macs : 1;
449
450         if (mpc->number_of_mps_macs != num_macs) { /* need to reallocate? */
451                 if (mpc->number_of_mps_macs != 0) kfree(mpc->mps_macs);
452                 mpc->number_of_mps_macs = 0;
453                 mpc->mps_macs = kmalloc(num_macs*ETH_ALEN, GFP_KERNEL);
454                 if (mpc->mps_macs == NULL) {
455                         printk("mpoa: (%s) copy_macs: out of mem\n", mpc->dev->name);
456                         return NULL;
457                 }
458         }
459         memcpy(mpc->mps_macs, router_mac, ETH_ALEN);
460         tlvs += 20; if (device_type == MPS_AND_MPC) tlvs += 20;
461         if (mps_macs > 0)
462                 memcpy(mpc->mps_macs, tlvs, mps_macs*ETH_ALEN);
463         tlvs += mps_macs*ETH_ALEN;
464         mpc->number_of_mps_macs = num_macs;
465
466         return tlvs;
467 }
468
469 static int send_via_shortcut(struct sk_buff *skb, struct mpoa_client *mpc)
470 {
471         in_cache_entry *entry;
472         struct iphdr *iph;
473         char *buff;
474         uint32_t ipaddr = 0;
475
476         static struct {
477                 struct llc_snap_hdr hdr;
478                 uint32_t tag;
479         } tagged_llc_snap_hdr = {
480                 {0xaa, 0xaa, 0x03, {0x00, 0x00, 0x00}, {0x88, 0x4c}},
481                 0
482         };
483
484         buff = skb->data + mpc->dev->hard_header_len;
485         iph = (struct iphdr *)buff;
486         ipaddr = iph->daddr;
487
488         ddprintk("mpoa: (%s) send_via_shortcut: ipaddr 0x%x\n", mpc->dev->name, ipaddr);        
489
490         entry = mpc->in_ops->get(ipaddr, mpc);
491         if (entry == NULL) {
492                 entry = mpc->in_ops->add_entry(ipaddr, mpc);
493                 if (entry != NULL) mpc->in_ops->put(entry);
494                 return 1;
495         }
496         if (mpc->in_ops->cache_hit(entry, mpc) != OPEN){   /* threshold not exceeded or VCC not ready */
497                 ddprintk("mpoa: (%s) send_via_shortcut: cache_hit: returns != OPEN\n", mpc->dev->name);        
498                 mpc->in_ops->put(entry);
499                 return 1;
500         }
501
502         ddprintk("mpoa: (%s) send_via_shortcut: using shortcut\n", mpc->dev->name);        
503         /* MPOA spec A.1.4, MPOA client must decrement IP ttl at least by one */
504         if (iph->ttl <= 1) {
505                 ddprintk("mpoa: (%s) send_via_shortcut: IP ttl = %u, using LANE\n", mpc->dev->name, iph->ttl);        
506                 mpc->in_ops->put(entry);
507                 return 1;
508         }
509         iph->ttl--;
510         iph->check = 0;
511         iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
512
513         if (entry->ctrl_info.tag != 0) {
514                 ddprintk("mpoa: (%s) send_via_shortcut: adding tag 0x%x\n", mpc->dev->name, entry->ctrl_info.tag);
515                 tagged_llc_snap_hdr.tag = entry->ctrl_info.tag;
516                 skb_pull(skb, ETH_HLEN);                       /* get rid of Eth header */
517                 skb_push(skb, sizeof(tagged_llc_snap_hdr));    /* add LLC/SNAP header   */
518                 memcpy(skb->data, &tagged_llc_snap_hdr, sizeof(tagged_llc_snap_hdr));
519         } else {
520                 skb_pull(skb, ETH_HLEN);                        /* get rid of Eth header */
521                 skb_push(skb, sizeof(struct llc_snap_hdr));     /* add LLC/SNAP header + tag  */
522                 memcpy(skb->data, &llc_snap_mpoa_data, sizeof(struct llc_snap_hdr));
523         }
524
525         atomic_add(skb->truesize, &entry->shortcut->sk->wmem_alloc);
526         ATM_SKB(skb)->atm_options = entry->shortcut->atm_options;
527         entry->shortcut->send(entry->shortcut, skb);
528         entry->packets_fwded++;
529         mpc->in_ops->put(entry);
530
531         return 0;
532 }
533
534 /*
535  * Probably needs some error checks and locking, not sure...
536  */
537 static int mpc_send_packet(struct sk_buff *skb, struct net_device *dev)
538 {
539         int retval;
540         struct mpoa_client *mpc;
541         struct ethhdr *eth;
542         int i = 0;
543         
544         mpc = find_mpc_by_lec(dev); /* this should NEVER fail */
545         if(mpc == NULL) {
546                 printk("mpoa: (%s) mpc_send_packet: no MPC found\n", dev->name);
547                 goto non_ip;
548         }
549
550         eth = (struct ethhdr *)skb->data;
551         if (eth->h_proto != htons(ETH_P_IP))
552                 goto non_ip; /* Multi-Protocol Over ATM :-) */
553
554         while (i < mpc->number_of_mps_macs) {
555                 if (memcmp(eth->h_dest, (mpc->mps_macs + i*ETH_ALEN), ETH_ALEN) == 0)
556                         if ( send_via_shortcut(skb, mpc) == 0 )           /* try shortcut */
557                                 return 0;                                 /* success!     */
558                 i++;
559         }
560
561  non_ip:
562         retval = mpc->old_hard_start_xmit(skb,dev);
563         
564         return retval;
565 }
566
567 int atm_mpoa_vcc_attach(struct atm_vcc *vcc, long arg)
568 {
569         int bytes_left;
570         struct mpoa_client *mpc;
571         struct atmmpc_ioc ioc_data;
572         in_cache_entry *in_entry;
573         uint32_t  ipaddr;
574         unsigned char *ip;
575
576         bytes_left = copy_from_user(&ioc_data, (void *)arg, sizeof(struct atmmpc_ioc));
577         if (bytes_left != 0) {
578                 printk("mpoa: mpc_vcc_attach: Short read (missed %d bytes) from userland\n", bytes_left);
579                 return -EFAULT;
580         }
581         ipaddr = ioc_data.ipaddr;
582         if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF)
583                 return -EINVAL;
584         
585         mpc = find_mpc_by_itfnum(ioc_data.dev_num);
586         if (mpc == NULL)
587                 return -EINVAL;
588         
589         if (ioc_data.type == MPC_SOCKET_INGRESS) {
590                 in_entry = mpc->in_ops->get(ipaddr, mpc);
591                 if (in_entry == NULL || in_entry->entry_state < INGRESS_RESOLVED) {
592                         printk("mpoa: (%s) mpc_vcc_attach: did not find RESOLVED entry from ingress cache\n",
593                                 mpc->dev->name);
594                         if (in_entry != NULL) mpc->in_ops->put(in_entry);
595                         return -EINVAL;
596                 }
597                 ip = (unsigned char*)&in_entry->ctrl_info.in_dst_ip;
598                 printk("mpoa: (%s) mpc_vcc_attach: attaching ingress SVC, entry = %u.%u.%u.%u\n",
599                        mpc->dev->name, ip[0], ip[1], ip[2], ip[3]);
600                 in_entry->shortcut = vcc;
601                 mpc->in_ops->put(in_entry);
602         } else {
603                 printk("mpoa: (%s) mpc_vcc_attach: attaching egress SVC\n", mpc->dev->name);
604         }
605
606         vcc->proto_data = mpc->dev;
607         vcc->push = mpc_push;
608
609         return 0;
610 }
611
612 /*
613  *
614  */
615 static void mpc_vcc_close(struct atm_vcc *vcc, struct net_device *dev)
616 {
617         struct mpoa_client *mpc;
618         in_cache_entry *in_entry;
619         eg_cache_entry *eg_entry;
620         
621         mpc = find_mpc_by_lec(dev);
622         if (mpc == NULL) {
623                 printk("mpoa: (%s) mpc_vcc_close: close for unknown MPC\n", dev->name);
624                 return;
625         }
626
627         dprintk("mpoa: (%s) mpc_vcc_close:\n", dev->name);
628         in_entry = mpc->in_ops->get_by_vcc(vcc, mpc);
629         if (in_entry) {
630                 unsigned char *ip __attribute__ ((unused)) =
631                     (unsigned char *)&in_entry->ctrl_info.in_dst_ip;
632                 dprintk("mpoa: (%s) mpc_vcc_close: ingress SVC closed ip = %u.%u.%u.%u\n",
633                        mpc->dev->name, ip[0], ip[1], ip[2], ip[3]);
634                 in_entry->shortcut = NULL;
635                 mpc->in_ops->put(in_entry);
636         }
637         eg_entry = mpc->eg_ops->get_by_vcc(vcc, mpc);
638         if (eg_entry) {
639                 dprintk("mpoa: (%s) mpc_vcc_close: egress SVC closed\n", mpc->dev->name);
640                 eg_entry->shortcut = NULL;
641                 mpc->eg_ops->put(eg_entry);
642         }
643
644         if (in_entry == NULL && eg_entry == NULL)
645                 dprintk("mpoa: (%s) mpc_vcc_close:  unused vcc closed\n", dev->name);
646
647         return;
648 }
649
650 static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb)
651 {
652         struct net_device *dev = (struct net_device *)vcc->proto_data;
653         struct sk_buff *new_skb;
654         eg_cache_entry *eg;
655         struct mpoa_client *mpc;
656         uint32_t tag;
657         char *tmp;
658         
659         ddprintk("mpoa: (%s) mpc_push:\n", dev->name);
660         if (skb == NULL) {
661                 dprintk("mpoa: (%s) mpc_push: null skb, closing VCC\n", dev->name);
662                 mpc_vcc_close(vcc, dev);
663                 return;
664         }
665         
666         skb->dev = dev;
667         if (memcmp(skb->data, &llc_snap_mpoa_ctrl, sizeof(struct llc_snap_hdr)) == 0) {
668                 dprintk("mpoa: (%s) mpc_push: control packet arrived\n", dev->name);
669                 skb_queue_tail(&vcc->sk->receive_queue, skb);           /* Pass control packets to daemon */
670                 wake_up(&vcc->sleep);
671                 return;
672         }
673
674         /* data coming over the shortcut */
675         atm_return(vcc, skb->truesize);
676
677         mpc = find_mpc_by_lec(dev);
678         if (mpc == NULL) {
679                 printk("mpoa: (%s) mpc_push: unknown MPC\n", dev->name);
680                 return;
681         }
682
683         if (memcmp(skb->data, &llc_snap_mpoa_data_tagged, sizeof(struct llc_snap_hdr)) == 0) { /* MPOA tagged data */
684                 ddprintk("mpoa: (%s) mpc_push: tagged data packet arrived\n", dev->name);
685
686         } else if (memcmp(skb->data, &llc_snap_mpoa_data, sizeof(struct llc_snap_hdr)) == 0) { /* MPOA data */
687                 printk("mpoa: (%s) mpc_push: non-tagged data packet arrived\n", dev->name);
688                 printk("           mpc_push: non-tagged data unsupported, purging\n");
689                 dev_kfree_skb_any(skb);
690                 return;
691         } else {
692                 printk("mpoa: (%s) mpc_push: garbage arrived, purging\n", dev->name);
693                 dev_kfree_skb_any(skb);
694                 return;
695         }
696
697         tmp = skb->data + sizeof(struct llc_snap_hdr);
698         tag = *(uint32_t *)tmp;
699
700         eg = mpc->eg_ops->get_by_tag(tag, mpc);
701         if (eg == NULL) {
702                 printk("mpoa: (%s) mpc_push: Didn't find egress cache entry, tag = %u\n",
703                        dev->name,tag);
704                 purge_egress_shortcut(vcc, NULL);
705                 dev_kfree_skb_any(skb);
706                 return;
707         }
708         
709         /*
710          * See if ingress MPC is using shortcut we opened as a return channel.
711          * This means we have a bi-directional vcc opened by us.
712          */ 
713         if (eg->shortcut == NULL) {
714                 eg->shortcut = vcc;
715                 printk("mpoa: (%s) mpc_push: egress SVC in use\n", dev->name);
716         }
717
718         skb_pull(skb, sizeof(struct llc_snap_hdr) + sizeof(tag)); /* get rid of LLC/SNAP header */
719         new_skb = skb_realloc_headroom(skb, eg->ctrl_info.DH_length); /* LLC/SNAP is shorter than MAC header :( */
720         dev_kfree_skb_any(skb);
721         if (new_skb == NULL){
722                 mpc->eg_ops->put(eg);
723                 return;
724         }
725         skb_push(new_skb, eg->ctrl_info.DH_length);     /* add MAC header */
726         memcpy(new_skb->data, eg->ctrl_info.DLL_header, eg->ctrl_info.DH_length);
727         new_skb->protocol = eth_type_trans(new_skb, dev);
728         new_skb->nh.raw = new_skb->data;
729
730         eg->latest_ip_addr = new_skb->nh.iph->saddr;
731         eg->packets_rcvd++;
732         mpc->eg_ops->put(eg);
733
734         memset(ATM_SKB(new_skb), 0, sizeof(struct atm_skb_data));
735         netif_rx(new_skb);
736
737         return;
738 }
739
740 static struct atmdev_ops mpc_ops = { /* only send is required */
741         .close  = mpoad_close,
742         .send   = msg_from_mpoad
743 };
744
745 static struct atm_dev mpc_dev = {
746         .ops    = &mpc_ops,
747         .type   = "mpc",
748         .number = 42,
749         .lock   = SPIN_LOCK_UNLOCKED
750         /* rest of the members will be 0 */
751 };
752
753 int atm_mpoa_mpoad_attach (struct atm_vcc *vcc, int arg)
754 {
755         struct mpoa_client *mpc;
756         struct lec_priv *priv;
757         
758         if (mpcs == NULL) {
759                 init_timer(&mpc_timer);
760                 mpc_timer_refresh();
761
762                 /* This lets us now how our LECs are doing */
763                 register_netdevice_notifier(&mpoa_notifier);
764         }
765         
766         mpc = find_mpc_by_itfnum(arg);
767         if (mpc == NULL) {
768                 dprintk("mpoa: mpoad_attach: allocating new mpc for itf %d\n", arg);
769                 mpc = alloc_mpc();
770                 if (mpc == NULL)
771                         return -ENOMEM;
772                 mpc->dev_num = arg;
773                 mpc->dev = find_lec_by_itfnum(arg); /* NULL if there was no lec */
774         }
775         if (mpc->mpoad_vcc) {
776                 printk("mpoa: mpoad_attach: mpoad is already present for itf %d\n", arg);
777                 return -EADDRINUSE;
778         }
779
780         if (mpc->dev) { /* check if the lec is LANE2 capable */
781                 priv = (struct lec_priv *)mpc->dev->priv;
782                 if (priv->lane_version < 2) {
783                         dev_put(mpc->dev);
784                         mpc->dev = NULL;
785                 } else
786                         priv->lane2_ops->associate_indicator = lane2_assoc_ind;  
787         }
788
789         mpc->mpoad_vcc = vcc;
790         vcc->dev = &mpc_dev;
791         vcc_insert_socket(vcc->sk);
792         set_bit(ATM_VF_META,&vcc->flags);
793         set_bit(ATM_VF_READY,&vcc->flags);
794
795         if (mpc->dev) {
796                 char empty[ATM_ESA_LEN];
797                 memset(empty, 0, ATM_ESA_LEN);
798                 
799                 start_mpc(mpc, mpc->dev);
800                 /* set address if mpcd e.g. gets killed and restarted.
801                  * If we do not do it now we have to wait for the next LE_ARP
802                  */
803                 if ( memcmp(mpc->mps_ctrl_addr, empty, ATM_ESA_LEN) != 0 )
804                         send_set_mps_ctrl_addr(mpc->mps_ctrl_addr, mpc);
805         }
806
807         MOD_INC_USE_COUNT;
808         return arg;
809 }
810
811 static void send_set_mps_ctrl_addr(char *addr, struct mpoa_client *mpc)
812 {
813         struct k_message mesg;
814
815         memcpy (mpc->mps_ctrl_addr, addr, ATM_ESA_LEN);
816         
817         mesg.type = SET_MPS_CTRL_ADDR;
818         memcpy(mesg.MPS_ctrl, addr, ATM_ESA_LEN);
819         msg_to_mpoad(&mesg, mpc);
820
821         return;
822 }
823
824 static void mpoad_close(struct atm_vcc *vcc)
825 {
826         struct mpoa_client *mpc;
827         struct sk_buff *skb;
828
829         mpc = find_mpc_by_vcc(vcc);
830         if (mpc == NULL) {
831                 printk("mpoa: mpoad_close: did not find MPC\n");
832                 return;
833         }
834         if (!mpc->mpoad_vcc) {
835                 printk("mpoa: mpoad_close: close for non-present mpoad\n");
836                 return;
837         }
838         
839         mpc->mpoad_vcc = NULL;
840         if (mpc->dev) {
841                 struct lec_priv *priv = (struct lec_priv *)mpc->dev->priv;
842                 priv->lane2_ops->associate_indicator = NULL;
843                 stop_mpc(mpc);
844                 dev_put(mpc->dev);
845         }
846
847         mpc->in_ops->destroy_cache(mpc);
848         mpc->eg_ops->destroy_cache(mpc);
849
850         while ( (skb = skb_dequeue(&vcc->sk->receive_queue)) ){
851                 atm_return(vcc, skb->truesize);
852                 kfree_skb(skb);
853         }
854         
855         printk("mpoa: (%s) going down\n",
856                 (mpc->dev) ? mpc->dev->name : "<unknown>");
857         MOD_DEC_USE_COUNT;
858
859         return;
860 }
861
862 /*
863  *
864  */
865 static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb)
866 {
867         
868         struct mpoa_client *mpc = find_mpc_by_vcc(vcc);
869         struct k_message *mesg = (struct k_message*)skb->data;
870         atomic_sub(skb->truesize, &vcc->sk->wmem_alloc);
871         
872         if (mpc == NULL) {
873                 printk("mpoa: msg_from_mpoad: no mpc found\n");
874                 return 0;
875         }
876         dprintk("mpoa: (%s) msg_from_mpoad:", (mpc->dev) ? mpc->dev->name : "<unknown>");
877         switch(mesg->type) {
878         case MPOA_RES_REPLY_RCVD:
879                 dprintk(" mpoa_res_reply_rcvd\n");
880                 MPOA_res_reply_rcvd(mesg, mpc);
881                 break;
882         case MPOA_TRIGGER_RCVD:
883                 dprintk(" mpoa_trigger_rcvd\n");
884                 MPOA_trigger_rcvd(mesg, mpc);
885                 break;
886         case INGRESS_PURGE_RCVD:
887                 dprintk(" nhrp_purge_rcvd\n");
888                 ingress_purge_rcvd(mesg, mpc);
889                 break;
890         case EGRESS_PURGE_RCVD:
891                 dprintk(" egress_purge_reply_rcvd\n");
892                 egress_purge_rcvd(mesg, mpc);
893                 break;
894         case MPS_DEATH:
895                 dprintk(" mps_death\n");
896                 mps_death(mesg, mpc);
897                 break;
898         case CACHE_IMPOS_RCVD:
899                 dprintk(" cache_impos_rcvd\n");
900                 MPOA_cache_impos_rcvd(mesg, mpc);
901                 break;
902         case SET_MPC_CTRL_ADDR:
903                 dprintk(" set_mpc_ctrl_addr\n");
904                 set_mpc_ctrl_addr_rcvd(mesg, mpc);
905                 break;
906         case SET_MPS_MAC_ADDR:
907                 dprintk(" set_mps_mac_addr\n");
908                 set_mps_mac_addr_rcvd(mesg, mpc);
909                 break;
910         case CLEAN_UP_AND_EXIT:
911                 dprintk(" clean_up_and_exit\n");
912                 clean_up(mesg, mpc, DIE);
913                 break;
914         case RELOAD:
915                 dprintk(" reload\n");
916                 clean_up(mesg, mpc, RELOAD);
917                 break;
918         case SET_MPC_PARAMS:
919                 dprintk(" set_mpc_params\n");
920                 mpc->parameters = mesg->content.params;
921                 break;
922         default:
923                 dprintk(" unknown message %d\n", mesg->type);
924                 break;
925         }
926         kfree_skb(skb);
927
928         return 0;
929 }
930
931 /* Remember that this function may not do things that sleep */
932 int msg_to_mpoad(struct k_message *mesg, struct mpoa_client *mpc)
933 {
934         struct sk_buff *skb;
935
936         if (mpc == NULL || !mpc->mpoad_vcc) {
937                 printk("mpoa: msg_to_mpoad: mesg %d to a non-existent mpoad\n", mesg->type);
938                 return -ENXIO;
939         }
940
941         skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
942         if (skb == NULL)
943                 return -ENOMEM;
944         skb_put(skb, sizeof(struct k_message));
945         memcpy(skb->data, mesg, sizeof(struct k_message));
946         atm_force_charge(mpc->mpoad_vcc, skb->truesize);
947         skb_queue_tail(&mpc->mpoad_vcc->sk->receive_queue, skb);
948         wake_up(&mpc->mpoad_vcc->sleep);
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         unsigned char *ip __attribute__ ((unused)) = (unsigned char *)&dst_ip;
1075         struct atm_mpoa_qos *qos = atm_mpoa_search_qos(dst_ip);
1076         eg_cache_entry *eg_entry = client->eg_ops->get_by_src_ip(dst_ip, client);
1077
1078         if(eg_entry && eg_entry->shortcut){
1079                 if(eg_entry->shortcut->qos.txtp.traffic_class &
1080                    msg->qos.txtp.traffic_class &
1081                    (qos ? qos->qos.txtp.traffic_class : ATM_UBR | ATM_CBR)){
1082                             if(eg_entry->shortcut->qos.txtp.traffic_class == ATM_UBR)
1083                                     entry->shortcut = eg_entry->shortcut;
1084                             else if(eg_entry->shortcut->qos.txtp.max_pcr > 0)
1085                                     entry->shortcut = eg_entry->shortcut;
1086                 }
1087                 if(entry->shortcut){
1088                         dprintk("mpoa: (%s) using egress SVC to reach %u.%u.%u.%u\n",client->dev->name, NIPQUAD(ip));
1089                         client->eg_ops->put(eg_entry);
1090                         return;
1091                 }
1092         }
1093         if (eg_entry != NULL)
1094                 client->eg_ops->put(eg_entry);
1095
1096         /* No luck in the egress cache we must open an ingress SVC */
1097         msg->type = OPEN_INGRESS_SVC;
1098         if (qos && (qos->qos.txtp.traffic_class == msg->qos.txtp.traffic_class))
1099         {
1100                 msg->qos = qos->qos;
1101                 printk("mpoa: (%s) trying to get a CBR shortcut\n",client->dev->name);
1102         }
1103         else memset(&msg->qos,0,sizeof(struct atm_qos));
1104         msg_to_mpoad(msg, client);
1105         return;
1106 }
1107
1108 static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1109 {
1110         unsigned char *ip;
1111
1112         uint32_t dst_ip = msg->content.in_info.in_dst_ip;
1113         in_cache_entry *entry = mpc->in_ops->get(dst_ip, mpc);
1114         ip = (unsigned char *)&dst_ip;
1115         dprintk("mpoa: (%s) MPOA_res_reply_rcvd: ip %u.%u.%u.%u\n", mpc->dev->name, NIPQUAD(ip));
1116         ddprintk("mpoa: (%s) MPOA_res_reply_rcvd() entry = %p", mpc->dev->name, entry);
1117         if(entry == NULL){
1118                 printk("\nmpoa: (%s) ARGH, received res. reply for an entry that doesn't exist.\n", mpc->dev->name);
1119                 return;
1120         }
1121         ddprintk(" entry_state = %d ", entry->entry_state);     
1122
1123         if (entry->entry_state == INGRESS_RESOLVED) {
1124                 printk("\nmpoa: (%s) MPOA_res_reply_rcvd for RESOLVED entry!\n", mpc->dev->name);
1125                 mpc->in_ops->put(entry);
1126                 return;
1127         }
1128
1129         entry->ctrl_info = msg->content.in_info;
1130         do_gettimeofday(&(entry->tv));
1131         do_gettimeofday(&(entry->reply_wait)); /* Used in refreshing func from now on */
1132         entry->refresh_time = 0;
1133         ddprintk("entry->shortcut = %p\n", entry->shortcut);
1134
1135         if(entry->entry_state == INGRESS_RESOLVING && entry->shortcut != NULL){
1136                 entry->entry_state = INGRESS_RESOLVED; 
1137                 mpc->in_ops->put(entry);
1138                 return; /* Shortcut already open... */
1139         }
1140
1141         if (entry->shortcut != NULL) {
1142                 printk("mpoa: (%s) MPOA_res_reply_rcvd: entry->shortcut != NULL, impossible!\n",
1143                        mpc->dev->name);
1144                 mpc->in_ops->put(entry);
1145                 return;
1146         }
1147         
1148         check_qos_and_open_shortcut(msg, mpc, entry);
1149         entry->entry_state = INGRESS_RESOLVED;
1150         mpc->in_ops->put(entry);
1151
1152         return;
1153
1154 }
1155
1156 static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1157 {
1158         uint32_t dst_ip = msg->content.in_info.in_dst_ip;
1159         uint32_t mask = msg->ip_mask;
1160         unsigned char *ip = (unsigned char *)&dst_ip;
1161         in_cache_entry *entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1162
1163         if(entry == NULL){
1164                 printk("mpoa: (%s) ingress_purge_rcvd: purge for a non-existing entry, ", mpc->dev->name);
1165                 printk("ip = %u.%u.%u.%u\n", ip[0], ip[1], ip[2], ip[3]);
1166                 return;
1167         }
1168
1169         do {
1170                 dprintk("mpoa: (%s) ingress_purge_rcvd: removing an ingress entry, ip = %u.%u.%u.%u\n" ,
1171                         mpc->dev->name, ip[0], ip[1], ip[2], ip[3]);
1172                 write_lock_bh(&mpc->ingress_lock);
1173                 mpc->in_ops->remove_entry(entry, mpc);
1174                 write_unlock_bh(&mpc->ingress_lock);
1175                 mpc->in_ops->put(entry);
1176                 entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1177         } while (entry != NULL);
1178
1179         return;
1180
1181
1182 static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1183 {
1184         uint32_t cache_id = msg->content.eg_info.cache_id;
1185         eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(cache_id, mpc);
1186         
1187         if (entry == NULL) {
1188                 dprintk("mpoa: (%s) egress_purge_rcvd: purge for a non-existing entry\n", mpc->dev->name);
1189                 return;
1190         }
1191
1192         write_lock_irq(&mpc->egress_lock);
1193         mpc->eg_ops->remove_entry(entry, mpc);
1194         write_unlock_irq(&mpc->egress_lock);
1195
1196         mpc->eg_ops->put(entry);
1197
1198         return;
1199
1200
1201 static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry)
1202 {
1203         struct k_message *purge_msg;
1204         struct sk_buff *skb;
1205
1206         dprintk("mpoa: purge_egress_shortcut: entering\n");
1207         if (vcc == NULL) {
1208                 printk("mpoa: purge_egress_shortcut: vcc == NULL\n");
1209                 return;
1210         }
1211
1212         skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
1213         if (skb == NULL) {
1214                  printk("mpoa: purge_egress_shortcut: out of memory\n");
1215                 return;
1216         }
1217
1218         skb_put(skb, sizeof(struct k_message));
1219         memset(skb->data, 0, sizeof(struct k_message));
1220         purge_msg = (struct k_message *)skb->data;
1221         purge_msg->type = DATA_PLANE_PURGE;
1222         if (entry != NULL)
1223                 purge_msg->content.eg_info = entry->ctrl_info;
1224
1225         atm_force_charge(vcc, skb->truesize);
1226         skb_queue_tail(&vcc->sk->receive_queue, skb);
1227         wake_up(&vcc->sleep);
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()
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 = 0;
1381         static unsigned long previous_refresh_time = 0;
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 struct atm_mpoa_ops __atm_mpoa_ops = {
1402         .mpoad_attach = atm_mpoa_mpoad_attach,
1403         .vcc_attach =   atm_mpoa_vcc_attach,
1404         .owner = THIS_MODULE
1405 };
1406
1407 static __init int atm_mpoa_init(void)
1408 {
1409         atm_mpoa_ops_set(&__atm_mpoa_ops);
1410
1411 #ifdef CONFIG_PROC_FS
1412         if (mpc_proc_init() != 0)
1413                 printk(KERN_INFO "mpoa: failed to initialize /proc/mpoa\n");
1414         else
1415                 printk(KERN_INFO "mpoa: /proc/mpoa initialized\n");
1416 #endif
1417
1418         printk("mpc.c: " __DATE__ " " __TIME__ " initialized\n");
1419
1420         return 0;
1421 }
1422
1423 void __exit atm_mpoa_cleanup(void)
1424 {
1425         struct mpoa_client *mpc, *tmp;
1426         struct atm_mpoa_qos *qos, *nextqos;
1427         struct lec_priv *priv;
1428
1429         if (MOD_IN_USE) {
1430                 printk("mpc.c: module in use\n");
1431                 return;
1432         }
1433 #ifdef CONFIG_PROC_FS
1434         mpc_proc_clean();
1435 #endif
1436
1437         del_timer(&mpc_timer);
1438         unregister_netdevice_notifier(&mpoa_notifier);
1439         atm_mpoa_ops_set(NULL);
1440
1441         mpc = mpcs;
1442         mpcs = NULL;
1443         while (mpc != NULL) {
1444                 tmp = mpc->next;
1445                 if (mpc->dev != NULL) {
1446                         stop_mpc(mpc);
1447                         priv = (struct lec_priv *)mpc->dev->priv;
1448                         if (priv->lane2_ops != NULL)
1449                                 priv->lane2_ops->associate_indicator = NULL;
1450                 }
1451                 ddprintk("mpoa: cleanup_module: about to clear caches\n");
1452                 mpc->in_ops->destroy_cache(mpc);
1453                 mpc->eg_ops->destroy_cache(mpc);
1454                 ddprintk("mpoa: cleanup_module: caches cleared\n");
1455                 kfree(mpc->mps_macs);
1456                 memset(mpc, 0, sizeof(struct mpoa_client));
1457                 ddprintk("mpoa: cleanup_module: about to kfree %p\n", mpc);
1458                 kfree(mpc);
1459                 ddprintk("mpoa: cleanup_module: next mpc is at %p\n", tmp);
1460                 mpc = tmp;
1461         }
1462
1463         qos = qos_head;
1464         qos_head = NULL;
1465         while (qos != NULL) {
1466                 nextqos = qos->next;
1467                 dprintk("mpoa: cleanup_module: freeing qos entry %p\n", qos);
1468                 kfree(qos);
1469                 qos = nextqos;
1470         }
1471
1472         return;
1473 }
1474
1475 module_init(atm_mpoa_init);
1476 module_exit(atm_mpoa_cleanup);
1477
1478 MODULE_LICENSE("GPL");