1 #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
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>
11 /* We are an ethernet device */
12 #include <linux/if_ether.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
16 #include <linux/skbuff.h>
18 #include <linux/uaccess.h>
19 #include <asm/byteorder.h>
20 #include <net/checksum.h> /* for ip_fast_csum() */
23 #include <linux/proc_fs.h>
26 #include <linux/atmdev.h>
27 #include <linux/atmlec.h>
28 #include <linux/atmmpc.h>
30 #include <linux/module.h>
34 #include "resources.h"
37 * mpc.c: Implementation of MPOA client kernel part
41 #define dprintk(format, args...) printk(KERN_DEBUG format, ##args) /* debug */
43 #define dprintk(format, args...) \
44 do { if (0) printk(KERN_DEBUG format, ##args); } while (0)
48 #define ddprintk printk(KERN_DEBUG format, ##args) /* more debug */
50 #define ddprintk(format, args...) \
51 do { if (0) printk(KERN_DEBUG format, ##args); } while (0)
56 #define MPOA_TAG_LEN 4
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,
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);
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,
77 static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry);
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);
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);
91 static struct llc_snap_hdr llc_snap_mpoa_ctrl = {
94 {0x00, 0x03} /* For MPOA control PDUs */
96 static struct llc_snap_hdr llc_snap_mpoa_data = {
99 {0x08, 0x00} /* This is for IP PDUs only */
101 static struct llc_snap_hdr llc_snap_mpoa_data_tagged = {
104 {0x88, 0x4c} /* This is for tagged data PDUs */
107 static struct notifier_block mpoa_notifier = {
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);
118 static struct mpoa_client *find_mpc_by_itfnum(int itf)
120 struct mpoa_client *mpc;
122 mpc = mpcs; /* our global linked list */
123 while (mpc != NULL) {
124 if (mpc->dev_num == itf)
129 return NULL; /* not found */
132 static struct mpoa_client *find_mpc_by_vcc(struct atm_vcc *vcc)
134 struct mpoa_client *mpc;
136 mpc = mpcs; /* our global linked list */
137 while (mpc != NULL) {
138 if (mpc->mpoad_vcc == vcc)
143 return NULL; /* not found */
146 static struct mpoa_client *find_mpc_by_lec(struct net_device *dev)
148 struct mpoa_client *mpc;
150 mpc = mpcs; /* our global linked list */
151 while (mpc != NULL) {
157 return NULL; /* not found */
161 * Functions for managing QoS list
165 * Overwrites the old entry or makes a new one.
167 struct atm_mpoa_qos *atm_mpoa_add_qos(__be32 dst_ip, struct atm_qos *qos)
169 struct atm_mpoa_qos *entry;
171 entry = atm_mpoa_search_qos(dst_ip);
177 entry = kmalloc(sizeof(struct atm_mpoa_qos), GFP_KERNEL);
179 pr_info("mpoa: out of memory\n");
183 entry->ipaddr = dst_ip;
186 entry->next = qos_head;
192 struct atm_mpoa_qos *atm_mpoa_search_qos(__be32 dst_ip)
194 struct atm_mpoa_qos *qos;
198 if (qos->ipaddr == dst_ip)
207 * Returns 0 for failure
209 int atm_mpoa_delete_qos(struct atm_mpoa_qos *entry)
211 struct atm_mpoa_qos *curr;
215 if (entry == qos_head) {
216 qos_head = qos_head->next;
222 while (curr != NULL) {
223 if (curr->next == entry) {
224 curr->next = entry->next;
234 /* this is buggered - we need locking for qos_head */
235 void atm_mpoa_disp_qos(struct seq_file *m)
237 struct atm_mpoa_qos *qos;
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");
243 while (qos != NULL) {
244 seq_printf(m, "%pI4\n %-7d %-7d %-7d %-7d %-7d\n %-7d %-7d %-7d %-7d %-7d\n",
246 qos->qos.txtp.max_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,
253 qos->qos.rxtp.min_pcr,
254 qos->qos.rxtp.max_cdv,
255 qos->qos.rxtp.max_sdu);
260 static struct net_device *find_lec_by_itfnum(int itf)
262 struct net_device *dev;
265 sprintf(name, "lec%d", itf);
266 dev = dev_get_by_name(&init_net, name);
271 static struct mpoa_client *alloc_mpc(void)
273 struct mpoa_client *mpc;
275 mpc = kzalloc(sizeof(struct mpoa_client), GFP_KERNEL);
278 rwlock_init(&mpc->ingress_lock);
279 rwlock_init(&mpc->egress_lock);
281 atm_mpoa_init_cache(mpc);
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;
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.
302 static void start_mpc(struct mpoa_client *mpc, struct net_device *dev)
305 dprintk("mpoa: (%s) start_mpc:\n", mpc->dev->name);
306 if (!dev->netdev_ops)
307 pr_info("(%s) not starting\n", dev->name);
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;
316 static void stop_mpc(struct mpoa_client *mpc)
318 struct net_device *dev = mpc->dev;
319 dprintk("mpoa: (%s) stop_mpc:", mpc->dev->name);
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");
328 dev->netdev_ops = mpc->old_ops;
331 /* close_shortcuts(mpc); ??? FIXME */
334 static const char *mpoa_device_type_string(char type) __attribute__ ((unused));
336 static const char *mpoa_device_type_string(char type)
340 return "non-MPOA device";
346 return "both MPS and MPC";
349 return "unspecified (non-MPOA) device";
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
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
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.
365 static void lane2_assoc_ind(struct net_device *dev, const u8 *mac_addr,
366 const u8 *tlvs, u32 sizeoftlvs)
369 uint8_t length, mpoa_device_type, number_of_mps_macs;
370 const uint8_t *end_of_tlvs;
371 struct mpoa_client *mpc;
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 */
378 pr_info("(%s) no mpc\n", dev->name);
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]);
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");
394 pr_info("mpoa: (%s) TLV type was 0, returning\n",
399 if (type != TLV_MPOA_DEVICE_TYPE) {
401 continue; /* skip other TLVs */
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",
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);
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)
423 continue; /* we are only interested in MPSs */
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 */
430 dprintk("this MPS has %d MAC addresses\n", number_of_mps_macs);
433 * ok, now we can go and tell our daemon
434 * the control address of MPS
436 send_set_mps_ctrl_addr(tlvs, mpc);
438 tlvs = copy_macs(mpc, mac_addr, tlvs,
439 number_of_mps_macs, mpoa_device_type);
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);
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.
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,
460 num_macs = (mps_macs > 1) ? mps_macs : 1;
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);
472 memcpy(mpc->mps_macs, router_mac, ETH_ALEN);
473 tlvs += 20; if (device_type == MPS_AND_MPC) tlvs += 20;
475 memcpy(mpc->mps_macs, tlvs, mps_macs*ETH_ALEN);
476 tlvs += mps_macs*ETH_ALEN;
477 mpc->number_of_mps_macs = num_macs;
482 static int send_via_shortcut(struct sk_buff *skb, struct mpoa_client *mpc)
484 in_cache_entry *entry;
490 struct llc_snap_hdr hdr;
492 } tagged_llc_snap_hdr = {
493 {0xaa, 0xaa, 0x03, {0x00, 0x00, 0x00}, {0x88, 0x4c}},
497 buff = skb->data + mpc->dev->hard_header_len;
498 iph = (struct iphdr *)buff;
501 ddprintk("mpoa: (%s) send_via_shortcut: ipaddr 0x%x\n",
502 mpc->dev->name, ipaddr);
504 entry = mpc->in_ops->get(ipaddr, mpc);
506 entry = mpc->in_ops->add_entry(ipaddr, mpc);
508 mpc->in_ops->put(entry);
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",
515 mpc->in_ops->put(entry);
519 ddprintk("mpoa: (%s) send_via_shortcut: using shortcut\n",
521 /* MPOA spec A.1.4, MPOA client must decrement IP ttl at least by one */
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);
530 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
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));
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));
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);
559 * Probably needs some error checks and locking, not sure...
561 static netdev_tx_t mpc_send_packet(struct sk_buff *skb,
562 struct net_device *dev)
564 struct mpoa_client *mpc;
568 mpc = find_mpc_by_lec(dev); /* this should NEVER fail */
570 pr_info("(%s) no MPC found\n", dev->name);
574 eth = (struct ethhdr *)skb->data;
575 if (eth->h_proto != htons(ETH_P_IP))
576 goto non_ip; /* Multi-Protocol Over ATM :-) */
578 /* Weed out funny packets (e.g., AF_PACKET or raw). */
579 if (skb->len < ETH_HLEN + sizeof(struct iphdr))
581 skb_set_network_header(skb, ETH_HLEN);
582 if (skb->len < ETH_HLEN + ip_hdr(skb)->ihl * 4 || ip_hdr(skb)->ihl < 5)
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 */
594 return mpc->old_ops->ndo_start_xmit(skb, dev);
597 static int atm_mpoa_vcc_attach(struct atm_vcc *vcc, void __user *arg)
600 struct mpoa_client *mpc;
601 struct atmmpc_ioc ioc_data;
602 in_cache_entry *in_entry;
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",
611 ipaddr = ioc_data.ipaddr;
612 if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF)
615 mpc = find_mpc_by_itfnum(ioc_data.dev_num);
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",
625 if (in_entry != NULL)
626 mpc->in_ops->put(in_entry);
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);
634 pr_info("(%s) attaching egress SVC\n", mpc->dev->name);
637 vcc->proto_data = mpc->dev;
638 vcc->push = mpc_push;
646 static void mpc_vcc_close(struct atm_vcc *vcc, struct net_device *dev)
648 struct mpoa_client *mpc;
649 in_cache_entry *in_entry;
650 eg_cache_entry *eg_entry;
652 mpc = find_mpc_by_lec(dev);
654 pr_info("(%s) close for unknown MPC\n", dev->name);
658 dprintk("mpoa: (%s) mpc_vcc_close:\n", dev->name);
659 in_entry = mpc->in_ops->get_by_vcc(vcc, mpc);
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);
666 eg_entry = mpc->eg_ops->get_by_vcc(vcc, mpc);
668 dprintk("mpoa: (%s) mpc_vcc_close: egress SVC closed\n",
670 eg_entry->shortcut = NULL;
671 mpc->eg_ops->put(eg_entry);
674 if (in_entry == NULL && eg_entry == NULL)
675 dprintk("mpoa: (%s) mpc_vcc_close: unused vcc closed\n",
681 static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb)
683 struct net_device *dev = (struct net_device *)vcc->proto_data;
684 struct sk_buff *new_skb;
686 struct mpoa_client *mpc;
690 ddprintk("mpoa: (%s) mpc_push:\n", dev->name);
692 dprintk("mpoa: (%s) mpc_push: null skb, closing VCC\n",
694 mpc_vcc_close(vcc, dev);
699 if (memcmp(skb->data, &llc_snap_mpoa_ctrl,
700 sizeof(struct llc_snap_hdr)) == 0) {
701 struct sock *sk = sk_atm(vcc);
703 dprintk("mpoa: (%s) mpc_push: control packet arrived\n",
705 /* Pass control packets to daemon */
706 skb_queue_tail(&sk->sk_receive_queue, skb);
707 sk->sk_data_ready(sk, skb->len);
711 /* data coming over the shortcut */
712 atm_return(vcc, skb->truesize);
714 mpc = find_mpc_by_lec(dev);
716 pr_info("(%s) unknown MPC\n", dev->name);
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",
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",
729 dev_kfree_skb_any(skb);
732 pr_info("(%s) garbage arrived, purging\n", dev->name);
733 dev_kfree_skb_any(skb);
737 tmp = skb->data + sizeof(struct llc_snap_hdr);
738 tag = *(__be32 *)tmp;
740 eg = mpc->eg_ops->get_by_tag(tag, mpc);
742 pr_info("mpoa: (%s) Didn't find egress cache entry, tag = %u\n",
744 purge_egress_shortcut(vcc, NULL);
745 dev_kfree_skb_any(skb);
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.
753 if (eg->shortcut == NULL) {
755 pr_info("(%s) egress SVC in use\n", dev->name);
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);
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);
773 eg->latest_ip_addr = ip_hdr(new_skb)->saddr;
775 mpc->eg_ops->put(eg);
777 memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
783 static struct atmdev_ops mpc_ops = { /* only send is required */
784 .close = mpoad_close,
785 .send = msg_from_mpoad
788 static struct atm_dev mpc_dev = {
792 .lock = __SPIN_LOCK_UNLOCKED(mpc_dev.lock)
793 /* members not explicitly initialised will be 0 */
796 static int atm_mpoa_mpoad_attach(struct atm_vcc *vcc, int arg)
798 struct mpoa_client *mpc;
799 struct lec_priv *priv;
803 init_timer(&mpc_timer);
806 /* This lets us now how our LECs are doing */
807 err = register_netdevice_notifier(&mpoa_notifier);
809 del_timer(&mpc_timer);
814 mpc = find_mpc_by_itfnum(arg);
816 dprintk("mpoa: mpoad_attach: allocating new mpc for itf %d\n",
822 mpc->dev = find_lec_by_itfnum(arg);
823 /* NULL if there was no lec */
825 if (mpc->mpoad_vcc) {
826 pr_info("mpoad is already present for itf %d\n", arg);
830 if (mpc->dev) { /* check if the lec is LANE2 capable */
831 priv = netdev_priv(mpc->dev);
832 if (priv->lane_version < 2) {
836 priv->lane2_ops->associate_indicator = lane2_assoc_ind;
839 mpc->mpoad_vcc = vcc;
841 vcc_insert_socket(sk_atm(vcc));
842 set_bit(ATM_VF_META, &vcc->flags);
843 set_bit(ATM_VF_READY, &vcc->flags);
846 char empty[ATM_ESA_LEN];
847 memset(empty, 0, ATM_ESA_LEN);
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
853 if (memcmp(mpc->mps_ctrl_addr, empty, ATM_ESA_LEN) != 0)
854 send_set_mps_ctrl_addr(mpc->mps_ctrl_addr, mpc);
857 __module_get(THIS_MODULE);
861 static void send_set_mps_ctrl_addr(const char *addr, struct mpoa_client *mpc)
863 struct k_message mesg;
865 memcpy(mpc->mps_ctrl_addr, addr, ATM_ESA_LEN);
867 mesg.type = SET_MPS_CTRL_ADDR;
868 memcpy(mesg.MPS_ctrl, addr, ATM_ESA_LEN);
869 msg_to_mpoad(&mesg, mpc);
874 static void mpoad_close(struct atm_vcc *vcc)
876 struct mpoa_client *mpc;
879 mpc = find_mpc_by_vcc(vcc);
881 pr_info("did not find MPC\n");
884 if (!mpc->mpoad_vcc) {
885 pr_info("close for non-present mpoad\n");
889 mpc->mpoad_vcc = NULL;
891 struct lec_priv *priv = netdev_priv(mpc->dev);
892 priv->lane2_ops->associate_indicator = NULL;
897 mpc->in_ops->destroy_cache(mpc);
898 mpc->eg_ops->destroy_cache(mpc);
900 while ((skb = skb_dequeue(&sk_atm(vcc)->sk_receive_queue))) {
901 atm_return(vcc, skb->truesize);
905 pr_info("(%s) going down\n",
906 (mpc->dev) ? mpc->dev->name : "<unknown>");
907 module_put(THIS_MODULE);
915 static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb)
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);
923 pr_info("no mpc found\n");
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);
933 case MPOA_TRIGGER_RCVD:
934 dprintk(" mpoa_trigger_rcvd\n");
935 MPOA_trigger_rcvd(mesg, mpc);
937 case INGRESS_PURGE_RCVD:
938 dprintk(" nhrp_purge_rcvd\n");
939 ingress_purge_rcvd(mesg, mpc);
941 case EGRESS_PURGE_RCVD:
942 dprintk(" egress_purge_reply_rcvd\n");
943 egress_purge_rcvd(mesg, mpc);
946 dprintk(" mps_death\n");
947 mps_death(mesg, mpc);
949 case CACHE_IMPOS_RCVD:
950 dprintk(" cache_impos_rcvd\n");
951 MPOA_cache_impos_rcvd(mesg, mpc);
953 case SET_MPC_CTRL_ADDR:
954 dprintk(" set_mpc_ctrl_addr\n");
955 set_mpc_ctrl_addr_rcvd(mesg, mpc);
957 case SET_MPS_MAC_ADDR:
958 dprintk(" set_mps_mac_addr\n");
959 set_mps_mac_addr_rcvd(mesg, mpc);
961 case CLEAN_UP_AND_EXIT:
962 dprintk(" clean_up_and_exit\n");
963 clean_up(mesg, mpc, DIE);
966 dprintk(" reload\n");
967 clean_up(mesg, mpc, RELOAD);
970 dprintk(" set_mpc_params\n");
971 mpc->parameters = mesg->content.params;
974 dprintk(" unknown message %d\n", mesg->type);
982 /* Remember that this function may not do things that sleep */
983 int msg_to_mpoad(struct k_message *mesg, struct mpoa_client *mpc)
988 if (mpc == NULL || !mpc->mpoad_vcc) {
989 pr_info("mesg %d to a non-existent mpoad\n", mesg->type);
993 skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
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);
1000 sk = sk_atm(mpc->mpoad_vcc);
1001 skb_queue_tail(&sk->sk_receive_queue, skb);
1002 sk->sk_data_ready(sk, skb->len);
1007 static int mpoa_event_listener(struct notifier_block *mpoa_notifier,
1008 unsigned long event, void *dev_ptr)
1010 struct net_device *dev;
1011 struct mpoa_client *mpc;
1012 struct lec_priv *priv;
1014 dev = (struct net_device *)dev_ptr;
1016 if (!net_eq(dev_net(dev), &init_net))
1019 if (dev->name == NULL || strncmp(dev->name, "lec", 3))
1020 return NOTIFY_DONE; /* we are only interested in lec:s */
1023 case NETDEV_REGISTER: /* a new lec device was allocated */
1024 priv = netdev_priv(dev);
1025 if (priv->lane_version < 2)
1027 priv->lane2_ops->associate_indicator = lane2_assoc_ind;
1028 mpc = find_mpc_by_itfnum(priv->itfnum);
1030 dprintk("mpoa: mpoa_event_listener: allocating new mpc for %s\n",
1034 pr_info("no new mpc");
1038 mpc->dev_num = priv->itfnum;
1041 dprintk("mpoa: (%s) was initialized\n", dev->name);
1043 case NETDEV_UNREGISTER:
1044 /* the lec device was deallocated */
1045 mpc = find_mpc_by_lec(dev);
1048 dprintk("mpoa: device (%s) was deallocated\n", dev->name);
1054 /* the dev was ifconfig'ed up */
1055 mpc = find_mpc_by_lec(dev);
1058 if (mpc->mpoad_vcc != NULL)
1059 start_mpc(mpc, dev);
1062 /* the dev was ifconfig'ed down */
1063 /* this means that the flow of packets from the
1066 mpc = find_mpc_by_lec(dev);
1069 if (mpc->mpoad_vcc != NULL)
1074 case NETDEV_CHANGEMTU:
1075 case NETDEV_CHANGEADDR:
1076 case NETDEV_GOING_DOWN:
1086 * Functions which are called after a message is received from mpcd.
1087 * Msg is reused on purpose.
1091 static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1093 __be32 dst_ip = msg->content.in_info.in_dst_ip;
1094 in_cache_entry *entry;
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);
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);
1118 pr_info("(%s) entry already in resolving state\n",
1119 (mpc->dev) ? mpc->dev->name : "<unknown>");
1120 mpc->in_ops->put(entry);
1125 * Things get complicated because we have to check if there's an egress
1126 * shortcut with suitable traffic parameters we could use.
1128 static void check_qos_and_open_shortcut(struct k_message *msg,
1129 struct mpoa_client *client,
1130 in_cache_entry *entry)
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);
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;
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);
1152 if (eg_entry != NULL)
1153 client->eg_ops->put(eg_entry);
1155 /* No luck in the egress cache we must open an ingress SVC */
1156 msg->type = OPEN_INGRESS_SVC;
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",
1163 memset(&msg->qos, 0, sizeof(struct atm_qos));
1164 msg_to_mpoad(msg, client);
1168 static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1170 __be32 dst_ip = msg->content.in_info.in_dst_ip;
1171 in_cache_entry *entry = mpc->in_ops->get(dst_ip, mpc);
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",
1182 ddprintk(" entry_state = %d ", entry->entry_state);
1184 if (entry->entry_state == INGRESS_RESOLVED) {
1185 pr_info("(%s) RESOLVED entry!\n", mpc->dev->name);
1186 mpc->in_ops->put(entry);
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);
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... */
1203 if (entry->shortcut != NULL) {
1204 pr_info("(%s) entry->shortcut != NULL, impossible!\n",
1206 mpc->in_ops->put(entry);
1210 check_qos_and_open_shortcut(msg, mpc, entry);
1211 entry->entry_state = INGRESS_RESOLVED;
1212 mpc->in_ops->put(entry);
1218 static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
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);
1224 if (entry == NULL) {
1225 pr_info("(%s) purge for a non-existing entry, ip = %pI4\n",
1226 mpc->dev->name, &dst_ip);
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);
1243 static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
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);
1248 if (entry == NULL) {
1249 dprintk("mpoa: (%s) egress_purge_rcvd: purge for a non-existing entry\n",
1254 write_lock_irq(&mpc->egress_lock);
1255 mpc->eg_ops->remove_entry(entry, mpc);
1256 write_unlock_irq(&mpc->egress_lock);
1258 mpc->eg_ops->put(entry);
1263 static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry)
1266 struct k_message *purge_msg;
1267 struct sk_buff *skb;
1269 dprintk("mpoa: purge_egress_shortcut: entering\n");
1271 pr_info("vcc == NULL\n");
1275 skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
1277 pr_info("out of memory\n");
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;
1286 purge_msg->content.eg_info = entry->ctrl_info;
1288 atm_force_charge(vcc, skb->truesize);
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");
1299 * Our MPS died. Tell our daemon to send NHRP data plane purge to each
1300 * of the egress shortcuts we have.
1302 static void mps_death(struct k_message *msg, struct mpoa_client *mpc)
1304 eg_cache_entry *entry;
1306 dprintk("mpoa: (%s) mps_death:\n", mpc->dev->name);
1308 if (memcmp(msg->MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN)) {
1309 pr_info("(%s) wrong MPS\n", mpc->dev->name);
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;
1320 read_unlock_irq(&mpc->egress_lock);
1322 mpc->in_ops->destroy_cache(mpc);
1323 mpc->eg_ops->destroy_cache(mpc);
1328 static void MPOA_cache_impos_rcvd(struct k_message *msg,
1329 struct mpoa_client *mpc)
1331 uint16_t holding_time;
1332 eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(msg->content.eg_info.cache_id, mpc);
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);
1343 mpc->eg_ops->update(entry, holding_time);
1347 write_lock_irq(&mpc->egress_lock);
1348 mpc->eg_ops->remove_entry(entry, mpc);
1349 write_unlock_irq(&mpc->egress_lock);
1351 mpc->eg_ops->put(entry);
1356 static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg,
1357 struct mpoa_client *mpc)
1359 struct lec_priv *priv;
1362 uint8_t tlv[4 + 1 + 1 + 1 + ATM_ESA_LEN];
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 */
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);
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]);
1379 priv = netdev_priv(mpc->dev);
1380 retval = priv->lane2_ops->associate_req(mpc->dev,
1384 pr_info("(%s) MPOA device type TLV association failed\n",
1386 retval = priv->lane2_ops->resolve(mpc->dev, NULL, 1, NULL, NULL);
1388 pr_info("(%s) targetless LE_ARP request failed\n",
1395 static void set_mps_mac_addr_rcvd(struct k_message *msg,
1396 struct mpoa_client *client)
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");
1407 client->number_of_mps_macs = 1;
1413 * purge egress cache and tell daemon to 'action' (DIE, RELOAD)
1415 static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action)
1418 eg_cache_entry *entry;
1419 msg->type = SND_EGRESS_PURGE;
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;
1431 read_unlock_irq(&mpc->egress_lock);
1434 msg_to_mpoad(msg, mpc);
1438 static void mpc_timer_refresh(void)
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);
1448 static void mpc_cache_check(unsigned long checking_time)
1450 struct mpoa_client *mpc = mpcs;
1451 static unsigned long previous_resolving_check_time;
1452 static unsigned long previous_refresh_time;
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;
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;
1469 mpc_timer_refresh();
1474 static int atm_mpoa_ioctl(struct socket *sock, unsigned int cmd,
1478 struct atm_vcc *vcc = ATM_SD(sock);
1480 if (cmd != ATMMPC_CTRL && cmd != ATMMPC_DATA)
1481 return -ENOIOCTLCMD;
1483 if (!capable(CAP_NET_ADMIN))
1488 err = atm_mpoa_mpoad_attach(vcc, (int)arg);
1490 sock->state = SS_CONNECTED;
1493 err = atm_mpoa_vcc_attach(vcc, (void __user *)arg);
1501 static struct atm_ioctl atm_ioctl_ops = {
1502 .owner = THIS_MODULE,
1503 .ioctl = atm_mpoa_ioctl,
1506 static __init int atm_mpoa_init(void)
1508 register_atm_ioctl(&atm_ioctl_ops);
1510 if (mpc_proc_init() != 0)
1511 pr_info("failed to initialize /proc/mpoa\n");
1513 pr_info("mpc.c: " __DATE__ " " __TIME__ " initialized\n");
1518 static void __exit atm_mpoa_cleanup(void)
1520 struct mpoa_client *mpc, *tmp;
1521 struct atm_mpoa_qos *qos, *nextqos;
1522 struct lec_priv *priv;
1526 del_timer(&mpc_timer);
1527 unregister_netdevice_notifier(&mpoa_notifier);
1528 deregister_atm_ioctl(&atm_ioctl_ops);
1532 while (mpc != NULL) {
1534 if (mpc->dev != NULL) {
1536 priv = netdev_priv(mpc->dev);
1537 if (priv->lane2_ops != NULL)
1538 priv->lane2_ops->associate_indicator = NULL;
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);
1548 ddprintk("mpoa: cleanup_module: next mpc is at %p\n", tmp);
1554 while (qos != NULL) {
1555 nextqos = qos->next;
1556 dprintk("mpoa: cleanup_module: freeing qos entry %p\n", qos);
1564 module_init(atm_mpoa_init);
1565 module_exit(atm_mpoa_cleanup);
1567 MODULE_LICENSE("GPL");