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