1 #include <linux/kernel.h>
2 #include <linux/string.h>
3 #include <linux/timer.h>
4 #include <linux/init.h>
5 #include <linux/bitops.h>
6 #include <linux/seq_file.h>
8 /* We are an ethernet device */
9 #include <linux/if_ether.h>
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
13 #include <linux/skbuff.h>
15 #include <asm/byteorder.h>
16 #include <asm/uaccess.h>
17 #include <net/checksum.h> /* for ip_fast_csum() */
20 #include <linux/proc_fs.h>
23 #include <linux/atmdev.h>
24 #include <linux/atmlec.h>
25 #include <linux/atmmpc.h>
27 #include <linux/config.h>
28 #include <linux/module.h>
32 #include "resources.h"
35 * mpc.c: Implementation of MPOA client kernel part
39 #define dprintk printk /* debug */
41 #define dprintk(format,args...)
45 #define ddprintk printk /* more debug */
47 #define ddprintk(format,args...)
52 #define MPOA_TAG_LEN 4
54 /* mpc_daemon -> kernel */
55 static void MPOA_trigger_rcvd (struct k_message *msg, struct mpoa_client *mpc);
56 static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc);
57 static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
58 static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
59 static void mps_death(struct k_message *msg, struct mpoa_client *mpc);
60 static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action);
61 static void MPOA_cache_impos_rcvd(struct k_message *msg, struct mpoa_client *mpc);
62 static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg, struct mpoa_client *mpc);
63 static void set_mps_mac_addr_rcvd(struct k_message *mesg, struct mpoa_client *mpc);
65 static uint8_t *copy_macs(struct mpoa_client *mpc, uint8_t *router_mac,
66 uint8_t *tlvs, uint8_t mps_macs, uint8_t device_type);
67 static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry);
69 static void send_set_mps_ctrl_addr(char *addr, struct mpoa_client *mpc);
70 static void mpoad_close(struct atm_vcc *vcc);
71 static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb);
73 static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb);
74 static int mpc_send_packet(struct sk_buff *skb, struct net_device *dev);
75 static int mpoa_event_listener(struct notifier_block *mpoa_notifier, unsigned long event, void *dev);
76 static void mpc_timer_refresh(void);
77 static void mpc_cache_check( unsigned long checking_time );
79 static struct llc_snap_hdr llc_snap_mpoa_ctrl = {
82 {0x00, 0x03} /* For MPOA control PDUs */
84 static struct llc_snap_hdr llc_snap_mpoa_data = {
87 {0x08, 0x00} /* This is for IP PDUs only */
89 static struct llc_snap_hdr llc_snap_mpoa_data_tagged = {
92 {0x88, 0x4c} /* This is for tagged data PDUs */
95 static struct notifier_block mpoa_notifier = {
101 #ifdef CONFIG_PROC_FS
102 extern int mpc_proc_init(void);
103 extern void mpc_proc_clean(void);
106 struct mpoa_client *mpcs = NULL; /* FIXME */
107 static struct atm_mpoa_qos *qos_head = NULL;
108 static struct timer_list mpc_timer = TIMER_INITIALIZER(NULL, 0, 0);
111 static struct mpoa_client *find_mpc_by_itfnum(int itf)
113 struct mpoa_client *mpc;
115 mpc = mpcs; /* our global linked list */
116 while (mpc != NULL) {
117 if (mpc->dev_num == itf)
122 return NULL; /* not found */
125 static struct mpoa_client *find_mpc_by_vcc(struct atm_vcc *vcc)
127 struct mpoa_client *mpc;
129 mpc = mpcs; /* our global linked list */
130 while (mpc != NULL) {
131 if (mpc->mpoad_vcc == vcc)
136 return NULL; /* not found */
139 static struct mpoa_client *find_mpc_by_lec(struct net_device *dev)
141 struct mpoa_client *mpc;
143 mpc = mpcs; /* our global linked list */
144 while (mpc != NULL) {
150 return NULL; /* not found */
154 * Functions for managing QoS list
158 * Overwrites the old entry or makes a new one.
160 struct atm_mpoa_qos *atm_mpoa_add_qos(uint32_t dst_ip, struct atm_qos *qos)
162 struct atm_mpoa_qos *entry;
164 entry = atm_mpoa_search_qos(dst_ip);
170 entry = kmalloc(sizeof(struct atm_mpoa_qos), GFP_KERNEL);
172 printk("mpoa: atm_mpoa_add_qos: out of memory\n");
176 entry->ipaddr = dst_ip;
179 entry->next = qos_head;
185 struct atm_mpoa_qos *atm_mpoa_search_qos(uint32_t dst_ip)
187 struct atm_mpoa_qos *qos;
190 while( qos != NULL ){
191 if(qos->ipaddr == dst_ip) {
201 * Returns 0 for failure
203 int atm_mpoa_delete_qos(struct atm_mpoa_qos *entry)
206 struct atm_mpoa_qos *curr;
208 if (entry == NULL) return 0;
209 if (entry == qos_head) {
210 qos_head = qos_head->next;
216 while (curr != NULL) {
217 if (curr->next == entry) {
218 curr->next = entry->next;
228 /* this is buggered - we need locking for qos_head */
229 void atm_mpoa_disp_qos(struct seq_file *m)
233 struct atm_mpoa_qos *qos;
236 seq_printf(m, "QoS entries for shortcuts:\n");
237 seq_printf(m, "IP address\n TX:max_pcr pcr min_pcr max_cdv max_sdu\n RX:max_pcr pcr min_pcr max_cdv max_sdu\n");
239 ipaddr[sizeof(ipaddr)-1] = '\0';
240 while (qos != NULL) {
241 ip = (unsigned char *)&qos->ipaddr;
242 sprintf(ipaddr, "%u.%u.%u.%u", NIPQUAD(ip));
243 seq_printf(m, "%u.%u.%u.%u\n %-7d %-7d %-7d %-7d %-7d\n %-7d %-7d %-7d %-7d %-7d\n",
245 qos->qos.txtp.max_pcr, qos->qos.txtp.pcr, qos->qos.txtp.min_pcr, qos->qos.txtp.max_cdv, qos->qos.txtp.max_sdu,
246 qos->qos.rxtp.max_pcr, qos->qos.rxtp.pcr, qos->qos.rxtp.min_pcr, qos->qos.rxtp.max_cdv, qos->qos.rxtp.max_sdu);
251 static struct net_device *find_lec_by_itfnum(int itf)
253 struct net_device *dev;
256 sprintf(name, "lec%d", itf);
257 dev = dev_get_by_name(name);
262 static struct mpoa_client *alloc_mpc(void)
264 struct mpoa_client *mpc;
266 mpc = kmalloc(sizeof (struct mpoa_client), GFP_KERNEL);
269 memset(mpc, 0, sizeof(struct mpoa_client));
270 mpc->ingress_lock = RW_LOCK_UNLOCKED;
271 mpc->egress_lock = RW_LOCK_UNLOCKED;
273 atm_mpoa_init_cache(mpc);
275 mpc->parameters.mpc_p1 = MPC_P1;
276 mpc->parameters.mpc_p2 = MPC_P2;
277 memset(mpc->parameters.mpc_p3,0,sizeof(mpc->parameters.mpc_p3));
278 mpc->parameters.mpc_p4 = MPC_P4;
279 mpc->parameters.mpc_p5 = MPC_P5;
280 mpc->parameters.mpc_p6 = MPC_P6;
289 * start_mpc() puts the MPC on line. All the packets destined
290 * to the lec underneath us are now being monitored and
291 * shortcuts will be established.
294 static void start_mpc(struct mpoa_client *mpc, struct net_device *dev)
297 dprintk("mpoa: (%s) start_mpc:\n", mpc->dev->name);
298 if (dev->hard_start_xmit == NULL) {
299 printk("mpoa: (%s) start_mpc: dev->hard_start_xmit == NULL, not starting\n",
303 mpc->old_hard_start_xmit = dev->hard_start_xmit;
304 dev->hard_start_xmit = mpc_send_packet;
309 static void stop_mpc(struct mpoa_client *mpc)
312 dprintk("mpoa: (%s) stop_mpc:", mpc->dev->name);
314 /* Lets not nullify lec device's dev->hard_start_xmit */
315 if (mpc->dev->hard_start_xmit != mpc_send_packet) {
316 dprintk(" mpc already stopped, not fatal\n");
320 mpc->dev->hard_start_xmit = mpc->old_hard_start_xmit;
321 mpc->old_hard_start_xmit = NULL;
322 /* close_shortcuts(mpc); ??? FIXME */
327 static const char *mpoa_device_type_string(char type) __attribute__ ((unused));
329 static const char *mpoa_device_type_string(char type)
333 return "non-MPOA device";
342 return "both MPS and MPC";
345 return "unspecified (non-MPOA) device";
349 return ""; /* not reached */
353 * lec device calls this via its dev->priv->lane2_ops->associate_indicator()
354 * 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, uint8_t *mac_addr,
366 uint8_t *tlvs, uint32_t sizeoftlvs)
369 uint8_t length, mpoa_device_type, number_of_mps_macs;
370 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 printk("mpoa: (%s) lane2_assoc_ind: 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) | (tlvs[2] << 8) | tlvs[3];
386 dprintk(" type 0x%x length %02x\n", type, length);
387 if (tlvs + length > end_of_tlvs) {
388 printk("TLV value extends past its buffer, aborting parse\n");
393 printk("mpoa: (%s) lane2_assoc_ind: TLV type was 0, returning\n", dev->name);
397 if (type != TLV_MPOA_DEVICE_TYPE) {
399 continue; /* skip other TLVs */
401 mpoa_device_type = *tlvs++;
402 number_of_mps_macs = *tlvs++;
403 dprintk("mpoa: (%s) MPOA device type '%s', ", dev->name, mpoa_device_type_string(mpoa_device_type));
404 if (mpoa_device_type == MPS_AND_MPC &&
405 length < (42 + number_of_mps_macs*ETH_ALEN)) { /* :) */
406 printk("\nmpoa: (%s) lane2_assoc_ind: short MPOA Device Type TLV\n",
410 if ((mpoa_device_type == MPS || mpoa_device_type == MPC)
411 && length < 22 + number_of_mps_macs*ETH_ALEN) {
412 printk("\nmpoa: (%s) lane2_assoc_ind: short MPOA Device Type TLV\n",
416 if (mpoa_device_type != MPS && mpoa_device_type != MPS_AND_MPC) {
417 dprintk("ignoring non-MPS device\n");
418 if (mpoa_device_type == MPC) tlvs += 20;
419 continue; /* we are only interested in MPSs */
421 if (number_of_mps_macs == 0 && mpoa_device_type == MPS_AND_MPC) {
422 printk("\nmpoa: (%s) lane2_assoc_ind: MPS_AND_MPC has zero MACs\n", dev->name);
423 continue; /* someone should read the spec */
425 dprintk("this MPS has %d MAC addresses\n", number_of_mps_macs);
427 /* ok, now we can go and tell our daemon the control address of MPS */
428 send_set_mps_ctrl_addr(tlvs, mpc);
430 tlvs = copy_macs(mpc, mac_addr, tlvs, number_of_mps_macs, mpoa_device_type);
431 if (tlvs == NULL) return;
433 if (end_of_tlvs - tlvs != 0)
434 printk("mpoa: (%s) lane2_assoc_ind: ignoring %Zd bytes of trailing TLV carbage\n",
435 dev->name, end_of_tlvs - tlvs);
440 * Store at least advertizing router's MAC address
441 * plus the possible MAC address(es) to mpc->mps_macs.
442 * For a freshly allocated MPOA client mpc->mps_macs == 0.
444 static uint8_t *copy_macs(struct mpoa_client *mpc, uint8_t *router_mac,
445 uint8_t *tlvs, uint8_t mps_macs, uint8_t device_type)
448 num_macs = (mps_macs > 1) ? mps_macs : 1;
450 if (mpc->number_of_mps_macs != num_macs) { /* need to reallocate? */
451 if (mpc->number_of_mps_macs != 0) kfree(mpc->mps_macs);
452 mpc->number_of_mps_macs = 0;
453 mpc->mps_macs = kmalloc(num_macs*ETH_ALEN, GFP_KERNEL);
454 if (mpc->mps_macs == NULL) {
455 printk("mpoa: (%s) copy_macs: out of mem\n", mpc->dev->name);
459 memcpy(mpc->mps_macs, router_mac, ETH_ALEN);
460 tlvs += 20; if (device_type == MPS_AND_MPC) tlvs += 20;
462 memcpy(mpc->mps_macs, tlvs, mps_macs*ETH_ALEN);
463 tlvs += mps_macs*ETH_ALEN;
464 mpc->number_of_mps_macs = num_macs;
469 static int send_via_shortcut(struct sk_buff *skb, struct mpoa_client *mpc)
471 in_cache_entry *entry;
477 struct llc_snap_hdr hdr;
479 } tagged_llc_snap_hdr = {
480 {0xaa, 0xaa, 0x03, {0x00, 0x00, 0x00}, {0x88, 0x4c}},
484 buff = skb->data + mpc->dev->hard_header_len;
485 iph = (struct iphdr *)buff;
488 ddprintk("mpoa: (%s) send_via_shortcut: ipaddr 0x%x\n", mpc->dev->name, ipaddr);
490 entry = mpc->in_ops->get(ipaddr, mpc);
492 entry = mpc->in_ops->add_entry(ipaddr, mpc);
493 if (entry != NULL) mpc->in_ops->put(entry);
496 if (mpc->in_ops->cache_hit(entry, mpc) != OPEN){ /* threshold not exceeded or VCC not ready */
497 ddprintk("mpoa: (%s) send_via_shortcut: cache_hit: returns != OPEN\n", mpc->dev->name);
498 mpc->in_ops->put(entry);
502 ddprintk("mpoa: (%s) send_via_shortcut: using shortcut\n", mpc->dev->name);
503 /* MPOA spec A.1.4, MPOA client must decrement IP ttl at least by one */
505 ddprintk("mpoa: (%s) send_via_shortcut: IP ttl = %u, using LANE\n", mpc->dev->name, iph->ttl);
506 mpc->in_ops->put(entry);
511 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
513 if (entry->ctrl_info.tag != 0) {
514 ddprintk("mpoa: (%s) send_via_shortcut: adding tag 0x%x\n", mpc->dev->name, entry->ctrl_info.tag);
515 tagged_llc_snap_hdr.tag = entry->ctrl_info.tag;
516 skb_pull(skb, ETH_HLEN); /* get rid of Eth header */
517 skb_push(skb, sizeof(tagged_llc_snap_hdr)); /* add LLC/SNAP header */
518 memcpy(skb->data, &tagged_llc_snap_hdr, sizeof(tagged_llc_snap_hdr));
520 skb_pull(skb, ETH_HLEN); /* get rid of Eth header */
521 skb_push(skb, sizeof(struct llc_snap_hdr)); /* add LLC/SNAP header + tag */
522 memcpy(skb->data, &llc_snap_mpoa_data, sizeof(struct llc_snap_hdr));
525 atomic_add(skb->truesize, &entry->shortcut->sk->sk_wmem_alloc);
526 ATM_SKB(skb)->atm_options = entry->shortcut->atm_options;
527 entry->shortcut->send(entry->shortcut, skb);
528 entry->packets_fwded++;
529 mpc->in_ops->put(entry);
535 * Probably needs some error checks and locking, not sure...
537 static int mpc_send_packet(struct sk_buff *skb, struct net_device *dev)
540 struct mpoa_client *mpc;
544 mpc = find_mpc_by_lec(dev); /* this should NEVER fail */
546 printk("mpoa: (%s) mpc_send_packet: no MPC found\n", dev->name);
550 eth = (struct ethhdr *)skb->data;
551 if (eth->h_proto != htons(ETH_P_IP))
552 goto non_ip; /* Multi-Protocol Over ATM :-) */
554 while (i < mpc->number_of_mps_macs) {
555 if (memcmp(eth->h_dest, (mpc->mps_macs + i*ETH_ALEN), ETH_ALEN) == 0)
556 if ( send_via_shortcut(skb, mpc) == 0 ) /* try shortcut */
557 return 0; /* success! */
562 retval = mpc->old_hard_start_xmit(skb,dev);
567 int atm_mpoa_vcc_attach(struct atm_vcc *vcc, void __user *arg)
570 struct mpoa_client *mpc;
571 struct atmmpc_ioc ioc_data;
572 in_cache_entry *in_entry;
576 bytes_left = copy_from_user(&ioc_data, arg, sizeof(struct atmmpc_ioc));
577 if (bytes_left != 0) {
578 printk("mpoa: mpc_vcc_attach: Short read (missed %d bytes) from userland\n", bytes_left);
581 ipaddr = ioc_data.ipaddr;
582 if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF)
585 mpc = find_mpc_by_itfnum(ioc_data.dev_num);
589 if (ioc_data.type == MPC_SOCKET_INGRESS) {
590 in_entry = mpc->in_ops->get(ipaddr, mpc);
591 if (in_entry == NULL || in_entry->entry_state < INGRESS_RESOLVED) {
592 printk("mpoa: (%s) mpc_vcc_attach: did not find RESOLVED entry from ingress cache\n",
594 if (in_entry != NULL) mpc->in_ops->put(in_entry);
597 ip = (unsigned char*)&in_entry->ctrl_info.in_dst_ip;
598 printk("mpoa: (%s) mpc_vcc_attach: attaching ingress SVC, entry = %u.%u.%u.%u\n",
599 mpc->dev->name, ip[0], ip[1], ip[2], ip[3]);
600 in_entry->shortcut = vcc;
601 mpc->in_ops->put(in_entry);
603 printk("mpoa: (%s) mpc_vcc_attach: attaching egress SVC\n", mpc->dev->name);
606 vcc->proto_data = mpc->dev;
607 vcc->push = mpc_push;
615 static void mpc_vcc_close(struct atm_vcc *vcc, struct net_device *dev)
617 struct mpoa_client *mpc;
618 in_cache_entry *in_entry;
619 eg_cache_entry *eg_entry;
621 mpc = find_mpc_by_lec(dev);
623 printk("mpoa: (%s) mpc_vcc_close: close for unknown MPC\n", dev->name);
627 dprintk("mpoa: (%s) mpc_vcc_close:\n", dev->name);
628 in_entry = mpc->in_ops->get_by_vcc(vcc, mpc);
630 unsigned char *ip __attribute__ ((unused)) =
631 (unsigned char *)&in_entry->ctrl_info.in_dst_ip;
632 dprintk("mpoa: (%s) mpc_vcc_close: ingress SVC closed ip = %u.%u.%u.%u\n",
633 mpc->dev->name, ip[0], ip[1], ip[2], ip[3]);
634 in_entry->shortcut = NULL;
635 mpc->in_ops->put(in_entry);
637 eg_entry = mpc->eg_ops->get_by_vcc(vcc, mpc);
639 dprintk("mpoa: (%s) mpc_vcc_close: egress SVC closed\n", mpc->dev->name);
640 eg_entry->shortcut = NULL;
641 mpc->eg_ops->put(eg_entry);
644 if (in_entry == NULL && eg_entry == NULL)
645 dprintk("mpoa: (%s) mpc_vcc_close: unused vcc closed\n", dev->name);
650 static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb)
652 struct net_device *dev = (struct net_device *)vcc->proto_data;
653 struct sk_buff *new_skb;
655 struct mpoa_client *mpc;
659 ddprintk("mpoa: (%s) mpc_push:\n", dev->name);
661 dprintk("mpoa: (%s) mpc_push: null skb, closing VCC\n", dev->name);
662 mpc_vcc_close(vcc, dev);
667 if (memcmp(skb->data, &llc_snap_mpoa_ctrl, sizeof(struct llc_snap_hdr)) == 0) {
668 dprintk("mpoa: (%s) mpc_push: control packet arrived\n", dev->name);
669 /* Pass control packets to daemon */
670 skb_queue_tail(&vcc->sk->sk_receive_queue, skb);
671 vcc->sk->sk_data_ready(vcc->sk, skb->len);
675 /* data coming over the shortcut */
676 atm_return(vcc, skb->truesize);
678 mpc = find_mpc_by_lec(dev);
680 printk("mpoa: (%s) mpc_push: unknown MPC\n", dev->name);
684 if (memcmp(skb->data, &llc_snap_mpoa_data_tagged, sizeof(struct llc_snap_hdr)) == 0) { /* MPOA tagged data */
685 ddprintk("mpoa: (%s) mpc_push: tagged data packet arrived\n", dev->name);
687 } else if (memcmp(skb->data, &llc_snap_mpoa_data, sizeof(struct llc_snap_hdr)) == 0) { /* MPOA data */
688 printk("mpoa: (%s) mpc_push: non-tagged data packet arrived\n", dev->name);
689 printk(" mpc_push: non-tagged data unsupported, purging\n");
690 dev_kfree_skb_any(skb);
693 printk("mpoa: (%s) mpc_push: garbage arrived, purging\n", dev->name);
694 dev_kfree_skb_any(skb);
698 tmp = skb->data + sizeof(struct llc_snap_hdr);
699 tag = *(uint32_t *)tmp;
701 eg = mpc->eg_ops->get_by_tag(tag, mpc);
703 printk("mpoa: (%s) mpc_push: Didn't find egress cache entry, tag = %u\n",
705 purge_egress_shortcut(vcc, NULL);
706 dev_kfree_skb_any(skb);
711 * See if ingress MPC is using shortcut we opened as a return channel.
712 * This means we have a bi-directional vcc opened by us.
714 if (eg->shortcut == NULL) {
716 printk("mpoa: (%s) mpc_push: egress SVC in use\n", dev->name);
719 skb_pull(skb, sizeof(struct llc_snap_hdr) + sizeof(tag)); /* get rid of LLC/SNAP header */
720 new_skb = skb_realloc_headroom(skb, eg->ctrl_info.DH_length); /* LLC/SNAP is shorter than MAC header :( */
721 dev_kfree_skb_any(skb);
722 if (new_skb == NULL){
723 mpc->eg_ops->put(eg);
726 skb_push(new_skb, eg->ctrl_info.DH_length); /* add MAC header */
727 memcpy(new_skb->data, eg->ctrl_info.DLL_header, eg->ctrl_info.DH_length);
728 new_skb->protocol = eth_type_trans(new_skb, dev);
729 new_skb->nh.raw = new_skb->data;
731 eg->latest_ip_addr = new_skb->nh.iph->saddr;
733 mpc->eg_ops->put(eg);
735 memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
741 static struct atmdev_ops mpc_ops = { /* only send is required */
742 .close = mpoad_close,
743 .send = msg_from_mpoad
746 static struct atm_dev mpc_dev = {
750 .lock = SPIN_LOCK_UNLOCKED
751 /* members not explicitly initialised will be 0 */
754 int atm_mpoa_mpoad_attach (struct atm_vcc *vcc, int arg)
756 struct mpoa_client *mpc;
757 struct lec_priv *priv;
760 init_timer(&mpc_timer);
763 /* This lets us now how our LECs are doing */
764 register_netdevice_notifier(&mpoa_notifier);
767 mpc = find_mpc_by_itfnum(arg);
769 dprintk("mpoa: mpoad_attach: allocating new mpc for itf %d\n", arg);
774 mpc->dev = find_lec_by_itfnum(arg); /* NULL if there was no lec */
776 if (mpc->mpoad_vcc) {
777 printk("mpoa: mpoad_attach: mpoad is already present for itf %d\n", arg);
781 if (mpc->dev) { /* check if the lec is LANE2 capable */
782 priv = (struct lec_priv *)mpc->dev->priv;
783 if (priv->lane_version < 2) {
787 priv->lane2_ops->associate_indicator = lane2_assoc_ind;
790 mpc->mpoad_vcc = vcc;
792 vcc_insert_socket(vcc->sk);
793 set_bit(ATM_VF_META,&vcc->flags);
794 set_bit(ATM_VF_READY,&vcc->flags);
797 char empty[ATM_ESA_LEN];
798 memset(empty, 0, ATM_ESA_LEN);
800 start_mpc(mpc, mpc->dev);
801 /* set address if mpcd e.g. gets killed and restarted.
802 * If we do not do it now we have to wait for the next LE_ARP
804 if ( memcmp(mpc->mps_ctrl_addr, empty, ATM_ESA_LEN) != 0 )
805 send_set_mps_ctrl_addr(mpc->mps_ctrl_addr, mpc);
808 __module_get(THIS_MODULE);
812 static void send_set_mps_ctrl_addr(char *addr, struct mpoa_client *mpc)
814 struct k_message mesg;
816 memcpy (mpc->mps_ctrl_addr, addr, ATM_ESA_LEN);
818 mesg.type = SET_MPS_CTRL_ADDR;
819 memcpy(mesg.MPS_ctrl, addr, ATM_ESA_LEN);
820 msg_to_mpoad(&mesg, mpc);
825 static void mpoad_close(struct atm_vcc *vcc)
827 struct mpoa_client *mpc;
830 mpc = find_mpc_by_vcc(vcc);
832 printk("mpoa: mpoad_close: did not find MPC\n");
835 if (!mpc->mpoad_vcc) {
836 printk("mpoa: mpoad_close: close for non-present mpoad\n");
840 mpc->mpoad_vcc = NULL;
842 struct lec_priv *priv = (struct lec_priv *)mpc->dev->priv;
843 priv->lane2_ops->associate_indicator = NULL;
848 mpc->in_ops->destroy_cache(mpc);
849 mpc->eg_ops->destroy_cache(mpc);
851 while ((skb = skb_dequeue(&vcc->sk->sk_receive_queue))) {
852 atm_return(vcc, skb->truesize);
856 printk("mpoa: (%s) going down\n",
857 (mpc->dev) ? mpc->dev->name : "<unknown>");
858 module_put(THIS_MODULE);
866 static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb)
869 struct mpoa_client *mpc = find_mpc_by_vcc(vcc);
870 struct k_message *mesg = (struct k_message*)skb->data;
871 atomic_sub(skb->truesize, &vcc->sk->sk_wmem_alloc);
874 printk("mpoa: msg_from_mpoad: no mpc found\n");
877 dprintk("mpoa: (%s) msg_from_mpoad:", (mpc->dev) ? mpc->dev->name : "<unknown>");
879 case MPOA_RES_REPLY_RCVD:
880 dprintk(" mpoa_res_reply_rcvd\n");
881 MPOA_res_reply_rcvd(mesg, mpc);
883 case MPOA_TRIGGER_RCVD:
884 dprintk(" mpoa_trigger_rcvd\n");
885 MPOA_trigger_rcvd(mesg, mpc);
887 case INGRESS_PURGE_RCVD:
888 dprintk(" nhrp_purge_rcvd\n");
889 ingress_purge_rcvd(mesg, mpc);
891 case EGRESS_PURGE_RCVD:
892 dprintk(" egress_purge_reply_rcvd\n");
893 egress_purge_rcvd(mesg, mpc);
896 dprintk(" mps_death\n");
897 mps_death(mesg, mpc);
899 case CACHE_IMPOS_RCVD:
900 dprintk(" cache_impos_rcvd\n");
901 MPOA_cache_impos_rcvd(mesg, mpc);
903 case SET_MPC_CTRL_ADDR:
904 dprintk(" set_mpc_ctrl_addr\n");
905 set_mpc_ctrl_addr_rcvd(mesg, mpc);
907 case SET_MPS_MAC_ADDR:
908 dprintk(" set_mps_mac_addr\n");
909 set_mps_mac_addr_rcvd(mesg, mpc);
911 case CLEAN_UP_AND_EXIT:
912 dprintk(" clean_up_and_exit\n");
913 clean_up(mesg, mpc, DIE);
916 dprintk(" reload\n");
917 clean_up(mesg, mpc, RELOAD);
920 dprintk(" set_mpc_params\n");
921 mpc->parameters = mesg->content.params;
924 dprintk(" unknown message %d\n", mesg->type);
932 /* Remember that this function may not do things that sleep */
933 int msg_to_mpoad(struct k_message *mesg, struct mpoa_client *mpc)
937 if (mpc == NULL || !mpc->mpoad_vcc) {
938 printk("mpoa: msg_to_mpoad: mesg %d to a non-existent mpoad\n", mesg->type);
942 skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
945 skb_put(skb, sizeof(struct k_message));
946 memcpy(skb->data, mesg, sizeof(struct k_message));
947 atm_force_charge(mpc->mpoad_vcc, skb->truesize);
948 skb_queue_tail(&mpc->mpoad_vcc->sk->sk_receive_queue, skb);
949 mpc->mpoad_vcc->sk->sk_data_ready(mpc->mpoad_vcc->sk, skb->len);
954 static int mpoa_event_listener(struct notifier_block *mpoa_notifier, unsigned long event, void *dev_ptr)
956 struct net_device *dev;
957 struct mpoa_client *mpc;
958 struct lec_priv *priv;
960 dev = (struct net_device *)dev_ptr;
961 if (dev->name == NULL || strncmp(dev->name, "lec", 3))
962 return NOTIFY_DONE; /* we are only interested in lec:s */
965 case NETDEV_REGISTER: /* a new lec device was allocated */
966 priv = (struct lec_priv *)dev->priv;
967 if (priv->lane_version < 2)
969 priv->lane2_ops->associate_indicator = lane2_assoc_ind;
970 mpc = find_mpc_by_itfnum(priv->itfnum);
972 dprintk("mpoa: mpoa_event_listener: allocating new mpc for %s\n",
976 printk("mpoa: mpoa_event_listener: no new mpc");
980 mpc->dev_num = priv->itfnum;
983 dprintk("mpoa: (%s) was initialized\n", dev->name);
985 case NETDEV_UNREGISTER:
986 /* the lec device was deallocated */
987 mpc = find_mpc_by_lec(dev);
990 dprintk("mpoa: device (%s) was deallocated\n", dev->name);
996 /* the dev was ifconfig'ed up */
997 mpc = find_mpc_by_lec(dev);
1000 if (mpc->mpoad_vcc != NULL) {
1001 start_mpc(mpc, dev);
1005 /* the dev was ifconfig'ed down */
1006 /* this means that the flow of packets from the
1009 mpc = find_mpc_by_lec(dev);
1012 if (mpc->mpoad_vcc != NULL) {
1018 case NETDEV_CHANGEMTU:
1019 case NETDEV_CHANGEADDR:
1020 case NETDEV_GOING_DOWN:
1030 * Functions which are called after a message is received from mpcd.
1031 * Msg is reused on purpose.
1035 static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1037 uint32_t dst_ip = msg->content.in_info.in_dst_ip;
1038 in_cache_entry *entry;
1040 entry = mpc->in_ops->get(dst_ip, mpc);
1042 entry = mpc->in_ops->add_entry(dst_ip, mpc);
1043 entry->entry_state = INGRESS_RESOLVING;
1044 msg->type = SND_MPOA_RES_RQST;
1045 msg->content.in_info = entry->ctrl_info;
1046 msg_to_mpoad(msg, mpc);
1047 do_gettimeofday(&(entry->reply_wait));
1048 mpc->in_ops->put(entry);
1052 if(entry->entry_state == INGRESS_INVALID){
1053 entry->entry_state = INGRESS_RESOLVING;
1054 msg->type = SND_MPOA_RES_RQST;
1055 msg->content.in_info = entry->ctrl_info;
1056 msg_to_mpoad(msg, mpc);
1057 do_gettimeofday(&(entry->reply_wait));
1058 mpc->in_ops->put(entry);
1062 printk("mpoa: (%s) MPOA_trigger_rcvd: entry already in resolving state\n",
1063 (mpc->dev) ? mpc->dev->name : "<unknown>");
1064 mpc->in_ops->put(entry);
1069 * Things get complicated because we have to check if there's an egress
1070 * shortcut with suitable traffic parameters we could use.
1072 static void check_qos_and_open_shortcut(struct k_message *msg, struct mpoa_client *client, in_cache_entry *entry)
1074 uint32_t dst_ip = msg->content.in_info.in_dst_ip;
1075 unsigned char *ip __attribute__ ((unused)) = (unsigned char *)&dst_ip;
1076 struct atm_mpoa_qos *qos = atm_mpoa_search_qos(dst_ip);
1077 eg_cache_entry *eg_entry = client->eg_ops->get_by_src_ip(dst_ip, client);
1079 if(eg_entry && eg_entry->shortcut){
1080 if(eg_entry->shortcut->qos.txtp.traffic_class &
1081 msg->qos.txtp.traffic_class &
1082 (qos ? qos->qos.txtp.traffic_class : ATM_UBR | ATM_CBR)){
1083 if(eg_entry->shortcut->qos.txtp.traffic_class == ATM_UBR)
1084 entry->shortcut = eg_entry->shortcut;
1085 else if(eg_entry->shortcut->qos.txtp.max_pcr > 0)
1086 entry->shortcut = eg_entry->shortcut;
1088 if(entry->shortcut){
1089 dprintk("mpoa: (%s) using egress SVC to reach %u.%u.%u.%u\n",client->dev->name, NIPQUAD(ip));
1090 client->eg_ops->put(eg_entry);
1094 if (eg_entry != NULL)
1095 client->eg_ops->put(eg_entry);
1097 /* No luck in the egress cache we must open an ingress SVC */
1098 msg->type = OPEN_INGRESS_SVC;
1099 if (qos && (qos->qos.txtp.traffic_class == msg->qos.txtp.traffic_class))
1101 msg->qos = qos->qos;
1102 printk("mpoa: (%s) trying to get a CBR shortcut\n",client->dev->name);
1104 else memset(&msg->qos,0,sizeof(struct atm_qos));
1105 msg_to_mpoad(msg, client);
1109 static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1113 uint32_t dst_ip = msg->content.in_info.in_dst_ip;
1114 in_cache_entry *entry = mpc->in_ops->get(dst_ip, mpc);
1115 ip = (unsigned char *)&dst_ip;
1116 dprintk("mpoa: (%s) MPOA_res_reply_rcvd: ip %u.%u.%u.%u\n", mpc->dev->name, NIPQUAD(ip));
1117 ddprintk("mpoa: (%s) MPOA_res_reply_rcvd() entry = %p", mpc->dev->name, entry);
1119 printk("\nmpoa: (%s) ARGH, received res. reply for an entry that doesn't exist.\n", mpc->dev->name);
1122 ddprintk(" entry_state = %d ", entry->entry_state);
1124 if (entry->entry_state == INGRESS_RESOLVED) {
1125 printk("\nmpoa: (%s) MPOA_res_reply_rcvd for RESOLVED entry!\n", mpc->dev->name);
1126 mpc->in_ops->put(entry);
1130 entry->ctrl_info = msg->content.in_info;
1131 do_gettimeofday(&(entry->tv));
1132 do_gettimeofday(&(entry->reply_wait)); /* Used in refreshing func from now on */
1133 entry->refresh_time = 0;
1134 ddprintk("entry->shortcut = %p\n", entry->shortcut);
1136 if(entry->entry_state == INGRESS_RESOLVING && entry->shortcut != NULL){
1137 entry->entry_state = INGRESS_RESOLVED;
1138 mpc->in_ops->put(entry);
1139 return; /* Shortcut already open... */
1142 if (entry->shortcut != NULL) {
1143 printk("mpoa: (%s) MPOA_res_reply_rcvd: entry->shortcut != NULL, impossible!\n",
1145 mpc->in_ops->put(entry);
1149 check_qos_and_open_shortcut(msg, mpc, entry);
1150 entry->entry_state = INGRESS_RESOLVED;
1151 mpc->in_ops->put(entry);
1157 static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1159 uint32_t dst_ip = msg->content.in_info.in_dst_ip;
1160 uint32_t mask = msg->ip_mask;
1161 unsigned char *ip = (unsigned char *)&dst_ip;
1162 in_cache_entry *entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1165 printk("mpoa: (%s) ingress_purge_rcvd: purge for a non-existing entry, ", mpc->dev->name);
1166 printk("ip = %u.%u.%u.%u\n", ip[0], ip[1], ip[2], ip[3]);
1171 dprintk("mpoa: (%s) ingress_purge_rcvd: removing an ingress entry, ip = %u.%u.%u.%u\n" ,
1172 mpc->dev->name, ip[0], ip[1], ip[2], ip[3]);
1173 write_lock_bh(&mpc->ingress_lock);
1174 mpc->in_ops->remove_entry(entry, mpc);
1175 write_unlock_bh(&mpc->ingress_lock);
1176 mpc->in_ops->put(entry);
1177 entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1178 } while (entry != NULL);
1183 static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1185 uint32_t cache_id = msg->content.eg_info.cache_id;
1186 eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(cache_id, mpc);
1188 if (entry == NULL) {
1189 dprintk("mpoa: (%s) egress_purge_rcvd: purge for a non-existing entry\n", mpc->dev->name);
1193 write_lock_irq(&mpc->egress_lock);
1194 mpc->eg_ops->remove_entry(entry, mpc);
1195 write_unlock_irq(&mpc->egress_lock);
1197 mpc->eg_ops->put(entry);
1202 static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry)
1204 struct k_message *purge_msg;
1205 struct sk_buff *skb;
1207 dprintk("mpoa: purge_egress_shortcut: entering\n");
1209 printk("mpoa: purge_egress_shortcut: vcc == NULL\n");
1213 skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
1215 printk("mpoa: purge_egress_shortcut: out of memory\n");
1219 skb_put(skb, sizeof(struct k_message));
1220 memset(skb->data, 0, sizeof(struct k_message));
1221 purge_msg = (struct k_message *)skb->data;
1222 purge_msg->type = DATA_PLANE_PURGE;
1224 purge_msg->content.eg_info = entry->ctrl_info;
1226 atm_force_charge(vcc, skb->truesize);
1227 skb_queue_tail(&vcc->sk->sk_receive_queue, skb);
1228 vcc->sk->sk_data_ready(vcc->sk, skb->len);
1229 dprintk("mpoa: purge_egress_shortcut: exiting:\n");
1235 * Our MPS died. Tell our daemon to send NHRP data plane purge to each
1236 * of the egress shortcuts we have.
1238 static void mps_death( struct k_message * msg, struct mpoa_client * mpc )
1240 eg_cache_entry *entry;
1242 dprintk("mpoa: (%s) mps_death:\n", mpc->dev->name);
1244 if(memcmp(msg->MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN)){
1245 printk("mpoa: (%s) mps_death: wrong MPS\n", mpc->dev->name);
1249 /* FIXME: This knows too much of the cache structure */
1250 read_lock_irq(&mpc->egress_lock);
1251 entry = mpc->eg_cache;
1252 while (entry != NULL) {
1253 purge_egress_shortcut(entry->shortcut, entry);
1254 entry = entry->next;
1256 read_unlock_irq(&mpc->egress_lock);
1258 mpc->in_ops->destroy_cache(mpc);
1259 mpc->eg_ops->destroy_cache(mpc);
1264 static void MPOA_cache_impos_rcvd( struct k_message * msg, struct mpoa_client * mpc)
1266 uint16_t holding_time;
1267 eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(msg->content.eg_info.cache_id, mpc);
1269 holding_time = msg->content.eg_info.holding_time;
1270 dprintk("mpoa: (%s) MPOA_cache_impos_rcvd: entry = %p, holding_time = %u\n",
1271 mpc->dev->name, entry, holding_time);
1272 if(entry == NULL && holding_time) {
1273 entry = mpc->eg_ops->add_entry(msg, mpc);
1274 mpc->eg_ops->put(entry);
1278 mpc->eg_ops->update(entry, holding_time);
1282 write_lock_irq(&mpc->egress_lock);
1283 mpc->eg_ops->remove_entry(entry, mpc);
1284 write_unlock_irq(&mpc->egress_lock);
1286 mpc->eg_ops->put(entry);
1291 static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg, struct mpoa_client *mpc)
1293 struct lec_priv *priv;
1296 uint8_t tlv[4 + 1 + 1 + 1 + ATM_ESA_LEN];
1298 tlv[0] = 00; tlv[1] = 0xa0; tlv[2] = 0x3e; tlv[3] = 0x2a; /* type */
1299 tlv[4] = 1 + 1 + ATM_ESA_LEN; /* length */
1300 tlv[5] = 0x02; /* MPOA client */
1301 tlv[6] = 0x00; /* number of MPS MAC addresses */
1303 memcpy(&tlv[7], mesg->MPS_ctrl, ATM_ESA_LEN); /* MPC ctrl ATM addr */
1304 memcpy(mpc->our_ctrl_addr, mesg->MPS_ctrl, ATM_ESA_LEN);
1306 dprintk("mpoa: (%s) setting MPC ctrl ATM address to ",
1307 (mpc->dev) ? mpc->dev->name : "<unknown>");
1308 for (i = 7; i < sizeof(tlv); i++)
1309 dprintk("%02x ", tlv[i]);
1313 priv = (struct lec_priv *)mpc->dev->priv;
1314 retval = priv->lane2_ops->associate_req(mpc->dev, mpc->dev->dev_addr, tlv, sizeof(tlv));
1316 printk("mpoa: (%s) MPOA device type TLV association failed\n", mpc->dev->name);
1317 retval = priv->lane2_ops->resolve(mpc->dev, NULL, 1, NULL, NULL);
1319 printk("mpoa: (%s) targetless LE_ARP request failed\n", mpc->dev->name);
1325 static void set_mps_mac_addr_rcvd(struct k_message *msg, struct mpoa_client *client)
1328 if(client->number_of_mps_macs)
1329 kfree(client->mps_macs);
1330 client->number_of_mps_macs = 0;
1331 client->mps_macs = kmalloc(ETH_ALEN,GFP_KERNEL);
1332 if (client->mps_macs == NULL) {
1333 printk("mpoa: set_mps_mac_addr_rcvd: out of memory\n");
1336 client->number_of_mps_macs = 1;
1337 memcpy(client->mps_macs, msg->MPS_ctrl, ETH_ALEN);
1343 * purge egress cache and tell daemon to 'action' (DIE, RELOAD)
1345 static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action)
1348 eg_cache_entry *entry;
1349 msg->type = SND_EGRESS_PURGE;
1352 /* FIXME: This knows too much of the cache structure */
1353 read_lock_irq(&mpc->egress_lock);
1354 entry = mpc->eg_cache;
1355 while (entry != NULL){
1356 msg->content.eg_info = entry->ctrl_info;
1357 dprintk("mpoa: cache_id %u\n", entry->ctrl_info.cache_id);
1358 msg_to_mpoad(msg, mpc);
1359 entry = entry->next;
1361 read_unlock_irq(&mpc->egress_lock);
1364 msg_to_mpoad(msg, mpc);
1368 static void mpc_timer_refresh(void)
1370 mpc_timer.expires = jiffies + (MPC_P2 * HZ);
1371 mpc_timer.data = mpc_timer.expires;
1372 mpc_timer.function = mpc_cache_check;
1373 add_timer(&mpc_timer);
1378 static void mpc_cache_check( unsigned long checking_time )
1380 struct mpoa_client *mpc = mpcs;
1381 static unsigned long previous_resolving_check_time;
1382 static unsigned long previous_refresh_time;
1384 while( mpc != NULL ){
1385 mpc->in_ops->clear_count(mpc);
1386 mpc->eg_ops->clear_expired(mpc);
1387 if(checking_time - previous_resolving_check_time > mpc->parameters.mpc_p4 * HZ ){
1388 mpc->in_ops->check_resolving(mpc);
1389 previous_resolving_check_time = checking_time;
1391 if(checking_time - previous_refresh_time > mpc->parameters.mpc_p5 * HZ ){
1392 mpc->in_ops->refresh(mpc);
1393 previous_refresh_time = checking_time;
1397 mpc_timer_refresh();
1402 static int atm_mpoa_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1405 struct atm_vcc *vcc = ATM_SD(sock);
1407 if (cmd != ATMMPC_CTRL && cmd != ATMMPC_DATA)
1408 return -ENOIOCTLCMD;
1410 if (!capable(CAP_NET_ADMIN))
1415 err = atm_mpoa_mpoad_attach(vcc, (int)arg);
1417 sock->state = SS_CONNECTED;
1420 err = atm_mpoa_vcc_attach(vcc, (void __user *)arg);
1429 static struct atm_ioctl atm_ioctl_ops = {
1430 .owner = THIS_MODULE,
1431 .ioctl = atm_mpoa_ioctl,
1434 static __init int atm_mpoa_init(void)
1436 register_atm_ioctl(&atm_ioctl_ops);
1438 #ifdef CONFIG_PROC_FS
1439 if (mpc_proc_init() != 0)
1440 printk(KERN_INFO "mpoa: failed to initialize /proc/mpoa\n");
1442 printk(KERN_INFO "mpoa: /proc/mpoa initialized\n");
1445 printk("mpc.c: " __DATE__ " " __TIME__ " initialized\n");
1450 void __exit atm_mpoa_cleanup(void)
1452 struct mpoa_client *mpc, *tmp;
1453 struct atm_mpoa_qos *qos, *nextqos;
1454 struct lec_priv *priv;
1456 #ifdef CONFIG_PROC_FS
1460 del_timer(&mpc_timer);
1461 unregister_netdevice_notifier(&mpoa_notifier);
1462 deregister_atm_ioctl(&atm_ioctl_ops);
1466 while (mpc != NULL) {
1468 if (mpc->dev != NULL) {
1470 priv = (struct lec_priv *)mpc->dev->priv;
1471 if (priv->lane2_ops != NULL)
1472 priv->lane2_ops->associate_indicator = NULL;
1474 ddprintk("mpoa: cleanup_module: about to clear caches\n");
1475 mpc->in_ops->destroy_cache(mpc);
1476 mpc->eg_ops->destroy_cache(mpc);
1477 ddprintk("mpoa: cleanup_module: caches cleared\n");
1478 kfree(mpc->mps_macs);
1479 memset(mpc, 0, sizeof(struct mpoa_client));
1480 ddprintk("mpoa: cleanup_module: about to kfree %p\n", mpc);
1482 ddprintk("mpoa: cleanup_module: next mpc is at %p\n", tmp);
1488 while (qos != NULL) {
1489 nextqos = qos->next;
1490 dprintk("mpoa: cleanup_module: freeing qos entry %p\n", qos);
1498 module_init(atm_mpoa_init);
1499 module_exit(atm_mpoa_cleanup);
1501 MODULE_LICENSE("GPL");