1 #include <linux/kernel.h>
2 #include <linux/string.h>
3 #include <linux/timer.h>
4 #include <linux/init.h>
5 #include <linux/bitops.h>
6 #include <linux/capability.h>
7 #include <linux/seq_file.h>
9 /* We are an ethernet device */
10 #include <linux/if_ether.h>
11 #include <linux/netdevice.h>
12 #include <linux/etherdevice.h>
14 #include <linux/skbuff.h>
16 #include <asm/byteorder.h>
17 #include <asm/uaccess.h>
18 #include <net/checksum.h> /* for ip_fast_csum() */
21 #include <linux/proc_fs.h>
24 #include <linux/atmdev.h>
25 #include <linux/atmlec.h>
26 #include <linux/atmmpc.h>
28 #include <linux/config.h>
29 #include <linux/module.h>
33 #include "resources.h"
36 * mpc.c: Implementation of MPOA client kernel part
40 #define dprintk printk /* debug */
42 #define dprintk(format,args...)
46 #define ddprintk printk /* more debug */
48 #define ddprintk(format,args...)
53 #define MPOA_TAG_LEN 4
55 /* mpc_daemon -> kernel */
56 static void MPOA_trigger_rcvd (struct k_message *msg, struct mpoa_client *mpc);
57 static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc);
58 static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
59 static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
60 static void mps_death(struct k_message *msg, struct mpoa_client *mpc);
61 static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action);
62 static void MPOA_cache_impos_rcvd(struct k_message *msg, struct mpoa_client *mpc);
63 static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg, struct mpoa_client *mpc);
64 static void set_mps_mac_addr_rcvd(struct k_message *mesg, struct mpoa_client *mpc);
66 static uint8_t *copy_macs(struct mpoa_client *mpc, uint8_t *router_mac,
67 uint8_t *tlvs, uint8_t mps_macs, uint8_t device_type);
68 static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry);
70 static void send_set_mps_ctrl_addr(char *addr, struct mpoa_client *mpc);
71 static void mpoad_close(struct atm_vcc *vcc);
72 static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb);
74 static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb);
75 static int mpc_send_packet(struct sk_buff *skb, struct net_device *dev);
76 static int mpoa_event_listener(struct notifier_block *mpoa_notifier, unsigned long event, void *dev);
77 static void mpc_timer_refresh(void);
78 static void mpc_cache_check( unsigned long checking_time );
80 static struct llc_snap_hdr llc_snap_mpoa_ctrl = {
83 {0x00, 0x03} /* For MPOA control PDUs */
85 static struct llc_snap_hdr llc_snap_mpoa_data = {
88 {0x08, 0x00} /* This is for IP PDUs only */
90 static struct llc_snap_hdr llc_snap_mpoa_data_tagged = {
93 {0x88, 0x4c} /* This is for tagged data PDUs */
96 static struct notifier_block mpoa_notifier = {
102 #ifdef CONFIG_PROC_FS
103 extern int mpc_proc_init(void);
104 extern void mpc_proc_clean(void);
107 struct mpoa_client *mpcs = NULL; /* FIXME */
108 static struct atm_mpoa_qos *qos_head = NULL;
109 static DEFINE_TIMER(mpc_timer, NULL, 0, 0);
112 static struct mpoa_client *find_mpc_by_itfnum(int itf)
114 struct mpoa_client *mpc;
116 mpc = mpcs; /* our global linked list */
117 while (mpc != NULL) {
118 if (mpc->dev_num == itf)
123 return NULL; /* not found */
126 static struct mpoa_client *find_mpc_by_vcc(struct atm_vcc *vcc)
128 struct mpoa_client *mpc;
130 mpc = mpcs; /* our global linked list */
131 while (mpc != NULL) {
132 if (mpc->mpoad_vcc == vcc)
137 return NULL; /* not found */
140 static struct mpoa_client *find_mpc_by_lec(struct net_device *dev)
142 struct mpoa_client *mpc;
144 mpc = mpcs; /* our global linked list */
145 while (mpc != NULL) {
151 return NULL; /* not found */
155 * Functions for managing QoS list
159 * Overwrites the old entry or makes a new one.
161 struct atm_mpoa_qos *atm_mpoa_add_qos(uint32_t dst_ip, struct atm_qos *qos)
163 struct atm_mpoa_qos *entry;
165 entry = atm_mpoa_search_qos(dst_ip);
171 entry = kmalloc(sizeof(struct atm_mpoa_qos), GFP_KERNEL);
173 printk("mpoa: atm_mpoa_add_qos: out of memory\n");
177 entry->ipaddr = dst_ip;
180 entry->next = qos_head;
186 struct atm_mpoa_qos *atm_mpoa_search_qos(uint32_t dst_ip)
188 struct atm_mpoa_qos *qos;
191 while( qos != NULL ){
192 if(qos->ipaddr == dst_ip) {
202 * Returns 0 for failure
204 int atm_mpoa_delete_qos(struct atm_mpoa_qos *entry)
207 struct atm_mpoa_qos *curr;
209 if (entry == NULL) return 0;
210 if (entry == qos_head) {
211 qos_head = qos_head->next;
217 while (curr != NULL) {
218 if (curr->next == entry) {
219 curr->next = entry->next;
229 /* this is buggered - we need locking for qos_head */
230 void atm_mpoa_disp_qos(struct seq_file *m)
232 struct atm_mpoa_qos *qos;
235 seq_printf(m, "QoS entries for shortcuts:\n");
236 seq_printf(m, "IP address\n TX:max_pcr pcr min_pcr max_cdv max_sdu\n RX:max_pcr pcr min_pcr max_cdv max_sdu\n");
238 while (qos != NULL) {
239 seq_printf(m, "%u.%u.%u.%u\n %-7d %-7d %-7d %-7d %-7d\n %-7d %-7d %-7d %-7d %-7d\n",
240 NIPQUAD(qos->ipaddr),
241 qos->qos.txtp.max_pcr, qos->qos.txtp.pcr, qos->qos.txtp.min_pcr, qos->qos.txtp.max_cdv, qos->qos.txtp.max_sdu,
242 qos->qos.rxtp.max_pcr, qos->qos.rxtp.pcr, qos->qos.rxtp.min_pcr, qos->qos.rxtp.max_cdv, qos->qos.rxtp.max_sdu);
247 static struct net_device *find_lec_by_itfnum(int itf)
249 struct net_device *dev;
252 sprintf(name, "lec%d", itf);
253 dev = dev_get_by_name(name);
258 static struct mpoa_client *alloc_mpc(void)
260 struct mpoa_client *mpc;
262 mpc = kmalloc(sizeof (struct mpoa_client), GFP_KERNEL);
265 memset(mpc, 0, sizeof(struct mpoa_client));
266 rwlock_init(&mpc->ingress_lock);
267 rwlock_init(&mpc->egress_lock);
269 atm_mpoa_init_cache(mpc);
271 mpc->parameters.mpc_p1 = MPC_P1;
272 mpc->parameters.mpc_p2 = MPC_P2;
273 memset(mpc->parameters.mpc_p3,0,sizeof(mpc->parameters.mpc_p3));
274 mpc->parameters.mpc_p4 = MPC_P4;
275 mpc->parameters.mpc_p5 = MPC_P5;
276 mpc->parameters.mpc_p6 = MPC_P6;
285 * start_mpc() puts the MPC on line. All the packets destined
286 * to the lec underneath us are now being monitored and
287 * shortcuts will be established.
290 static void start_mpc(struct mpoa_client *mpc, struct net_device *dev)
293 dprintk("mpoa: (%s) start_mpc:\n", mpc->dev->name);
294 if (dev->hard_start_xmit == NULL) {
295 printk("mpoa: (%s) start_mpc: dev->hard_start_xmit == NULL, not starting\n",
299 mpc->old_hard_start_xmit = dev->hard_start_xmit;
300 dev->hard_start_xmit = mpc_send_packet;
305 static void stop_mpc(struct mpoa_client *mpc)
308 dprintk("mpoa: (%s) stop_mpc:", mpc->dev->name);
310 /* Lets not nullify lec device's dev->hard_start_xmit */
311 if (mpc->dev->hard_start_xmit != mpc_send_packet) {
312 dprintk(" mpc already stopped, not fatal\n");
316 mpc->dev->hard_start_xmit = mpc->old_hard_start_xmit;
317 mpc->old_hard_start_xmit = NULL;
318 /* close_shortcuts(mpc); ??? FIXME */
323 static const char *mpoa_device_type_string(char type) __attribute__ ((unused));
325 static const char *mpoa_device_type_string(char type)
329 return "non-MPOA device";
338 return "both MPS and MPC";
341 return "unspecified (non-MPOA) device";
345 return ""; /* not reached */
349 * lec device calls this via its dev->priv->lane2_ops->associate_indicator()
350 * when it sees a TLV in LE_ARP packet.
351 * We fill in the pointer above when we see a LANE2 lec initializing
352 * See LANE2 spec 3.1.5
354 * Quite a big and ugly function but when you look at it
355 * all it does is to try to locate and parse MPOA Device
357 * We give our lec a pointer to this function and when the
358 * lec sees a TLV it uses the pointer to call this function.
361 static void lane2_assoc_ind(struct net_device *dev, uint8_t *mac_addr,
362 uint8_t *tlvs, uint32_t sizeoftlvs)
365 uint8_t length, mpoa_device_type, number_of_mps_macs;
366 uint8_t *end_of_tlvs;
367 struct mpoa_client *mpc;
369 mpoa_device_type = number_of_mps_macs = 0; /* silence gcc */
370 dprintk("mpoa: (%s) lane2_assoc_ind: received TLV(s), ", dev->name);
371 dprintk("total length of all TLVs %d\n", sizeoftlvs);
372 mpc = find_mpc_by_lec(dev); /* Sampo-Fix: moved here from below */
374 printk("mpoa: (%s) lane2_assoc_ind: no mpc\n", dev->name);
377 end_of_tlvs = tlvs + sizeoftlvs;
378 while (end_of_tlvs - tlvs >= 5) {
379 type = (tlvs[0] << 24) | (tlvs[1] << 16) | (tlvs[2] << 8) | tlvs[3];
382 dprintk(" type 0x%x length %02x\n", type, length);
383 if (tlvs + length > end_of_tlvs) {
384 printk("TLV value extends past its buffer, aborting parse\n");
389 printk("mpoa: (%s) lane2_assoc_ind: TLV type was 0, returning\n", dev->name);
393 if (type != TLV_MPOA_DEVICE_TYPE) {
395 continue; /* skip other TLVs */
397 mpoa_device_type = *tlvs++;
398 number_of_mps_macs = *tlvs++;
399 dprintk("mpoa: (%s) MPOA device type '%s', ", dev->name, mpoa_device_type_string(mpoa_device_type));
400 if (mpoa_device_type == MPS_AND_MPC &&
401 length < (42 + number_of_mps_macs*ETH_ALEN)) { /* :) */
402 printk("\nmpoa: (%s) lane2_assoc_ind: short MPOA Device Type TLV\n",
406 if ((mpoa_device_type == MPS || mpoa_device_type == MPC)
407 && length < 22 + number_of_mps_macs*ETH_ALEN) {
408 printk("\nmpoa: (%s) lane2_assoc_ind: short MPOA Device Type TLV\n",
412 if (mpoa_device_type != MPS && mpoa_device_type != MPS_AND_MPC) {
413 dprintk("ignoring non-MPS device\n");
414 if (mpoa_device_type == MPC) tlvs += 20;
415 continue; /* we are only interested in MPSs */
417 if (number_of_mps_macs == 0 && mpoa_device_type == MPS_AND_MPC) {
418 printk("\nmpoa: (%s) lane2_assoc_ind: MPS_AND_MPC has zero MACs\n", dev->name);
419 continue; /* someone should read the spec */
421 dprintk("this MPS has %d MAC addresses\n", number_of_mps_macs);
423 /* ok, now we can go and tell our daemon the control address of MPS */
424 send_set_mps_ctrl_addr(tlvs, mpc);
426 tlvs = copy_macs(mpc, mac_addr, tlvs, number_of_mps_macs, mpoa_device_type);
427 if (tlvs == NULL) return;
429 if (end_of_tlvs - tlvs != 0)
430 printk("mpoa: (%s) lane2_assoc_ind: ignoring %Zd bytes of trailing TLV carbage\n",
431 dev->name, end_of_tlvs - tlvs);
436 * Store at least advertizing router's MAC address
437 * plus the possible MAC address(es) to mpc->mps_macs.
438 * For a freshly allocated MPOA client mpc->mps_macs == 0.
440 static uint8_t *copy_macs(struct mpoa_client *mpc, uint8_t *router_mac,
441 uint8_t *tlvs, uint8_t mps_macs, uint8_t device_type)
444 num_macs = (mps_macs > 1) ? mps_macs : 1;
446 if (mpc->number_of_mps_macs != num_macs) { /* need to reallocate? */
447 if (mpc->number_of_mps_macs != 0) kfree(mpc->mps_macs);
448 mpc->number_of_mps_macs = 0;
449 mpc->mps_macs = kmalloc(num_macs*ETH_ALEN, GFP_KERNEL);
450 if (mpc->mps_macs == NULL) {
451 printk("mpoa: (%s) copy_macs: out of mem\n", mpc->dev->name);
455 memcpy(mpc->mps_macs, router_mac, ETH_ALEN);
456 tlvs += 20; if (device_type == MPS_AND_MPC) tlvs += 20;
458 memcpy(mpc->mps_macs, tlvs, mps_macs*ETH_ALEN);
459 tlvs += mps_macs*ETH_ALEN;
460 mpc->number_of_mps_macs = num_macs;
465 static int send_via_shortcut(struct sk_buff *skb, struct mpoa_client *mpc)
467 in_cache_entry *entry;
473 struct llc_snap_hdr hdr;
475 } tagged_llc_snap_hdr = {
476 {0xaa, 0xaa, 0x03, {0x00, 0x00, 0x00}, {0x88, 0x4c}},
480 buff = skb->data + mpc->dev->hard_header_len;
481 iph = (struct iphdr *)buff;
484 ddprintk("mpoa: (%s) send_via_shortcut: ipaddr 0x%x\n", mpc->dev->name, ipaddr);
486 entry = mpc->in_ops->get(ipaddr, mpc);
488 entry = mpc->in_ops->add_entry(ipaddr, mpc);
489 if (entry != NULL) mpc->in_ops->put(entry);
492 if (mpc->in_ops->cache_hit(entry, mpc) != OPEN){ /* threshold not exceeded or VCC not ready */
493 ddprintk("mpoa: (%s) send_via_shortcut: cache_hit: returns != OPEN\n", mpc->dev->name);
494 mpc->in_ops->put(entry);
498 ddprintk("mpoa: (%s) send_via_shortcut: using shortcut\n", mpc->dev->name);
499 /* MPOA spec A.1.4, MPOA client must decrement IP ttl at least by one */
501 ddprintk("mpoa: (%s) send_via_shortcut: IP ttl = %u, using LANE\n", mpc->dev->name, iph->ttl);
502 mpc->in_ops->put(entry);
507 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
509 if (entry->ctrl_info.tag != 0) {
510 ddprintk("mpoa: (%s) send_via_shortcut: adding tag 0x%x\n", mpc->dev->name, entry->ctrl_info.tag);
511 tagged_llc_snap_hdr.tag = entry->ctrl_info.tag;
512 skb_pull(skb, ETH_HLEN); /* get rid of Eth header */
513 skb_push(skb, sizeof(tagged_llc_snap_hdr)); /* add LLC/SNAP header */
514 memcpy(skb->data, &tagged_llc_snap_hdr, sizeof(tagged_llc_snap_hdr));
516 skb_pull(skb, ETH_HLEN); /* get rid of Eth header */
517 skb_push(skb, sizeof(struct llc_snap_hdr)); /* add LLC/SNAP header + tag */
518 memcpy(skb->data, &llc_snap_mpoa_data, sizeof(struct llc_snap_hdr));
521 atomic_add(skb->truesize, &sk_atm(entry->shortcut)->sk_wmem_alloc);
522 ATM_SKB(skb)->atm_options = entry->shortcut->atm_options;
523 entry->shortcut->send(entry->shortcut, skb);
524 entry->packets_fwded++;
525 mpc->in_ops->put(entry);
531 * Probably needs some error checks and locking, not sure...
533 static int mpc_send_packet(struct sk_buff *skb, struct net_device *dev)
536 struct mpoa_client *mpc;
540 mpc = find_mpc_by_lec(dev); /* this should NEVER fail */
542 printk("mpoa: (%s) mpc_send_packet: no MPC found\n", dev->name);
546 eth = (struct ethhdr *)skb->data;
547 if (eth->h_proto != htons(ETH_P_IP))
548 goto non_ip; /* Multi-Protocol Over ATM :-) */
550 while (i < mpc->number_of_mps_macs) {
551 if (!compare_ether_addr(eth->h_dest, (mpc->mps_macs + i*ETH_ALEN)))
552 if ( send_via_shortcut(skb, mpc) == 0 ) /* try shortcut */
553 return 0; /* success! */
558 retval = mpc->old_hard_start_xmit(skb,dev);
563 static int atm_mpoa_vcc_attach(struct atm_vcc *vcc, void __user *arg)
566 struct mpoa_client *mpc;
567 struct atmmpc_ioc ioc_data;
568 in_cache_entry *in_entry;
572 bytes_left = copy_from_user(&ioc_data, arg, sizeof(struct atmmpc_ioc));
573 if (bytes_left != 0) {
574 printk("mpoa: mpc_vcc_attach: Short read (missed %d bytes) from userland\n", bytes_left);
577 ipaddr = ioc_data.ipaddr;
578 if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF)
581 mpc = find_mpc_by_itfnum(ioc_data.dev_num);
585 if (ioc_data.type == MPC_SOCKET_INGRESS) {
586 in_entry = mpc->in_ops->get(ipaddr, mpc);
587 if (in_entry == NULL || in_entry->entry_state < INGRESS_RESOLVED) {
588 printk("mpoa: (%s) mpc_vcc_attach: did not find RESOLVED entry from ingress cache\n",
590 if (in_entry != NULL) mpc->in_ops->put(in_entry);
593 ip = (unsigned char*)&in_entry->ctrl_info.in_dst_ip;
594 printk("mpoa: (%s) mpc_vcc_attach: attaching ingress SVC, entry = %u.%u.%u.%u\n",
595 mpc->dev->name, ip[0], ip[1], ip[2], ip[3]);
596 in_entry->shortcut = vcc;
597 mpc->in_ops->put(in_entry);
599 printk("mpoa: (%s) mpc_vcc_attach: attaching egress SVC\n", mpc->dev->name);
602 vcc->proto_data = mpc->dev;
603 vcc->push = mpc_push;
611 static void mpc_vcc_close(struct atm_vcc *vcc, struct net_device *dev)
613 struct mpoa_client *mpc;
614 in_cache_entry *in_entry;
615 eg_cache_entry *eg_entry;
617 mpc = find_mpc_by_lec(dev);
619 printk("mpoa: (%s) mpc_vcc_close: close for unknown MPC\n", dev->name);
623 dprintk("mpoa: (%s) mpc_vcc_close:\n", dev->name);
624 in_entry = mpc->in_ops->get_by_vcc(vcc, mpc);
626 unsigned char *ip __attribute__ ((unused)) =
627 (unsigned char *)&in_entry->ctrl_info.in_dst_ip;
628 dprintk("mpoa: (%s) mpc_vcc_close: ingress SVC closed ip = %u.%u.%u.%u\n",
629 mpc->dev->name, ip[0], ip[1], ip[2], ip[3]);
630 in_entry->shortcut = NULL;
631 mpc->in_ops->put(in_entry);
633 eg_entry = mpc->eg_ops->get_by_vcc(vcc, mpc);
635 dprintk("mpoa: (%s) mpc_vcc_close: egress SVC closed\n", mpc->dev->name);
636 eg_entry->shortcut = NULL;
637 mpc->eg_ops->put(eg_entry);
640 if (in_entry == NULL && eg_entry == NULL)
641 dprintk("mpoa: (%s) mpc_vcc_close: unused vcc closed\n", dev->name);
646 static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb)
648 struct net_device *dev = (struct net_device *)vcc->proto_data;
649 struct sk_buff *new_skb;
651 struct mpoa_client *mpc;
655 ddprintk("mpoa: (%s) mpc_push:\n", dev->name);
657 dprintk("mpoa: (%s) mpc_push: null skb, closing VCC\n", dev->name);
658 mpc_vcc_close(vcc, dev);
663 if (memcmp(skb->data, &llc_snap_mpoa_ctrl, sizeof(struct llc_snap_hdr)) == 0) {
664 struct sock *sk = sk_atm(vcc);
666 dprintk("mpoa: (%s) mpc_push: control packet arrived\n", dev->name);
667 /* Pass control packets to daemon */
668 skb_queue_tail(&sk->sk_receive_queue, skb);
669 sk->sk_data_ready(sk, skb->len);
673 /* data coming over the shortcut */
674 atm_return(vcc, skb->truesize);
676 mpc = find_mpc_by_lec(dev);
678 printk("mpoa: (%s) mpc_push: unknown MPC\n", dev->name);
682 if (memcmp(skb->data, &llc_snap_mpoa_data_tagged, sizeof(struct llc_snap_hdr)) == 0) { /* MPOA tagged data */
683 ddprintk("mpoa: (%s) mpc_push: tagged data packet arrived\n", dev->name);
685 } else if (memcmp(skb->data, &llc_snap_mpoa_data, sizeof(struct llc_snap_hdr)) == 0) { /* MPOA data */
686 printk("mpoa: (%s) mpc_push: non-tagged data packet arrived\n", dev->name);
687 printk(" mpc_push: non-tagged data unsupported, purging\n");
688 dev_kfree_skb_any(skb);
691 printk("mpoa: (%s) mpc_push: garbage arrived, purging\n", dev->name);
692 dev_kfree_skb_any(skb);
696 tmp = skb->data + sizeof(struct llc_snap_hdr);
697 tag = *(uint32_t *)tmp;
699 eg = mpc->eg_ops->get_by_tag(tag, mpc);
701 printk("mpoa: (%s) mpc_push: Didn't find egress cache entry, tag = %u\n",
703 purge_egress_shortcut(vcc, NULL);
704 dev_kfree_skb_any(skb);
709 * See if ingress MPC is using shortcut we opened as a return channel.
710 * This means we have a bi-directional vcc opened by us.
712 if (eg->shortcut == NULL) {
714 printk("mpoa: (%s) mpc_push: egress SVC in use\n", dev->name);
717 skb_pull(skb, sizeof(struct llc_snap_hdr) + sizeof(tag)); /* get rid of LLC/SNAP header */
718 new_skb = skb_realloc_headroom(skb, eg->ctrl_info.DH_length); /* LLC/SNAP is shorter than MAC header :( */
719 dev_kfree_skb_any(skb);
720 if (new_skb == NULL){
721 mpc->eg_ops->put(eg);
724 skb_push(new_skb, eg->ctrl_info.DH_length); /* add MAC header */
725 memcpy(new_skb->data, eg->ctrl_info.DLL_header, eg->ctrl_info.DH_length);
726 new_skb->protocol = eth_type_trans(new_skb, dev);
727 new_skb->nh.raw = new_skb->data;
729 eg->latest_ip_addr = new_skb->nh.iph->saddr;
731 mpc->eg_ops->put(eg);
733 memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
739 static struct atmdev_ops mpc_ops = { /* only send is required */
740 .close = mpoad_close,
741 .send = msg_from_mpoad
744 static struct atm_dev mpc_dev = {
748 .lock = SPIN_LOCK_UNLOCKED
749 /* members not explicitly initialised will be 0 */
752 static int atm_mpoa_mpoad_attach (struct atm_vcc *vcc, int arg)
754 struct mpoa_client *mpc;
755 struct lec_priv *priv;
759 init_timer(&mpc_timer);
762 /* This lets us now how our LECs are doing */
763 err = register_netdevice_notifier(&mpoa_notifier);
765 del_timer(&mpc_timer);
770 mpc = find_mpc_by_itfnum(arg);
772 dprintk("mpoa: mpoad_attach: allocating new mpc for itf %d\n", arg);
777 mpc->dev = find_lec_by_itfnum(arg); /* NULL if there was no lec */
779 if (mpc->mpoad_vcc) {
780 printk("mpoa: mpoad_attach: mpoad is already present for itf %d\n", arg);
784 if (mpc->dev) { /* check if the lec is LANE2 capable */
785 priv = (struct lec_priv *)mpc->dev->priv;
786 if (priv->lane_version < 2) {
790 priv->lane2_ops->associate_indicator = lane2_assoc_ind;
793 mpc->mpoad_vcc = vcc;
795 vcc_insert_socket(sk_atm(vcc));
796 set_bit(ATM_VF_META,&vcc->flags);
797 set_bit(ATM_VF_READY,&vcc->flags);
800 char empty[ATM_ESA_LEN];
801 memset(empty, 0, ATM_ESA_LEN);
803 start_mpc(mpc, mpc->dev);
804 /* set address if mpcd e.g. gets killed and restarted.
805 * If we do not do it now we have to wait for the next LE_ARP
807 if ( memcmp(mpc->mps_ctrl_addr, empty, ATM_ESA_LEN) != 0 )
808 send_set_mps_ctrl_addr(mpc->mps_ctrl_addr, mpc);
811 __module_get(THIS_MODULE);
815 static void send_set_mps_ctrl_addr(char *addr, struct mpoa_client *mpc)
817 struct k_message mesg;
819 memcpy (mpc->mps_ctrl_addr, addr, ATM_ESA_LEN);
821 mesg.type = SET_MPS_CTRL_ADDR;
822 memcpy(mesg.MPS_ctrl, addr, ATM_ESA_LEN);
823 msg_to_mpoad(&mesg, mpc);
828 static void mpoad_close(struct atm_vcc *vcc)
830 struct mpoa_client *mpc;
833 mpc = find_mpc_by_vcc(vcc);
835 printk("mpoa: mpoad_close: did not find MPC\n");
838 if (!mpc->mpoad_vcc) {
839 printk("mpoa: mpoad_close: close for non-present mpoad\n");
843 mpc->mpoad_vcc = NULL;
845 struct lec_priv *priv = (struct lec_priv *)mpc->dev->priv;
846 priv->lane2_ops->associate_indicator = NULL;
851 mpc->in_ops->destroy_cache(mpc);
852 mpc->eg_ops->destroy_cache(mpc);
854 while ((skb = skb_dequeue(&sk_atm(vcc)->sk_receive_queue))) {
855 atm_return(vcc, skb->truesize);
859 printk("mpoa: (%s) going down\n",
860 (mpc->dev) ? mpc->dev->name : "<unknown>");
861 module_put(THIS_MODULE);
869 static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb)
872 struct mpoa_client *mpc = find_mpc_by_vcc(vcc);
873 struct k_message *mesg = (struct k_message*)skb->data;
874 atomic_sub(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc);
877 printk("mpoa: msg_from_mpoad: no mpc found\n");
880 dprintk("mpoa: (%s) msg_from_mpoad:", (mpc->dev) ? mpc->dev->name : "<unknown>");
882 case MPOA_RES_REPLY_RCVD:
883 dprintk(" mpoa_res_reply_rcvd\n");
884 MPOA_res_reply_rcvd(mesg, mpc);
886 case MPOA_TRIGGER_RCVD:
887 dprintk(" mpoa_trigger_rcvd\n");
888 MPOA_trigger_rcvd(mesg, mpc);
890 case INGRESS_PURGE_RCVD:
891 dprintk(" nhrp_purge_rcvd\n");
892 ingress_purge_rcvd(mesg, mpc);
894 case EGRESS_PURGE_RCVD:
895 dprintk(" egress_purge_reply_rcvd\n");
896 egress_purge_rcvd(mesg, mpc);
899 dprintk(" mps_death\n");
900 mps_death(mesg, mpc);
902 case CACHE_IMPOS_RCVD:
903 dprintk(" cache_impos_rcvd\n");
904 MPOA_cache_impos_rcvd(mesg, mpc);
906 case SET_MPC_CTRL_ADDR:
907 dprintk(" set_mpc_ctrl_addr\n");
908 set_mpc_ctrl_addr_rcvd(mesg, mpc);
910 case SET_MPS_MAC_ADDR:
911 dprintk(" set_mps_mac_addr\n");
912 set_mps_mac_addr_rcvd(mesg, mpc);
914 case CLEAN_UP_AND_EXIT:
915 dprintk(" clean_up_and_exit\n");
916 clean_up(mesg, mpc, DIE);
919 dprintk(" reload\n");
920 clean_up(mesg, mpc, RELOAD);
923 dprintk(" set_mpc_params\n");
924 mpc->parameters = mesg->content.params;
927 dprintk(" unknown message %d\n", mesg->type);
935 /* Remember that this function may not do things that sleep */
936 int msg_to_mpoad(struct k_message *mesg, struct mpoa_client *mpc)
941 if (mpc == NULL || !mpc->mpoad_vcc) {
942 printk("mpoa: msg_to_mpoad: mesg %d to a non-existent mpoad\n", mesg->type);
946 skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
949 skb_put(skb, sizeof(struct k_message));
950 memcpy(skb->data, mesg, sizeof(struct k_message));
951 atm_force_charge(mpc->mpoad_vcc, skb->truesize);
953 sk = sk_atm(mpc->mpoad_vcc);
954 skb_queue_tail(&sk->sk_receive_queue, skb);
955 sk->sk_data_ready(sk, skb->len);
960 static int mpoa_event_listener(struct notifier_block *mpoa_notifier, unsigned long event, void *dev_ptr)
962 struct net_device *dev;
963 struct mpoa_client *mpc;
964 struct lec_priv *priv;
966 dev = (struct net_device *)dev_ptr;
967 if (dev->name == NULL || strncmp(dev->name, "lec", 3))
968 return NOTIFY_DONE; /* we are only interested in lec:s */
971 case NETDEV_REGISTER: /* a new lec device was allocated */
972 priv = (struct lec_priv *)dev->priv;
973 if (priv->lane_version < 2)
975 priv->lane2_ops->associate_indicator = lane2_assoc_ind;
976 mpc = find_mpc_by_itfnum(priv->itfnum);
978 dprintk("mpoa: mpoa_event_listener: allocating new mpc for %s\n",
982 printk("mpoa: mpoa_event_listener: no new mpc");
986 mpc->dev_num = priv->itfnum;
989 dprintk("mpoa: (%s) was initialized\n", dev->name);
991 case NETDEV_UNREGISTER:
992 /* the lec device was deallocated */
993 mpc = find_mpc_by_lec(dev);
996 dprintk("mpoa: device (%s) was deallocated\n", dev->name);
1002 /* the dev was ifconfig'ed up */
1003 mpc = find_mpc_by_lec(dev);
1006 if (mpc->mpoad_vcc != NULL) {
1007 start_mpc(mpc, dev);
1011 /* the dev was ifconfig'ed down */
1012 /* this means that the flow of packets from the
1015 mpc = find_mpc_by_lec(dev);
1018 if (mpc->mpoad_vcc != NULL) {
1024 case NETDEV_CHANGEMTU:
1025 case NETDEV_CHANGEADDR:
1026 case NETDEV_GOING_DOWN:
1036 * Functions which are called after a message is received from mpcd.
1037 * Msg is reused on purpose.
1041 static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1043 uint32_t dst_ip = msg->content.in_info.in_dst_ip;
1044 in_cache_entry *entry;
1046 entry = mpc->in_ops->get(dst_ip, mpc);
1048 entry = mpc->in_ops->add_entry(dst_ip, mpc);
1049 entry->entry_state = INGRESS_RESOLVING;
1050 msg->type = SND_MPOA_RES_RQST;
1051 msg->content.in_info = entry->ctrl_info;
1052 msg_to_mpoad(msg, mpc);
1053 do_gettimeofday(&(entry->reply_wait));
1054 mpc->in_ops->put(entry);
1058 if(entry->entry_state == INGRESS_INVALID){
1059 entry->entry_state = INGRESS_RESOLVING;
1060 msg->type = SND_MPOA_RES_RQST;
1061 msg->content.in_info = entry->ctrl_info;
1062 msg_to_mpoad(msg, mpc);
1063 do_gettimeofday(&(entry->reply_wait));
1064 mpc->in_ops->put(entry);
1068 printk("mpoa: (%s) MPOA_trigger_rcvd: entry already in resolving state\n",
1069 (mpc->dev) ? mpc->dev->name : "<unknown>");
1070 mpc->in_ops->put(entry);
1075 * Things get complicated because we have to check if there's an egress
1076 * shortcut with suitable traffic parameters we could use.
1078 static void check_qos_and_open_shortcut(struct k_message *msg, struct mpoa_client *client, in_cache_entry *entry)
1080 uint32_t dst_ip = msg->content.in_info.in_dst_ip;
1081 struct atm_mpoa_qos *qos = atm_mpoa_search_qos(dst_ip);
1082 eg_cache_entry *eg_entry = client->eg_ops->get_by_src_ip(dst_ip, client);
1084 if(eg_entry && eg_entry->shortcut){
1085 if(eg_entry->shortcut->qos.txtp.traffic_class &
1086 msg->qos.txtp.traffic_class &
1087 (qos ? qos->qos.txtp.traffic_class : ATM_UBR | ATM_CBR)){
1088 if(eg_entry->shortcut->qos.txtp.traffic_class == ATM_UBR)
1089 entry->shortcut = eg_entry->shortcut;
1090 else if(eg_entry->shortcut->qos.txtp.max_pcr > 0)
1091 entry->shortcut = eg_entry->shortcut;
1093 if(entry->shortcut){
1094 dprintk("mpoa: (%s) using egress SVC to reach %u.%u.%u.%u\n",client->dev->name, NIPQUAD(dst_ip));
1095 client->eg_ops->put(eg_entry);
1099 if (eg_entry != NULL)
1100 client->eg_ops->put(eg_entry);
1102 /* No luck in the egress cache we must open an ingress SVC */
1103 msg->type = OPEN_INGRESS_SVC;
1104 if (qos && (qos->qos.txtp.traffic_class == msg->qos.txtp.traffic_class))
1106 msg->qos = qos->qos;
1107 printk("mpoa: (%s) trying to get a CBR shortcut\n",client->dev->name);
1109 else memset(&msg->qos,0,sizeof(struct atm_qos));
1110 msg_to_mpoad(msg, client);
1114 static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1118 uint32_t dst_ip = msg->content.in_info.in_dst_ip;
1119 in_cache_entry *entry = mpc->in_ops->get(dst_ip, mpc);
1120 dprintk("mpoa: (%s) MPOA_res_reply_rcvd: ip %u.%u.%u.%u\n", mpc->dev->name, NIPQUAD(dst_ip));
1121 ddprintk("mpoa: (%s) MPOA_res_reply_rcvd() entry = %p", mpc->dev->name, entry);
1123 printk("\nmpoa: (%s) ARGH, received res. reply for an entry that doesn't exist.\n", mpc->dev->name);
1126 ddprintk(" entry_state = %d ", entry->entry_state);
1128 if (entry->entry_state == INGRESS_RESOLVED) {
1129 printk("\nmpoa: (%s) MPOA_res_reply_rcvd for RESOLVED entry!\n", mpc->dev->name);
1130 mpc->in_ops->put(entry);
1134 entry->ctrl_info = msg->content.in_info;
1135 do_gettimeofday(&(entry->tv));
1136 do_gettimeofday(&(entry->reply_wait)); /* Used in refreshing func from now on */
1137 entry->refresh_time = 0;
1138 ddprintk("entry->shortcut = %p\n", entry->shortcut);
1140 if(entry->entry_state == INGRESS_RESOLVING && entry->shortcut != NULL){
1141 entry->entry_state = INGRESS_RESOLVED;
1142 mpc->in_ops->put(entry);
1143 return; /* Shortcut already open... */
1146 if (entry->shortcut != NULL) {
1147 printk("mpoa: (%s) MPOA_res_reply_rcvd: entry->shortcut != NULL, impossible!\n",
1149 mpc->in_ops->put(entry);
1153 check_qos_and_open_shortcut(msg, mpc, entry);
1154 entry->entry_state = INGRESS_RESOLVED;
1155 mpc->in_ops->put(entry);
1161 static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1163 uint32_t dst_ip = msg->content.in_info.in_dst_ip;
1164 uint32_t mask = msg->ip_mask;
1165 unsigned char *ip = (unsigned char *)&dst_ip;
1166 in_cache_entry *entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1169 printk("mpoa: (%s) ingress_purge_rcvd: purge for a non-existing entry, ", mpc->dev->name);
1170 printk("ip = %u.%u.%u.%u\n", ip[0], ip[1], ip[2], ip[3]);
1175 dprintk("mpoa: (%s) ingress_purge_rcvd: removing an ingress entry, ip = %u.%u.%u.%u\n" ,
1176 mpc->dev->name, ip[0], ip[1], ip[2], ip[3]);
1177 write_lock_bh(&mpc->ingress_lock);
1178 mpc->in_ops->remove_entry(entry, mpc);
1179 write_unlock_bh(&mpc->ingress_lock);
1180 mpc->in_ops->put(entry);
1181 entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1182 } while (entry != NULL);
1187 static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1189 uint32_t cache_id = msg->content.eg_info.cache_id;
1190 eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(cache_id, mpc);
1192 if (entry == NULL) {
1193 dprintk("mpoa: (%s) egress_purge_rcvd: purge for a non-existing entry\n", mpc->dev->name);
1197 write_lock_irq(&mpc->egress_lock);
1198 mpc->eg_ops->remove_entry(entry, mpc);
1199 write_unlock_irq(&mpc->egress_lock);
1201 mpc->eg_ops->put(entry);
1206 static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry)
1209 struct k_message *purge_msg;
1210 struct sk_buff *skb;
1212 dprintk("mpoa: purge_egress_shortcut: entering\n");
1214 printk("mpoa: purge_egress_shortcut: vcc == NULL\n");
1218 skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
1220 printk("mpoa: purge_egress_shortcut: out of memory\n");
1224 skb_put(skb, sizeof(struct k_message));
1225 memset(skb->data, 0, sizeof(struct k_message));
1226 purge_msg = (struct k_message *)skb->data;
1227 purge_msg->type = DATA_PLANE_PURGE;
1229 purge_msg->content.eg_info = entry->ctrl_info;
1231 atm_force_charge(vcc, skb->truesize);
1234 skb_queue_tail(&sk->sk_receive_queue, skb);
1235 sk->sk_data_ready(sk, skb->len);
1236 dprintk("mpoa: purge_egress_shortcut: exiting:\n");
1242 * Our MPS died. Tell our daemon to send NHRP data plane purge to each
1243 * of the egress shortcuts we have.
1245 static void mps_death( struct k_message * msg, struct mpoa_client * mpc )
1247 eg_cache_entry *entry;
1249 dprintk("mpoa: (%s) mps_death:\n", mpc->dev->name);
1251 if(memcmp(msg->MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN)){
1252 printk("mpoa: (%s) mps_death: wrong MPS\n", mpc->dev->name);
1256 /* FIXME: This knows too much of the cache structure */
1257 read_lock_irq(&mpc->egress_lock);
1258 entry = mpc->eg_cache;
1259 while (entry != NULL) {
1260 purge_egress_shortcut(entry->shortcut, entry);
1261 entry = entry->next;
1263 read_unlock_irq(&mpc->egress_lock);
1265 mpc->in_ops->destroy_cache(mpc);
1266 mpc->eg_ops->destroy_cache(mpc);
1271 static void MPOA_cache_impos_rcvd( struct k_message * msg, struct mpoa_client * mpc)
1273 uint16_t holding_time;
1274 eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(msg->content.eg_info.cache_id, mpc);
1276 holding_time = msg->content.eg_info.holding_time;
1277 dprintk("mpoa: (%s) MPOA_cache_impos_rcvd: entry = %p, holding_time = %u\n",
1278 mpc->dev->name, entry, holding_time);
1279 if(entry == NULL && holding_time) {
1280 entry = mpc->eg_ops->add_entry(msg, mpc);
1281 mpc->eg_ops->put(entry);
1285 mpc->eg_ops->update(entry, holding_time);
1289 write_lock_irq(&mpc->egress_lock);
1290 mpc->eg_ops->remove_entry(entry, mpc);
1291 write_unlock_irq(&mpc->egress_lock);
1293 mpc->eg_ops->put(entry);
1298 static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg, struct mpoa_client *mpc)
1300 struct lec_priv *priv;
1303 uint8_t tlv[4 + 1 + 1 + 1 + ATM_ESA_LEN];
1305 tlv[0] = 00; tlv[1] = 0xa0; tlv[2] = 0x3e; tlv[3] = 0x2a; /* type */
1306 tlv[4] = 1 + 1 + ATM_ESA_LEN; /* length */
1307 tlv[5] = 0x02; /* MPOA client */
1308 tlv[6] = 0x00; /* number of MPS MAC addresses */
1310 memcpy(&tlv[7], mesg->MPS_ctrl, ATM_ESA_LEN); /* MPC ctrl ATM addr */
1311 memcpy(mpc->our_ctrl_addr, mesg->MPS_ctrl, ATM_ESA_LEN);
1313 dprintk("mpoa: (%s) setting MPC ctrl ATM address to ",
1314 (mpc->dev) ? mpc->dev->name : "<unknown>");
1315 for (i = 7; i < sizeof(tlv); i++)
1316 dprintk("%02x ", tlv[i]);
1320 priv = (struct lec_priv *)mpc->dev->priv;
1321 retval = priv->lane2_ops->associate_req(mpc->dev, mpc->dev->dev_addr, tlv, sizeof(tlv));
1323 printk("mpoa: (%s) MPOA device type TLV association failed\n", mpc->dev->name);
1324 retval = priv->lane2_ops->resolve(mpc->dev, NULL, 1, NULL, NULL);
1326 printk("mpoa: (%s) targetless LE_ARP request failed\n", mpc->dev->name);
1332 static void set_mps_mac_addr_rcvd(struct k_message *msg, struct mpoa_client *client)
1335 if(client->number_of_mps_macs)
1336 kfree(client->mps_macs);
1337 client->number_of_mps_macs = 0;
1338 client->mps_macs = kmalloc(ETH_ALEN,GFP_KERNEL);
1339 if (client->mps_macs == NULL) {
1340 printk("mpoa: set_mps_mac_addr_rcvd: out of memory\n");
1343 client->number_of_mps_macs = 1;
1344 memcpy(client->mps_macs, msg->MPS_ctrl, ETH_ALEN);
1350 * purge egress cache and tell daemon to 'action' (DIE, RELOAD)
1352 static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action)
1355 eg_cache_entry *entry;
1356 msg->type = SND_EGRESS_PURGE;
1359 /* FIXME: This knows too much of the cache structure */
1360 read_lock_irq(&mpc->egress_lock);
1361 entry = mpc->eg_cache;
1362 while (entry != NULL){
1363 msg->content.eg_info = entry->ctrl_info;
1364 dprintk("mpoa: cache_id %u\n", entry->ctrl_info.cache_id);
1365 msg_to_mpoad(msg, mpc);
1366 entry = entry->next;
1368 read_unlock_irq(&mpc->egress_lock);
1371 msg_to_mpoad(msg, mpc);
1375 static void mpc_timer_refresh(void)
1377 mpc_timer.expires = jiffies + (MPC_P2 * HZ);
1378 mpc_timer.data = mpc_timer.expires;
1379 mpc_timer.function = mpc_cache_check;
1380 add_timer(&mpc_timer);
1385 static void mpc_cache_check( unsigned long checking_time )
1387 struct mpoa_client *mpc = mpcs;
1388 static unsigned long previous_resolving_check_time;
1389 static unsigned long previous_refresh_time;
1391 while( mpc != NULL ){
1392 mpc->in_ops->clear_count(mpc);
1393 mpc->eg_ops->clear_expired(mpc);
1394 if(checking_time - previous_resolving_check_time > mpc->parameters.mpc_p4 * HZ ){
1395 mpc->in_ops->check_resolving(mpc);
1396 previous_resolving_check_time = checking_time;
1398 if(checking_time - previous_refresh_time > mpc->parameters.mpc_p5 * HZ ){
1399 mpc->in_ops->refresh(mpc);
1400 previous_refresh_time = checking_time;
1404 mpc_timer_refresh();
1409 static int atm_mpoa_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1412 struct atm_vcc *vcc = ATM_SD(sock);
1414 if (cmd != ATMMPC_CTRL && cmd != ATMMPC_DATA)
1415 return -ENOIOCTLCMD;
1417 if (!capable(CAP_NET_ADMIN))
1422 err = atm_mpoa_mpoad_attach(vcc, (int)arg);
1424 sock->state = SS_CONNECTED;
1427 err = atm_mpoa_vcc_attach(vcc, (void __user *)arg);
1436 static struct atm_ioctl atm_ioctl_ops = {
1437 .owner = THIS_MODULE,
1438 .ioctl = atm_mpoa_ioctl,
1441 static __init int atm_mpoa_init(void)
1443 register_atm_ioctl(&atm_ioctl_ops);
1445 #ifdef CONFIG_PROC_FS
1446 if (mpc_proc_init() != 0)
1447 printk(KERN_INFO "mpoa: failed to initialize /proc/mpoa\n");
1449 printk(KERN_INFO "mpoa: /proc/mpoa initialized\n");
1452 printk("mpc.c: " __DATE__ " " __TIME__ " initialized\n");
1457 static void __exit atm_mpoa_cleanup(void)
1459 struct mpoa_client *mpc, *tmp;
1460 struct atm_mpoa_qos *qos, *nextqos;
1461 struct lec_priv *priv;
1463 #ifdef CONFIG_PROC_FS
1467 del_timer(&mpc_timer);
1468 unregister_netdevice_notifier(&mpoa_notifier);
1469 deregister_atm_ioctl(&atm_ioctl_ops);
1473 while (mpc != NULL) {
1475 if (mpc->dev != NULL) {
1477 priv = (struct lec_priv *)mpc->dev->priv;
1478 if (priv->lane2_ops != NULL)
1479 priv->lane2_ops->associate_indicator = NULL;
1481 ddprintk("mpoa: cleanup_module: about to clear caches\n");
1482 mpc->in_ops->destroy_cache(mpc);
1483 mpc->eg_ops->destroy_cache(mpc);
1484 ddprintk("mpoa: cleanup_module: caches cleared\n");
1485 kfree(mpc->mps_macs);
1486 memset(mpc, 0, sizeof(struct mpoa_client));
1487 ddprintk("mpoa: cleanup_module: about to kfree %p\n", mpc);
1489 ddprintk("mpoa: cleanup_module: next mpc is at %p\n", tmp);
1495 while (qos != NULL) {
1496 nextqos = qos->next;
1497 dprintk("mpoa: cleanup_module: freeing qos entry %p\n", qos);
1505 module_init(atm_mpoa_init);
1506 module_exit(atm_mpoa_cleanup);
1508 MODULE_LICENSE("GPL");