Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
0003 
0004 #include <linux/kernel.h>
0005 #include <linux/string.h>
0006 #include <linux/slab.h>
0007 #include <linux/timer.h>
0008 #include <linux/init.h>
0009 #include <linux/bitops.h>
0010 #include <linux/capability.h>
0011 #include <linux/seq_file.h>
0012 
0013 /* We are an ethernet device */
0014 #include <linux/if_ether.h>
0015 #include <linux/netdevice.h>
0016 #include <linux/etherdevice.h>
0017 #include <net/sock.h>
0018 #include <linux/skbuff.h>
0019 #include <linux/ip.h>
0020 #include <linux/uaccess.h>
0021 #include <asm/byteorder.h>
0022 #include <net/checksum.h>   /* for ip_fast_csum() */
0023 #include <net/arp.h>
0024 #include <net/dst.h>
0025 #include <linux/proc_fs.h>
0026 
0027 /* And atm device */
0028 #include <linux/atmdev.h>
0029 #include <linux/atmlec.h>
0030 #include <linux/atmmpc.h>
0031 /* Modular too */
0032 #include <linux/module.h>
0033 
0034 #include "lec.h"
0035 #include "mpc.h"
0036 #include "resources.h"
0037 
0038 /*
0039  * mpc.c: Implementation of MPOA client kernel part
0040  */
0041 
0042 #if 0
0043 #define dprintk(format, args...) \
0044     printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args)
0045 #define dprintk_cont(format, args...) printk(KERN_CONT format, ##args)
0046 #else
0047 #define dprintk(format, args...)                    \
0048     do { if (0)                         \
0049         printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args);\
0050     } while (0)
0051 #define dprintk_cont(format, args...)           \
0052     do { if (0) printk(KERN_CONT format, ##args); } while (0)
0053 #endif
0054 
0055 #if 0
0056 #define ddprintk(format, args...) \
0057     printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args)
0058 #define ddprintk_cont(format, args...) printk(KERN_CONT format, ##args)
0059 #else
0060 #define ddprintk(format, args...)                   \
0061     do { if (0)                         \
0062         printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args);\
0063     } while (0)
0064 #define ddprintk_cont(format, args...)          \
0065     do { if (0) printk(KERN_CONT format, ##args); } while (0)
0066 #endif
0067 
0068 /* mpc_daemon -> kernel */
0069 static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc);
0070 static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc);
0071 static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
0072 static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
0073 static void mps_death(struct k_message *msg, struct mpoa_client *mpc);
0074 static void clean_up(struct k_message *msg, struct mpoa_client *mpc,
0075              int action);
0076 static void MPOA_cache_impos_rcvd(struct k_message *msg,
0077                   struct mpoa_client *mpc);
0078 static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg,
0079                    struct mpoa_client *mpc);
0080 static void set_mps_mac_addr_rcvd(struct k_message *mesg,
0081                   struct mpoa_client *mpc);
0082 
0083 static const uint8_t *copy_macs(struct mpoa_client *mpc,
0084                 const uint8_t *router_mac,
0085                 const uint8_t *tlvs, uint8_t mps_macs,
0086                 uint8_t device_type);
0087 static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry);
0088 
0089 static void send_set_mps_ctrl_addr(const char *addr, struct mpoa_client *mpc);
0090 static void mpoad_close(struct atm_vcc *vcc);
0091 static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb);
0092 
0093 static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb);
0094 static netdev_tx_t mpc_send_packet(struct sk_buff *skb,
0095                    struct net_device *dev);
0096 static int mpoa_event_listener(struct notifier_block *mpoa_notifier,
0097                    unsigned long event, void *dev);
0098 static void mpc_timer_refresh(void);
0099 static void mpc_cache_check(struct timer_list *unused);
0100 
0101 static struct llc_snap_hdr llc_snap_mpoa_ctrl = {
0102     0xaa, 0xaa, 0x03,
0103     {0x00, 0x00, 0x5e},
0104     {0x00, 0x03}         /* For MPOA control PDUs */
0105 };
0106 static struct llc_snap_hdr llc_snap_mpoa_data = {
0107     0xaa, 0xaa, 0x03,
0108     {0x00, 0x00, 0x00},
0109     {0x08, 0x00}         /* This is for IP PDUs only */
0110 };
0111 static struct llc_snap_hdr llc_snap_mpoa_data_tagged = {
0112     0xaa, 0xaa, 0x03,
0113     {0x00, 0x00, 0x00},
0114     {0x88, 0x4c}         /* This is for tagged data PDUs */
0115 };
0116 
0117 static struct notifier_block mpoa_notifier = {
0118     mpoa_event_listener,
0119     NULL,
0120     0
0121 };
0122 
0123 struct mpoa_client *mpcs = NULL; /* FIXME */
0124 static struct atm_mpoa_qos *qos_head = NULL;
0125 static DEFINE_TIMER(mpc_timer, mpc_cache_check);
0126 
0127 
0128 static struct mpoa_client *find_mpc_by_itfnum(int itf)
0129 {
0130     struct mpoa_client *mpc;
0131 
0132     mpc = mpcs;  /* our global linked list */
0133     while (mpc != NULL) {
0134         if (mpc->dev_num == itf)
0135             return mpc;
0136         mpc = mpc->next;
0137     }
0138 
0139     return NULL;   /* not found */
0140 }
0141 
0142 static struct mpoa_client *find_mpc_by_vcc(struct atm_vcc *vcc)
0143 {
0144     struct mpoa_client *mpc;
0145 
0146     mpc = mpcs;  /* our global linked list */
0147     while (mpc != NULL) {
0148         if (mpc->mpoad_vcc == vcc)
0149             return mpc;
0150         mpc = mpc->next;
0151     }
0152 
0153     return NULL;   /* not found */
0154 }
0155 
0156 static struct mpoa_client *find_mpc_by_lec(struct net_device *dev)
0157 {
0158     struct mpoa_client *mpc;
0159 
0160     mpc = mpcs;  /* our global linked list */
0161     while (mpc != NULL) {
0162         if (mpc->dev == dev)
0163             return mpc;
0164         mpc = mpc->next;
0165     }
0166 
0167     return NULL;   /* not found */
0168 }
0169 
0170 /*
0171  * Functions for managing QoS list
0172  */
0173 
0174 /*
0175  * Overwrites the old entry or makes a new one.
0176  */
0177 struct atm_mpoa_qos *atm_mpoa_add_qos(__be32 dst_ip, struct atm_qos *qos)
0178 {
0179     struct atm_mpoa_qos *entry;
0180 
0181     entry = atm_mpoa_search_qos(dst_ip);
0182     if (entry != NULL) {
0183         entry->qos = *qos;
0184         return entry;
0185     }
0186 
0187     entry = kmalloc(sizeof(struct atm_mpoa_qos), GFP_KERNEL);
0188     if (entry == NULL) {
0189         pr_info("mpoa: out of memory\n");
0190         return entry;
0191     }
0192 
0193     entry->ipaddr = dst_ip;
0194     entry->qos = *qos;
0195 
0196     entry->next = qos_head;
0197     qos_head = entry;
0198 
0199     return entry;
0200 }
0201 
0202 struct atm_mpoa_qos *atm_mpoa_search_qos(__be32 dst_ip)
0203 {
0204     struct atm_mpoa_qos *qos;
0205 
0206     qos = qos_head;
0207     while (qos) {
0208         if (qos->ipaddr == dst_ip)
0209             break;
0210         qos = qos->next;
0211     }
0212 
0213     return qos;
0214 }
0215 
0216 /*
0217  * Returns 0 for failure
0218  */
0219 int atm_mpoa_delete_qos(struct atm_mpoa_qos *entry)
0220 {
0221     struct atm_mpoa_qos *curr;
0222 
0223     if (entry == NULL)
0224         return 0;
0225     if (entry == qos_head) {
0226         qos_head = qos_head->next;
0227         kfree(entry);
0228         return 1;
0229     }
0230 
0231     curr = qos_head;
0232     while (curr != NULL) {
0233         if (curr->next == entry) {
0234             curr->next = entry->next;
0235             kfree(entry);
0236             return 1;
0237         }
0238         curr = curr->next;
0239     }
0240 
0241     return 0;
0242 }
0243 
0244 /* this is buggered - we need locking for qos_head */
0245 void atm_mpoa_disp_qos(struct seq_file *m)
0246 {
0247     struct atm_mpoa_qos *qos;
0248 
0249     qos = qos_head;
0250     seq_printf(m, "QoS entries for shortcuts:\n");
0251     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");
0252 
0253     while (qos != NULL) {
0254         seq_printf(m, "%pI4\n     %-7d %-7d %-7d %-7d %-7d\n     %-7d %-7d %-7d %-7d %-7d\n",
0255                &qos->ipaddr,
0256                qos->qos.txtp.max_pcr,
0257                qos->qos.txtp.pcr,
0258                qos->qos.txtp.min_pcr,
0259                qos->qos.txtp.max_cdv,
0260                qos->qos.txtp.max_sdu,
0261                qos->qos.rxtp.max_pcr,
0262                qos->qos.rxtp.pcr,
0263                qos->qos.rxtp.min_pcr,
0264                qos->qos.rxtp.max_cdv,
0265                qos->qos.rxtp.max_sdu);
0266         qos = qos->next;
0267     }
0268 }
0269 
0270 static struct net_device *find_lec_by_itfnum(int itf)
0271 {
0272     struct net_device *dev;
0273     char name[IFNAMSIZ];
0274 
0275     sprintf(name, "lec%d", itf);
0276     dev = dev_get_by_name(&init_net, name);
0277 
0278     return dev;
0279 }
0280 
0281 static struct mpoa_client *alloc_mpc(void)
0282 {
0283     struct mpoa_client *mpc;
0284 
0285     mpc = kzalloc(sizeof(struct mpoa_client), GFP_KERNEL);
0286     if (mpc == NULL)
0287         return NULL;
0288     rwlock_init(&mpc->ingress_lock);
0289     rwlock_init(&mpc->egress_lock);
0290     mpc->next = mpcs;
0291     atm_mpoa_init_cache(mpc);
0292 
0293     mpc->parameters.mpc_p1 = MPC_P1;
0294     mpc->parameters.mpc_p2 = MPC_P2;
0295     memset(mpc->parameters.mpc_p3, 0, sizeof(mpc->parameters.mpc_p3));
0296     mpc->parameters.mpc_p4 = MPC_P4;
0297     mpc->parameters.mpc_p5 = MPC_P5;
0298     mpc->parameters.mpc_p6 = MPC_P6;
0299 
0300     mpcs = mpc;
0301 
0302     return mpc;
0303 }
0304 
0305 /*
0306  *
0307  * start_mpc() puts the MPC on line. All the packets destined
0308  * to the lec underneath us are now being monitored and
0309  * shortcuts will be established.
0310  *
0311  */
0312 static void start_mpc(struct mpoa_client *mpc, struct net_device *dev)
0313 {
0314 
0315     dprintk("(%s)\n", mpc->dev->name);
0316     if (!dev->netdev_ops)
0317         pr_info("(%s) not starting\n", dev->name);
0318     else {
0319         mpc->old_ops = dev->netdev_ops;
0320         mpc->new_ops = *mpc->old_ops;
0321         mpc->new_ops.ndo_start_xmit = mpc_send_packet;
0322         dev->netdev_ops = &mpc->new_ops;
0323     }
0324 }
0325 
0326 static void stop_mpc(struct mpoa_client *mpc)
0327 {
0328     struct net_device *dev = mpc->dev;
0329     dprintk("(%s)", mpc->dev->name);
0330 
0331     /* Lets not nullify lec device's dev->hard_start_xmit */
0332     if (dev->netdev_ops != &mpc->new_ops) {
0333         dprintk_cont(" mpc already stopped, not fatal\n");
0334         return;
0335     }
0336     dprintk_cont("\n");
0337 
0338     dev->netdev_ops = mpc->old_ops;
0339     mpc->old_ops = NULL;
0340 
0341     /* close_shortcuts(mpc);    ??? FIXME */
0342 }
0343 
0344 static const char *mpoa_device_type_string(char type) __attribute__ ((unused));
0345 
0346 static const char *mpoa_device_type_string(char type)
0347 {
0348     switch (type) {
0349     case NON_MPOA:
0350         return "non-MPOA device";
0351     case MPS:
0352         return "MPS";
0353     case MPC:
0354         return "MPC";
0355     case MPS_AND_MPC:
0356         return "both MPS and MPC";
0357     }
0358 
0359     return "unspecified (non-MPOA) device";
0360 }
0361 
0362 /*
0363  * lec device calls this via its netdev_priv(dev)->lane2_ops
0364  * ->associate_indicator() when it sees a TLV in LE_ARP packet.
0365  * We fill in the pointer above when we see a LANE2 lec initializing
0366  * See LANE2 spec 3.1.5
0367  *
0368  * Quite a big and ugly function but when you look at it
0369  * all it does is to try to locate and parse MPOA Device
0370  * Type TLV.
0371  * We give our lec a pointer to this function and when the
0372  * lec sees a TLV it uses the pointer to call this function.
0373  *
0374  */
0375 static void lane2_assoc_ind(struct net_device *dev, const u8 *mac_addr,
0376                 const u8 *tlvs, u32 sizeoftlvs)
0377 {
0378     uint32_t type;
0379     uint8_t length, mpoa_device_type, number_of_mps_macs;
0380     const uint8_t *end_of_tlvs;
0381     struct mpoa_client *mpc;
0382 
0383     mpoa_device_type = number_of_mps_macs = 0; /* silence gcc */
0384     dprintk("(%s) received TLV(s), ", dev->name);
0385     dprintk("total length of all TLVs %d\n", sizeoftlvs);
0386     mpc = find_mpc_by_lec(dev); /* Sampo-Fix: moved here from below */
0387     if (mpc == NULL) {
0388         pr_info("(%s) no mpc\n", dev->name);
0389         return;
0390     }
0391     end_of_tlvs = tlvs + sizeoftlvs;
0392     while (end_of_tlvs - tlvs >= 5) {
0393         type = ((tlvs[0] << 24) | (tlvs[1] << 16) |
0394             (tlvs[2] << 8) | tlvs[3]);
0395         length = tlvs[4];
0396         tlvs += 5;
0397         dprintk("    type 0x%x length %02x\n", type, length);
0398         if (tlvs + length > end_of_tlvs) {
0399             pr_info("TLV value extends past its buffer, aborting parse\n");
0400             return;
0401         }
0402 
0403         if (type == 0) {
0404             pr_info("mpoa: (%s) TLV type was 0, returning\n",
0405                 dev->name);
0406             return;
0407         }
0408 
0409         if (type != TLV_MPOA_DEVICE_TYPE) {
0410             tlvs += length;
0411             continue;  /* skip other TLVs */
0412         }
0413         mpoa_device_type = *tlvs++;
0414         number_of_mps_macs = *tlvs++;
0415         dprintk("(%s) MPOA device type '%s', ",
0416             dev->name, mpoa_device_type_string(mpoa_device_type));
0417         if (mpoa_device_type == MPS_AND_MPC &&
0418             length < (42 + number_of_mps_macs*ETH_ALEN)) { /* :) */
0419             pr_info("(%s) short MPOA Device Type TLV\n",
0420                 dev->name);
0421             continue;
0422         }
0423         if ((mpoa_device_type == MPS || mpoa_device_type == MPC) &&
0424             length < 22 + number_of_mps_macs*ETH_ALEN) {
0425             pr_info("(%s) short MPOA Device Type TLV\n", dev->name);
0426             continue;
0427         }
0428         if (mpoa_device_type != MPS &&
0429             mpoa_device_type != MPS_AND_MPC) {
0430             dprintk("ignoring non-MPS device ");
0431             if (mpoa_device_type == MPC)
0432                 tlvs += 20;
0433             continue;  /* we are only interested in MPSs */
0434         }
0435         if (number_of_mps_macs == 0 &&
0436             mpoa_device_type == MPS_AND_MPC) {
0437             pr_info("(%s) MPS_AND_MPC has zero MACs\n", dev->name);
0438             continue;  /* someone should read the spec */
0439         }
0440         dprintk_cont("this MPS has %d MAC addresses\n",
0441                  number_of_mps_macs);
0442 
0443         /*
0444          * ok, now we can go and tell our daemon
0445          * the control address of MPS
0446          */
0447         send_set_mps_ctrl_addr(tlvs, mpc);
0448 
0449         tlvs = copy_macs(mpc, mac_addr, tlvs,
0450                  number_of_mps_macs, mpoa_device_type);
0451         if (tlvs == NULL)
0452             return;
0453     }
0454     if (end_of_tlvs - tlvs != 0)
0455         pr_info("(%s) ignoring %zd bytes of trailing TLV garbage\n",
0456             dev->name, end_of_tlvs - tlvs);
0457 }
0458 
0459 /*
0460  * Store at least advertizing router's MAC address
0461  * plus the possible MAC address(es) to mpc->mps_macs.
0462  * For a freshly allocated MPOA client mpc->mps_macs == 0.
0463  */
0464 static const uint8_t *copy_macs(struct mpoa_client *mpc,
0465                 const uint8_t *router_mac,
0466                 const uint8_t *tlvs, uint8_t mps_macs,
0467                 uint8_t device_type)
0468 {
0469     int num_macs;
0470     num_macs = (mps_macs > 1) ? mps_macs : 1;
0471 
0472     if (mpc->number_of_mps_macs != num_macs) { /* need to reallocate? */
0473         if (mpc->number_of_mps_macs != 0)
0474             kfree(mpc->mps_macs);
0475         mpc->number_of_mps_macs = 0;
0476         mpc->mps_macs = kmalloc_array(ETH_ALEN, num_macs, GFP_KERNEL);
0477         if (mpc->mps_macs == NULL) {
0478             pr_info("(%s) out of mem\n", mpc->dev->name);
0479             return NULL;
0480         }
0481     }
0482     ether_addr_copy(mpc->mps_macs, router_mac);
0483     tlvs += 20; if (device_type == MPS_AND_MPC) tlvs += 20;
0484     if (mps_macs > 0)
0485         memcpy(mpc->mps_macs, tlvs, mps_macs*ETH_ALEN);
0486     tlvs += mps_macs*ETH_ALEN;
0487     mpc->number_of_mps_macs = num_macs;
0488 
0489     return tlvs;
0490 }
0491 
0492 static int send_via_shortcut(struct sk_buff *skb, struct mpoa_client *mpc)
0493 {
0494     in_cache_entry *entry;
0495     struct iphdr *iph;
0496     char *buff;
0497     __be32 ipaddr = 0;
0498 
0499     static struct {
0500         struct llc_snap_hdr hdr;
0501         __be32 tag;
0502     } tagged_llc_snap_hdr = {
0503         {0xaa, 0xaa, 0x03, {0x00, 0x00, 0x00}, {0x88, 0x4c}},
0504         0
0505     };
0506 
0507     buff = skb->data + mpc->dev->hard_header_len;
0508     iph = (struct iphdr *)buff;
0509     ipaddr = iph->daddr;
0510 
0511     ddprintk("(%s) ipaddr 0x%x\n",
0512          mpc->dev->name, ipaddr);
0513 
0514     entry = mpc->in_ops->get(ipaddr, mpc);
0515     if (entry == NULL) {
0516         entry = mpc->in_ops->add_entry(ipaddr, mpc);
0517         if (entry != NULL)
0518             mpc->in_ops->put(entry);
0519         return 1;
0520     }
0521     /* threshold not exceeded or VCC not ready */
0522     if (mpc->in_ops->cache_hit(entry, mpc) != OPEN) {
0523         ddprintk("(%s) cache_hit: returns != OPEN\n",
0524              mpc->dev->name);
0525         mpc->in_ops->put(entry);
0526         return 1;
0527     }
0528 
0529     ddprintk("(%s) using shortcut\n",
0530          mpc->dev->name);
0531     /* MPOA spec A.1.4, MPOA client must decrement IP ttl at least by one */
0532     if (iph->ttl <= 1) {
0533         ddprintk("(%s) IP ttl = %u, using LANE\n",
0534              mpc->dev->name, iph->ttl);
0535         mpc->in_ops->put(entry);
0536         return 1;
0537     }
0538     iph->ttl--;
0539     iph->check = 0;
0540     iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
0541 
0542     if (entry->ctrl_info.tag != 0) {
0543         ddprintk("(%s) adding tag 0x%x\n",
0544              mpc->dev->name, entry->ctrl_info.tag);
0545         tagged_llc_snap_hdr.tag = entry->ctrl_info.tag;
0546         skb_pull(skb, ETH_HLEN);    /* get rid of Eth header */
0547         skb_push(skb, sizeof(tagged_llc_snap_hdr));
0548                         /* add LLC/SNAP header   */
0549         skb_copy_to_linear_data(skb, &tagged_llc_snap_hdr,
0550                     sizeof(tagged_llc_snap_hdr));
0551     } else {
0552         skb_pull(skb, ETH_HLEN);    /* get rid of Eth header */
0553         skb_push(skb, sizeof(struct llc_snap_hdr));
0554                         /* add LLC/SNAP header + tag  */
0555         skb_copy_to_linear_data(skb, &llc_snap_mpoa_data,
0556                     sizeof(struct llc_snap_hdr));
0557     }
0558 
0559     atm_account_tx(entry->shortcut, skb);
0560     entry->shortcut->send(entry->shortcut, skb);
0561     entry->packets_fwded++;
0562     mpc->in_ops->put(entry);
0563 
0564     return 0;
0565 }
0566 
0567 /*
0568  * Probably needs some error checks and locking, not sure...
0569  */
0570 static netdev_tx_t mpc_send_packet(struct sk_buff *skb,
0571                      struct net_device *dev)
0572 {
0573     struct mpoa_client *mpc;
0574     struct ethhdr *eth;
0575     int i = 0;
0576 
0577     mpc = find_mpc_by_lec(dev); /* this should NEVER fail */
0578     if (mpc == NULL) {
0579         pr_info("(%s) no MPC found\n", dev->name);
0580         goto non_ip;
0581     }
0582 
0583     eth = (struct ethhdr *)skb->data;
0584     if (eth->h_proto != htons(ETH_P_IP))
0585         goto non_ip; /* Multi-Protocol Over ATM :-) */
0586 
0587     /* Weed out funny packets (e.g., AF_PACKET or raw). */
0588     if (skb->len < ETH_HLEN + sizeof(struct iphdr))
0589         goto non_ip;
0590     skb_set_network_header(skb, ETH_HLEN);
0591     if (skb->len < ETH_HLEN + ip_hdr(skb)->ihl * 4 || ip_hdr(skb)->ihl < 5)
0592         goto non_ip;
0593 
0594     while (i < mpc->number_of_mps_macs) {
0595         if (ether_addr_equal(eth->h_dest, mpc->mps_macs + i * ETH_ALEN))
0596             if (send_via_shortcut(skb, mpc) == 0) /* try shortcut */
0597                 return NETDEV_TX_OK;
0598         i++;
0599     }
0600 
0601 non_ip:
0602     return __netdev_start_xmit(mpc->old_ops, skb, dev, false);
0603 }
0604 
0605 static int atm_mpoa_vcc_attach(struct atm_vcc *vcc, void __user *arg)
0606 {
0607     int bytes_left;
0608     struct mpoa_client *mpc;
0609     struct atmmpc_ioc ioc_data;
0610     in_cache_entry *in_entry;
0611     __be32  ipaddr;
0612 
0613     bytes_left = copy_from_user(&ioc_data, arg, sizeof(struct atmmpc_ioc));
0614     if (bytes_left != 0) {
0615         pr_info("mpoa:Short read (missed %d bytes) from userland\n",
0616             bytes_left);
0617         return -EFAULT;
0618     }
0619     ipaddr = ioc_data.ipaddr;
0620     if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF)
0621         return -EINVAL;
0622 
0623     mpc = find_mpc_by_itfnum(ioc_data.dev_num);
0624     if (mpc == NULL)
0625         return -EINVAL;
0626 
0627     if (ioc_data.type == MPC_SOCKET_INGRESS) {
0628         in_entry = mpc->in_ops->get(ipaddr, mpc);
0629         if (in_entry == NULL ||
0630             in_entry->entry_state < INGRESS_RESOLVED) {
0631             pr_info("(%s) did not find RESOLVED entry from ingress cache\n",
0632                 mpc->dev->name);
0633             if (in_entry != NULL)
0634                 mpc->in_ops->put(in_entry);
0635             return -EINVAL;
0636         }
0637         pr_info("(%s) attaching ingress SVC, entry = %pI4\n",
0638             mpc->dev->name, &in_entry->ctrl_info.in_dst_ip);
0639         in_entry->shortcut = vcc;
0640         mpc->in_ops->put(in_entry);
0641     } else {
0642         pr_info("(%s) attaching egress SVC\n", mpc->dev->name);
0643     }
0644 
0645     vcc->proto_data = mpc->dev;
0646     vcc->push = mpc_push;
0647 
0648     return 0;
0649 }
0650 
0651 /*
0652  *
0653  */
0654 static void mpc_vcc_close(struct atm_vcc *vcc, struct net_device *dev)
0655 {
0656     struct mpoa_client *mpc;
0657     in_cache_entry *in_entry;
0658     eg_cache_entry *eg_entry;
0659 
0660     mpc = find_mpc_by_lec(dev);
0661     if (mpc == NULL) {
0662         pr_info("(%s) close for unknown MPC\n", dev->name);
0663         return;
0664     }
0665 
0666     dprintk("(%s)\n", dev->name);
0667     in_entry = mpc->in_ops->get_by_vcc(vcc, mpc);
0668     if (in_entry) {
0669         dprintk("(%s) ingress SVC closed ip = %pI4\n",
0670             mpc->dev->name, &in_entry->ctrl_info.in_dst_ip);
0671         in_entry->shortcut = NULL;
0672         mpc->in_ops->put(in_entry);
0673     }
0674     eg_entry = mpc->eg_ops->get_by_vcc(vcc, mpc);
0675     if (eg_entry) {
0676         dprintk("(%s) egress SVC closed\n", mpc->dev->name);
0677         eg_entry->shortcut = NULL;
0678         mpc->eg_ops->put(eg_entry);
0679     }
0680 
0681     if (in_entry == NULL && eg_entry == NULL)
0682         dprintk("(%s) unused vcc closed\n", dev->name);
0683 }
0684 
0685 static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb)
0686 {
0687     struct net_device *dev = (struct net_device *)vcc->proto_data;
0688     struct sk_buff *new_skb;
0689     eg_cache_entry *eg;
0690     struct mpoa_client *mpc;
0691     __be32 tag;
0692     char *tmp;
0693 
0694     ddprintk("(%s)\n", dev->name);
0695     if (skb == NULL) {
0696         dprintk("(%s) null skb, closing VCC\n", dev->name);
0697         mpc_vcc_close(vcc, dev);
0698         return;
0699     }
0700 
0701     skb->dev = dev;
0702     if (memcmp(skb->data, &llc_snap_mpoa_ctrl,
0703            sizeof(struct llc_snap_hdr)) == 0) {
0704         struct sock *sk = sk_atm(vcc);
0705 
0706         dprintk("(%s) control packet arrived\n", dev->name);
0707         /* Pass control packets to daemon */
0708         skb_queue_tail(&sk->sk_receive_queue, skb);
0709         sk->sk_data_ready(sk);
0710         return;
0711     }
0712 
0713     /* data coming over the shortcut */
0714     atm_return(vcc, skb->truesize);
0715 
0716     mpc = find_mpc_by_lec(dev);
0717     if (mpc == NULL) {
0718         pr_info("(%s) unknown MPC\n", dev->name);
0719         return;
0720     }
0721 
0722     if (memcmp(skb->data, &llc_snap_mpoa_data_tagged,
0723            sizeof(struct llc_snap_hdr)) == 0) { /* MPOA tagged data */
0724         ddprintk("(%s) tagged data packet arrived\n", dev->name);
0725 
0726     } else if (memcmp(skb->data, &llc_snap_mpoa_data,
0727               sizeof(struct llc_snap_hdr)) == 0) { /* MPOA data */
0728         pr_info("(%s) Unsupported non-tagged data packet arrived.  Purging\n",
0729             dev->name);
0730         dev_kfree_skb_any(skb);
0731         return;
0732     } else {
0733         pr_info("(%s) garbage arrived, purging\n", dev->name);
0734         dev_kfree_skb_any(skb);
0735         return;
0736     }
0737 
0738     tmp = skb->data + sizeof(struct llc_snap_hdr);
0739     tag = *(__be32 *)tmp;
0740 
0741     eg = mpc->eg_ops->get_by_tag(tag, mpc);
0742     if (eg == NULL) {
0743         pr_info("mpoa: (%s) Didn't find egress cache entry, tag = %u\n",
0744             dev->name, tag);
0745         purge_egress_shortcut(vcc, NULL);
0746         dev_kfree_skb_any(skb);
0747         return;
0748     }
0749 
0750     /*
0751      * See if ingress MPC is using shortcut we opened as a return channel.
0752      * This means we have a bi-directional vcc opened by us.
0753      */
0754     if (eg->shortcut == NULL) {
0755         eg->shortcut = vcc;
0756         pr_info("(%s) egress SVC in use\n", dev->name);
0757     }
0758 
0759     skb_pull(skb, sizeof(struct llc_snap_hdr) + sizeof(tag));
0760                     /* get rid of LLC/SNAP header */
0761     new_skb = skb_realloc_headroom(skb, eg->ctrl_info.DH_length);
0762                     /* LLC/SNAP is shorter than MAC header :( */
0763     dev_kfree_skb_any(skb);
0764     if (new_skb == NULL) {
0765         mpc->eg_ops->put(eg);
0766         return;
0767     }
0768     skb_push(new_skb, eg->ctrl_info.DH_length);     /* add MAC header */
0769     skb_copy_to_linear_data(new_skb, eg->ctrl_info.DLL_header,
0770                 eg->ctrl_info.DH_length);
0771     new_skb->protocol = eth_type_trans(new_skb, dev);
0772     skb_reset_network_header(new_skb);
0773 
0774     eg->latest_ip_addr = ip_hdr(new_skb)->saddr;
0775     eg->packets_rcvd++;
0776     mpc->eg_ops->put(eg);
0777 
0778     memset(ATM_SKB(new_skb), 0, sizeof(struct atm_skb_data));
0779     netif_rx(new_skb);
0780 }
0781 
0782 static const struct atmdev_ops mpc_ops = { /* only send is required */
0783     .close  = mpoad_close,
0784     .send   = msg_from_mpoad
0785 };
0786 
0787 static struct atm_dev mpc_dev = {
0788     .ops    = &mpc_ops,
0789     .type   = "mpc",
0790     .number = 42,
0791     .lock   = __SPIN_LOCK_UNLOCKED(mpc_dev.lock)
0792     /* members not explicitly initialised will be 0 */
0793 };
0794 
0795 static int atm_mpoa_mpoad_attach(struct atm_vcc *vcc, int arg)
0796 {
0797     struct mpoa_client *mpc;
0798     struct lec_priv *priv;
0799     int err;
0800 
0801     if (mpcs == NULL) {
0802         mpc_timer_refresh();
0803 
0804         /* This lets us now how our LECs are doing */
0805         err = register_netdevice_notifier(&mpoa_notifier);
0806         if (err < 0) {
0807             del_timer(&mpc_timer);
0808             return err;
0809         }
0810     }
0811 
0812     mpc = find_mpc_by_itfnum(arg);
0813     if (mpc == NULL) {
0814         dprintk("allocating new mpc for itf %d\n", arg);
0815         mpc = alloc_mpc();
0816         if (mpc == NULL)
0817             return -ENOMEM;
0818         mpc->dev_num = arg;
0819         mpc->dev = find_lec_by_itfnum(arg);
0820                     /* NULL if there was no lec */
0821     }
0822     if (mpc->mpoad_vcc) {
0823         pr_info("mpoad is already present for itf %d\n", arg);
0824         return -EADDRINUSE;
0825     }
0826 
0827     if (mpc->dev) { /* check if the lec is LANE2 capable */
0828         priv = netdev_priv(mpc->dev);
0829         if (priv->lane_version < 2) {
0830             dev_put(mpc->dev);
0831             mpc->dev = NULL;
0832         } else
0833             priv->lane2_ops->associate_indicator = lane2_assoc_ind;
0834     }
0835 
0836     mpc->mpoad_vcc = vcc;
0837     vcc->dev = &mpc_dev;
0838     vcc_insert_socket(sk_atm(vcc));
0839     set_bit(ATM_VF_META, &vcc->flags);
0840     set_bit(ATM_VF_READY, &vcc->flags);
0841 
0842     if (mpc->dev) {
0843         char empty[ATM_ESA_LEN];
0844         memset(empty, 0, ATM_ESA_LEN);
0845 
0846         start_mpc(mpc, mpc->dev);
0847         /* set address if mpcd e.g. gets killed and restarted.
0848          * If we do not do it now we have to wait for the next LE_ARP
0849          */
0850         if (memcmp(mpc->mps_ctrl_addr, empty, ATM_ESA_LEN) != 0)
0851             send_set_mps_ctrl_addr(mpc->mps_ctrl_addr, mpc);
0852     }
0853 
0854     __module_get(THIS_MODULE);
0855     return arg;
0856 }
0857 
0858 static void send_set_mps_ctrl_addr(const char *addr, struct mpoa_client *mpc)
0859 {
0860     struct k_message mesg;
0861 
0862     memcpy(mpc->mps_ctrl_addr, addr, ATM_ESA_LEN);
0863 
0864     mesg.type = SET_MPS_CTRL_ADDR;
0865     memcpy(mesg.MPS_ctrl, addr, ATM_ESA_LEN);
0866     msg_to_mpoad(&mesg, mpc);
0867 }
0868 
0869 static void mpoad_close(struct atm_vcc *vcc)
0870 {
0871     struct mpoa_client *mpc;
0872     struct sk_buff *skb;
0873 
0874     mpc = find_mpc_by_vcc(vcc);
0875     if (mpc == NULL) {
0876         pr_info("did not find MPC\n");
0877         return;
0878     }
0879     if (!mpc->mpoad_vcc) {
0880         pr_info("close for non-present mpoad\n");
0881         return;
0882     }
0883 
0884     mpc->mpoad_vcc = NULL;
0885     if (mpc->dev) {
0886         struct lec_priv *priv = netdev_priv(mpc->dev);
0887         priv->lane2_ops->associate_indicator = NULL;
0888         stop_mpc(mpc);
0889         dev_put(mpc->dev);
0890     }
0891 
0892     mpc->in_ops->destroy_cache(mpc);
0893     mpc->eg_ops->destroy_cache(mpc);
0894 
0895     while ((skb = skb_dequeue(&sk_atm(vcc)->sk_receive_queue))) {
0896         atm_return(vcc, skb->truesize);
0897         kfree_skb(skb);
0898     }
0899 
0900     pr_info("(%s) going down\n",
0901         (mpc->dev) ? mpc->dev->name : "<unknown>");
0902     module_put(THIS_MODULE);
0903 }
0904 
0905 /*
0906  *
0907  */
0908 static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb)
0909 {
0910 
0911     struct mpoa_client *mpc = find_mpc_by_vcc(vcc);
0912     struct k_message *mesg = (struct k_message *)skb->data;
0913     WARN_ON(refcount_sub_and_test(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc));
0914 
0915     if (mpc == NULL) {
0916         pr_info("no mpc found\n");
0917         return 0;
0918     }
0919     dprintk("(%s)", mpc->dev ? mpc->dev->name : "<unknown>");
0920     switch (mesg->type) {
0921     case MPOA_RES_REPLY_RCVD:
0922         dprintk_cont("mpoa_res_reply_rcvd\n");
0923         MPOA_res_reply_rcvd(mesg, mpc);
0924         break;
0925     case MPOA_TRIGGER_RCVD:
0926         dprintk_cont("mpoa_trigger_rcvd\n");
0927         MPOA_trigger_rcvd(mesg, mpc);
0928         break;
0929     case INGRESS_PURGE_RCVD:
0930         dprintk_cont("nhrp_purge_rcvd\n");
0931         ingress_purge_rcvd(mesg, mpc);
0932         break;
0933     case EGRESS_PURGE_RCVD:
0934         dprintk_cont("egress_purge_reply_rcvd\n");
0935         egress_purge_rcvd(mesg, mpc);
0936         break;
0937     case MPS_DEATH:
0938         dprintk_cont("mps_death\n");
0939         mps_death(mesg, mpc);
0940         break;
0941     case CACHE_IMPOS_RCVD:
0942         dprintk_cont("cache_impos_rcvd\n");
0943         MPOA_cache_impos_rcvd(mesg, mpc);
0944         break;
0945     case SET_MPC_CTRL_ADDR:
0946         dprintk_cont("set_mpc_ctrl_addr\n");
0947         set_mpc_ctrl_addr_rcvd(mesg, mpc);
0948         break;
0949     case SET_MPS_MAC_ADDR:
0950         dprintk_cont("set_mps_mac_addr\n");
0951         set_mps_mac_addr_rcvd(mesg, mpc);
0952         break;
0953     case CLEAN_UP_AND_EXIT:
0954         dprintk_cont("clean_up_and_exit\n");
0955         clean_up(mesg, mpc, DIE);
0956         break;
0957     case RELOAD:
0958         dprintk_cont("reload\n");
0959         clean_up(mesg, mpc, RELOAD);
0960         break;
0961     case SET_MPC_PARAMS:
0962         dprintk_cont("set_mpc_params\n");
0963         mpc->parameters = mesg->content.params;
0964         break;
0965     default:
0966         dprintk_cont("unknown message %d\n", mesg->type);
0967         break;
0968     }
0969     kfree_skb(skb);
0970 
0971     return 0;
0972 }
0973 
0974 /* Remember that this function may not do things that sleep */
0975 int msg_to_mpoad(struct k_message *mesg, struct mpoa_client *mpc)
0976 {
0977     struct sk_buff *skb;
0978     struct sock *sk;
0979 
0980     if (mpc == NULL || !mpc->mpoad_vcc) {
0981         pr_info("mesg %d to a non-existent mpoad\n", mesg->type);
0982         return -ENXIO;
0983     }
0984 
0985     skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
0986     if (skb == NULL)
0987         return -ENOMEM;
0988     skb_put(skb, sizeof(struct k_message));
0989     skb_copy_to_linear_data(skb, mesg, sizeof(*mesg));
0990     atm_force_charge(mpc->mpoad_vcc, skb->truesize);
0991 
0992     sk = sk_atm(mpc->mpoad_vcc);
0993     skb_queue_tail(&sk->sk_receive_queue, skb);
0994     sk->sk_data_ready(sk);
0995 
0996     return 0;
0997 }
0998 
0999 static int mpoa_event_listener(struct notifier_block *mpoa_notifier,
1000                    unsigned long event, void *ptr)
1001 {
1002     struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1003     struct mpoa_client *mpc;
1004     struct lec_priv *priv;
1005 
1006     if (!net_eq(dev_net(dev), &init_net))
1007         return NOTIFY_DONE;
1008 
1009     if (strncmp(dev->name, "lec", 3))
1010         return NOTIFY_DONE; /* we are only interested in lec:s */
1011 
1012     switch (event) {
1013     case NETDEV_REGISTER:       /* a new lec device was allocated */
1014         priv = netdev_priv(dev);
1015         if (priv->lane_version < 2)
1016             break;
1017         priv->lane2_ops->associate_indicator = lane2_assoc_ind;
1018         mpc = find_mpc_by_itfnum(priv->itfnum);
1019         if (mpc == NULL) {
1020             dprintk("allocating new mpc for %s\n", dev->name);
1021             mpc = alloc_mpc();
1022             if (mpc == NULL) {
1023                 pr_info("no new mpc");
1024                 break;
1025             }
1026         }
1027         mpc->dev_num = priv->itfnum;
1028         mpc->dev = dev;
1029         dev_hold(dev);
1030         dprintk("(%s) was initialized\n", dev->name);
1031         break;
1032     case NETDEV_UNREGISTER:
1033         /* the lec device was deallocated */
1034         mpc = find_mpc_by_lec(dev);
1035         if (mpc == NULL)
1036             break;
1037         dprintk("device (%s) was deallocated\n", dev->name);
1038         stop_mpc(mpc);
1039         dev_put(mpc->dev);
1040         mpc->dev = NULL;
1041         break;
1042     case NETDEV_UP:
1043         /* the dev was ifconfig'ed up */
1044         mpc = find_mpc_by_lec(dev);
1045         if (mpc == NULL)
1046             break;
1047         if (mpc->mpoad_vcc != NULL)
1048             start_mpc(mpc, dev);
1049         break;
1050     case NETDEV_DOWN:
1051         /* the dev was ifconfig'ed down */
1052         /* this means that the flow of packets from the
1053          * upper layer stops
1054          */
1055         mpc = find_mpc_by_lec(dev);
1056         if (mpc == NULL)
1057             break;
1058         if (mpc->mpoad_vcc != NULL)
1059             stop_mpc(mpc);
1060         break;
1061     case NETDEV_REBOOT:
1062     case NETDEV_CHANGE:
1063     case NETDEV_CHANGEMTU:
1064     case NETDEV_CHANGEADDR:
1065     case NETDEV_GOING_DOWN:
1066         break;
1067     default:
1068         break;
1069     }
1070 
1071     return NOTIFY_DONE;
1072 }
1073 
1074 /*
1075  * Functions which are called after a message is received from mpcd.
1076  * Msg is reused on purpose.
1077  */
1078 
1079 
1080 static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1081 {
1082     __be32 dst_ip = msg->content.in_info.in_dst_ip;
1083     in_cache_entry *entry;
1084 
1085     entry = mpc->in_ops->get(dst_ip, mpc);
1086     if (entry == NULL) {
1087         entry = mpc->in_ops->add_entry(dst_ip, mpc);
1088         entry->entry_state = INGRESS_RESOLVING;
1089         msg->type = SND_MPOA_RES_RQST;
1090         msg->content.in_info = entry->ctrl_info;
1091         msg_to_mpoad(msg, mpc);
1092         entry->reply_wait = ktime_get_seconds();
1093         mpc->in_ops->put(entry);
1094         return;
1095     }
1096 
1097     if (entry->entry_state == INGRESS_INVALID) {
1098         entry->entry_state = INGRESS_RESOLVING;
1099         msg->type = SND_MPOA_RES_RQST;
1100         msg->content.in_info = entry->ctrl_info;
1101         msg_to_mpoad(msg, mpc);
1102         entry->reply_wait = ktime_get_seconds();
1103         mpc->in_ops->put(entry);
1104         return;
1105     }
1106 
1107     pr_info("(%s) entry already in resolving state\n",
1108         (mpc->dev) ? mpc->dev->name : "<unknown>");
1109     mpc->in_ops->put(entry);
1110 }
1111 
1112 /*
1113  * Things get complicated because we have to check if there's an egress
1114  * shortcut with suitable traffic parameters we could use.
1115  */
1116 static void check_qos_and_open_shortcut(struct k_message *msg,
1117                     struct mpoa_client *client,
1118                     in_cache_entry *entry)
1119 {
1120     __be32 dst_ip = msg->content.in_info.in_dst_ip;
1121     struct atm_mpoa_qos *qos = atm_mpoa_search_qos(dst_ip);
1122     eg_cache_entry *eg_entry = client->eg_ops->get_by_src_ip(dst_ip, client);
1123 
1124     if (eg_entry && eg_entry->shortcut) {
1125         if (eg_entry->shortcut->qos.txtp.traffic_class &
1126             msg->qos.txtp.traffic_class &
1127             (qos ? qos->qos.txtp.traffic_class : ATM_UBR | ATM_CBR)) {
1128             if (eg_entry->shortcut->qos.txtp.traffic_class == ATM_UBR)
1129                 entry->shortcut = eg_entry->shortcut;
1130             else if (eg_entry->shortcut->qos.txtp.max_pcr > 0)
1131                 entry->shortcut = eg_entry->shortcut;
1132         }
1133         if (entry->shortcut) {
1134             dprintk("(%s) using egress SVC to reach %pI4\n",
1135                 client->dev->name, &dst_ip);
1136             client->eg_ops->put(eg_entry);
1137             return;
1138         }
1139     }
1140     if (eg_entry != NULL)
1141         client->eg_ops->put(eg_entry);
1142 
1143     /* No luck in the egress cache we must open an ingress SVC */
1144     msg->type = OPEN_INGRESS_SVC;
1145     if (qos &&
1146         (qos->qos.txtp.traffic_class == msg->qos.txtp.traffic_class)) {
1147         msg->qos = qos->qos;
1148         pr_info("(%s) trying to get a CBR shortcut\n",
1149             client->dev->name);
1150     } else
1151         memset(&msg->qos, 0, sizeof(struct atm_qos));
1152     msg_to_mpoad(msg, client);
1153 }
1154 
1155 static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1156 {
1157     __be32 dst_ip = msg->content.in_info.in_dst_ip;
1158     in_cache_entry *entry = mpc->in_ops->get(dst_ip, mpc);
1159 
1160     dprintk("(%s) ip %pI4\n",
1161         mpc->dev->name, &dst_ip);
1162     ddprintk("(%s) entry = %p",
1163          mpc->dev->name, entry);
1164     if (entry == NULL) {
1165         pr_info("(%s) ARGH, received res. reply for an entry that doesn't exist.\n",
1166             mpc->dev->name);
1167         return;
1168     }
1169     ddprintk_cont(" entry_state = %d ", entry->entry_state);
1170 
1171     if (entry->entry_state == INGRESS_RESOLVED) {
1172         pr_info("(%s) RESOLVED entry!\n", mpc->dev->name);
1173         mpc->in_ops->put(entry);
1174         return;
1175     }
1176 
1177     entry->ctrl_info = msg->content.in_info;
1178     entry->time = ktime_get_seconds();
1179     /* Used in refreshing func from now on */
1180     entry->reply_wait = ktime_get_seconds();
1181     entry->refresh_time = 0;
1182     ddprintk_cont("entry->shortcut = %p\n", entry->shortcut);
1183 
1184     if (entry->entry_state == INGRESS_RESOLVING &&
1185         entry->shortcut != NULL) {
1186         entry->entry_state = INGRESS_RESOLVED;
1187         mpc->in_ops->put(entry);
1188         return; /* Shortcut already open... */
1189     }
1190 
1191     if (entry->shortcut != NULL) {
1192         pr_info("(%s) entry->shortcut != NULL, impossible!\n",
1193             mpc->dev->name);
1194         mpc->in_ops->put(entry);
1195         return;
1196     }
1197 
1198     check_qos_and_open_shortcut(msg, mpc, entry);
1199     entry->entry_state = INGRESS_RESOLVED;
1200     mpc->in_ops->put(entry);
1201 
1202     return;
1203 
1204 }
1205 
1206 static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1207 {
1208     __be32 dst_ip = msg->content.in_info.in_dst_ip;
1209     __be32 mask = msg->ip_mask;
1210     in_cache_entry *entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1211 
1212     if (entry == NULL) {
1213         pr_info("(%s) purge for a non-existing entry, ip = %pI4\n",
1214             mpc->dev->name, &dst_ip);
1215         return;
1216     }
1217 
1218     do {
1219         dprintk("(%s) removing an ingress entry, ip = %pI4\n",
1220             mpc->dev->name, &dst_ip);
1221         write_lock_bh(&mpc->ingress_lock);
1222         mpc->in_ops->remove_entry(entry, mpc);
1223         write_unlock_bh(&mpc->ingress_lock);
1224         mpc->in_ops->put(entry);
1225         entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1226     } while (entry != NULL);
1227 }
1228 
1229 static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1230 {
1231     __be32 cache_id = msg->content.eg_info.cache_id;
1232     eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(cache_id, mpc);
1233 
1234     if (entry == NULL) {
1235         dprintk("(%s) purge for a non-existing entry\n",
1236             mpc->dev->name);
1237         return;
1238     }
1239 
1240     write_lock_irq(&mpc->egress_lock);
1241     mpc->eg_ops->remove_entry(entry, mpc);
1242     write_unlock_irq(&mpc->egress_lock);
1243 
1244     mpc->eg_ops->put(entry);
1245 }
1246 
1247 static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry)
1248 {
1249     struct sock *sk;
1250     struct k_message *purge_msg;
1251     struct sk_buff *skb;
1252 
1253     dprintk("entering\n");
1254     if (vcc == NULL) {
1255         pr_info("vcc == NULL\n");
1256         return;
1257     }
1258 
1259     skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
1260     if (skb == NULL) {
1261         pr_info("out of memory\n");
1262         return;
1263     }
1264 
1265     skb_put(skb, sizeof(struct k_message));
1266     memset(skb->data, 0, sizeof(struct k_message));
1267     purge_msg = (struct k_message *)skb->data;
1268     purge_msg->type = DATA_PLANE_PURGE;
1269     if (entry != NULL)
1270         purge_msg->content.eg_info = entry->ctrl_info;
1271 
1272     atm_force_charge(vcc, skb->truesize);
1273 
1274     sk = sk_atm(vcc);
1275     skb_queue_tail(&sk->sk_receive_queue, skb);
1276     sk->sk_data_ready(sk);
1277     dprintk("exiting\n");
1278 }
1279 
1280 /*
1281  * Our MPS died. Tell our daemon to send NHRP data plane purge to each
1282  * of the egress shortcuts we have.
1283  */
1284 static void mps_death(struct k_message *msg, struct mpoa_client *mpc)
1285 {
1286     eg_cache_entry *entry;
1287 
1288     dprintk("(%s)\n", mpc->dev->name);
1289 
1290     if (memcmp(msg->MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN)) {
1291         pr_info("(%s) wrong MPS\n", mpc->dev->name);
1292         return;
1293     }
1294 
1295     /* FIXME: This knows too much of the cache structure */
1296     read_lock_irq(&mpc->egress_lock);
1297     entry = mpc->eg_cache;
1298     while (entry != NULL) {
1299         purge_egress_shortcut(entry->shortcut, entry);
1300         entry = entry->next;
1301     }
1302     read_unlock_irq(&mpc->egress_lock);
1303 
1304     mpc->in_ops->destroy_cache(mpc);
1305     mpc->eg_ops->destroy_cache(mpc);
1306 }
1307 
1308 static void MPOA_cache_impos_rcvd(struct k_message *msg,
1309                   struct mpoa_client *mpc)
1310 {
1311     uint16_t holding_time;
1312     eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(msg->content.eg_info.cache_id, mpc);
1313 
1314     holding_time = msg->content.eg_info.holding_time;
1315     dprintk("(%s) entry = %p, holding_time = %u\n",
1316         mpc->dev->name, entry, holding_time);
1317     if (entry == NULL && holding_time) {
1318         entry = mpc->eg_ops->add_entry(msg, mpc);
1319         mpc->eg_ops->put(entry);
1320         return;
1321     }
1322     if (holding_time) {
1323         mpc->eg_ops->update(entry, holding_time);
1324         return;
1325     }
1326 
1327     write_lock_irq(&mpc->egress_lock);
1328     mpc->eg_ops->remove_entry(entry, mpc);
1329     write_unlock_irq(&mpc->egress_lock);
1330 
1331     mpc->eg_ops->put(entry);
1332 }
1333 
1334 static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg,
1335                    struct mpoa_client *mpc)
1336 {
1337     struct lec_priv *priv;
1338     int i, retval ;
1339 
1340     uint8_t tlv[4 + 1 + 1 + 1 + ATM_ESA_LEN];
1341 
1342     tlv[0] = 00; tlv[1] = 0xa0; tlv[2] = 0x3e; tlv[3] = 0x2a; /* type  */
1343     tlv[4] = 1 + 1 + ATM_ESA_LEN;  /* length                           */
1344     tlv[5] = 0x02;                 /* MPOA client                      */
1345     tlv[6] = 0x00;                 /* number of MPS MAC addresses      */
1346 
1347     memcpy(&tlv[7], mesg->MPS_ctrl, ATM_ESA_LEN); /* MPC ctrl ATM addr */
1348     memcpy(mpc->our_ctrl_addr, mesg->MPS_ctrl, ATM_ESA_LEN);
1349 
1350     dprintk("(%s) setting MPC ctrl ATM address to",
1351         mpc->dev ? mpc->dev->name : "<unknown>");
1352     for (i = 7; i < sizeof(tlv); i++)
1353         dprintk_cont(" %02x", tlv[i]);
1354     dprintk_cont("\n");
1355 
1356     if (mpc->dev) {
1357         priv = netdev_priv(mpc->dev);
1358         retval = priv->lane2_ops->associate_req(mpc->dev,
1359                             mpc->dev->dev_addr,
1360                             tlv, sizeof(tlv));
1361         if (retval == 0)
1362             pr_info("(%s) MPOA device type TLV association failed\n",
1363                 mpc->dev->name);
1364         retval = priv->lane2_ops->resolve(mpc->dev, NULL, 1, NULL, NULL);
1365         if (retval < 0)
1366             pr_info("(%s) targetless LE_ARP request failed\n",
1367                 mpc->dev->name);
1368     }
1369 }
1370 
1371 static void set_mps_mac_addr_rcvd(struct k_message *msg,
1372                   struct mpoa_client *client)
1373 {
1374 
1375     if (client->number_of_mps_macs)
1376         kfree(client->mps_macs);
1377     client->number_of_mps_macs = 0;
1378     client->mps_macs = kmemdup(msg->MPS_ctrl, ETH_ALEN, GFP_KERNEL);
1379     if (client->mps_macs == NULL) {
1380         pr_info("out of memory\n");
1381         return;
1382     }
1383     client->number_of_mps_macs = 1;
1384 }
1385 
1386 /*
1387  * purge egress cache and tell daemon to 'action' (DIE, RELOAD)
1388  */
1389 static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action)
1390 {
1391 
1392     eg_cache_entry *entry;
1393     msg->type = SND_EGRESS_PURGE;
1394 
1395 
1396     /* FIXME: This knows too much of the cache structure */
1397     read_lock_irq(&mpc->egress_lock);
1398     entry = mpc->eg_cache;
1399     while (entry != NULL) {
1400         msg->content.eg_info = entry->ctrl_info;
1401         dprintk("cache_id %u\n", entry->ctrl_info.cache_id);
1402         msg_to_mpoad(msg, mpc);
1403         entry = entry->next;
1404     }
1405     read_unlock_irq(&mpc->egress_lock);
1406 
1407     msg->type = action;
1408     msg_to_mpoad(msg, mpc);
1409 }
1410 
1411 static unsigned long checking_time;
1412 
1413 static void mpc_timer_refresh(void)
1414 {
1415     mpc_timer.expires = jiffies + (MPC_P2 * HZ);
1416     checking_time = mpc_timer.expires;
1417     add_timer(&mpc_timer);
1418 }
1419 
1420 static void mpc_cache_check(struct timer_list *unused)
1421 {
1422     struct mpoa_client *mpc = mpcs;
1423     static unsigned long previous_resolving_check_time;
1424     static unsigned long previous_refresh_time;
1425 
1426     while (mpc != NULL) {
1427         mpc->in_ops->clear_count(mpc);
1428         mpc->eg_ops->clear_expired(mpc);
1429         if (checking_time - previous_resolving_check_time >
1430             mpc->parameters.mpc_p4 * HZ) {
1431             mpc->in_ops->check_resolving(mpc);
1432             previous_resolving_check_time = checking_time;
1433         }
1434         if (checking_time - previous_refresh_time >
1435             mpc->parameters.mpc_p5 * HZ) {
1436             mpc->in_ops->refresh(mpc);
1437             previous_refresh_time = checking_time;
1438         }
1439         mpc = mpc->next;
1440     }
1441     mpc_timer_refresh();
1442 }
1443 
1444 static int atm_mpoa_ioctl(struct socket *sock, unsigned int cmd,
1445               unsigned long arg)
1446 {
1447     int err = 0;
1448     struct atm_vcc *vcc = ATM_SD(sock);
1449 
1450     if (cmd != ATMMPC_CTRL && cmd != ATMMPC_DATA)
1451         return -ENOIOCTLCMD;
1452 
1453     if (!capable(CAP_NET_ADMIN))
1454         return -EPERM;
1455 
1456     switch (cmd) {
1457     case ATMMPC_CTRL:
1458         err = atm_mpoa_mpoad_attach(vcc, (int)arg);
1459         if (err >= 0)
1460             sock->state = SS_CONNECTED;
1461         break;
1462     case ATMMPC_DATA:
1463         err = atm_mpoa_vcc_attach(vcc, (void __user *)arg);
1464         break;
1465     default:
1466         break;
1467     }
1468     return err;
1469 }
1470 
1471 static struct atm_ioctl atm_ioctl_ops = {
1472     .owner  = THIS_MODULE,
1473     .ioctl  = atm_mpoa_ioctl,
1474 };
1475 
1476 static __init int atm_mpoa_init(void)
1477 {
1478     register_atm_ioctl(&atm_ioctl_ops);
1479 
1480     if (mpc_proc_init() != 0)
1481         pr_info("failed to initialize /proc/mpoa\n");
1482 
1483     pr_info("mpc.c: initialized\n");
1484 
1485     return 0;
1486 }
1487 
1488 static void __exit atm_mpoa_cleanup(void)
1489 {
1490     struct mpoa_client *mpc, *tmp;
1491     struct atm_mpoa_qos *qos, *nextqos;
1492     struct lec_priv *priv;
1493 
1494     mpc_proc_clean();
1495 
1496     del_timer_sync(&mpc_timer);
1497     unregister_netdevice_notifier(&mpoa_notifier);
1498     deregister_atm_ioctl(&atm_ioctl_ops);
1499 
1500     mpc = mpcs;
1501     mpcs = NULL;
1502     while (mpc != NULL) {
1503         tmp = mpc->next;
1504         if (mpc->dev != NULL) {
1505             stop_mpc(mpc);
1506             priv = netdev_priv(mpc->dev);
1507             if (priv->lane2_ops != NULL)
1508                 priv->lane2_ops->associate_indicator = NULL;
1509         }
1510         ddprintk("about to clear caches\n");
1511         mpc->in_ops->destroy_cache(mpc);
1512         mpc->eg_ops->destroy_cache(mpc);
1513         ddprintk("caches cleared\n");
1514         kfree(mpc->mps_macs);
1515         memset(mpc, 0, sizeof(struct mpoa_client));
1516         ddprintk("about to kfree %p\n", mpc);
1517         kfree(mpc);
1518         ddprintk("next mpc is at %p\n", tmp);
1519         mpc = tmp;
1520     }
1521 
1522     qos = qos_head;
1523     qos_head = NULL;
1524     while (qos != NULL) {
1525         nextqos = qos->next;
1526         dprintk("freeing qos entry %p\n", qos);
1527         kfree(qos);
1528         qos = nextqos;
1529     }
1530 }
1531 
1532 module_init(atm_mpoa_init);
1533 module_exit(atm_mpoa_cleanup);
1534 
1535 MODULE_LICENSE("GPL");