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