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