blob: 7c85aa551d5e98977e7e9f91a0a6949f31bf9544 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/kernel.h>
2#include <linux/string.h>
3#include <linux/timer.h>
4#include <linux/init.h>
5#include <linux/bitops.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -08006#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/seq_file.h>
8
9/* We are an ethernet device */
10#include <linux/if_ether.h>
11#include <linux/netdevice.h>
12#include <linux/etherdevice.h>
13#include <net/sock.h>
14#include <linux/skbuff.h>
15#include <linux/ip.h>
16#include <asm/byteorder.h>
17#include <asm/uaccess.h>
18#include <net/checksum.h> /* for ip_fast_csum() */
19#include <net/arp.h>
20#include <net/dst.h>
21#include <linux/proc_fs.h>
22
23/* And atm device */
24#include <linux/atmdev.h>
25#include <linux/atmlec.h>
26#include <linux/atmmpc.h>
27/* Modular too */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29
30#include "lec.h"
31#include "mpc.h"
32#include "resources.h"
33
34/*
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090035 * mpc.c: Implementation of MPOA client kernel part
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
37
38#if 0
39#define dprintk printk /* debug */
40#else
41#define dprintk(format,args...)
42#endif
43
44#if 0
45#define ddprintk printk /* more debug */
46#else
47#define ddprintk(format,args...)
48#endif
49
50
51
52#define MPOA_TAG_LEN 4
53
54/* mpc_daemon -> kernel */
55static void MPOA_trigger_rcvd (struct k_message *msg, struct mpoa_client *mpc);
56static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc);
57static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
58static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
59static void mps_death(struct k_message *msg, struct mpoa_client *mpc);
60static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action);
61static void MPOA_cache_impos_rcvd(struct k_message *msg, struct mpoa_client *mpc);
62static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg, struct mpoa_client *mpc);
63static void set_mps_mac_addr_rcvd(struct k_message *mesg, struct mpoa_client *mpc);
64
65static uint8_t *copy_macs(struct mpoa_client *mpc, uint8_t *router_mac,
66 uint8_t *tlvs, uint8_t mps_macs, uint8_t device_type);
67static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry);
68
69static void send_set_mps_ctrl_addr(char *addr, struct mpoa_client *mpc);
70static void mpoad_close(struct atm_vcc *vcc);
71static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb);
72
73static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb);
74static int mpc_send_packet(struct sk_buff *skb, struct net_device *dev);
75static int mpoa_event_listener(struct notifier_block *mpoa_notifier, unsigned long event, void *dev);
76static void mpc_timer_refresh(void);
77static void mpc_cache_check( unsigned long checking_time );
78
79static struct llc_snap_hdr llc_snap_mpoa_ctrl = {
80 0xaa, 0xaa, 0x03,
81 {0x00, 0x00, 0x5e},
82 {0x00, 0x03} /* For MPOA control PDUs */
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090083};
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static struct llc_snap_hdr llc_snap_mpoa_data = {
85 0xaa, 0xaa, 0x03,
86 {0x00, 0x00, 0x00},
87 {0x08, 0x00} /* This is for IP PDUs only */
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090088};
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static struct llc_snap_hdr llc_snap_mpoa_data_tagged = {
90 0xaa, 0xaa, 0x03,
91 {0x00, 0x00, 0x00},
92 {0x88, 0x4c} /* This is for tagged data PDUs */
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090093};
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95static struct notifier_block mpoa_notifier = {
96 mpoa_event_listener,
97 NULL,
98 0
99};
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101struct mpoa_client *mpcs = NULL; /* FIXME */
102static struct atm_mpoa_qos *qos_head = NULL;
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700103static DEFINE_TIMER(mpc_timer, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105
106static struct mpoa_client *find_mpc_by_itfnum(int itf)
107{
108 struct mpoa_client *mpc;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 mpc = mpcs; /* our global linked list */
111 while (mpc != NULL) {
112 if (mpc->dev_num == itf)
113 return mpc;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900114 mpc = mpc->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116
117 return NULL; /* not found */
118}
119
120static struct mpoa_client *find_mpc_by_vcc(struct atm_vcc *vcc)
121{
122 struct mpoa_client *mpc;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 mpc = mpcs; /* our global linked list */
125 while (mpc != NULL) {
126 if (mpc->mpoad_vcc == vcc)
127 return mpc;
128 mpc = mpc->next;
129 }
130
131 return NULL; /* not found */
132}
133
134static struct mpoa_client *find_mpc_by_lec(struct net_device *dev)
135{
136 struct mpoa_client *mpc;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 mpc = mpcs; /* our global linked list */
139 while (mpc != NULL) {
140 if (mpc->dev == dev)
141 return mpc;
142 mpc = mpc->next;
143 }
144
145 return NULL; /* not found */
146}
147
148/*
149 * Functions for managing QoS list
150 */
151
152/*
153 * Overwrites the old entry or makes a new one.
154 */
Al Viro30d492d2006-11-14 21:11:29 -0800155struct atm_mpoa_qos *atm_mpoa_add_qos(__be32 dst_ip, struct atm_qos *qos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct atm_mpoa_qos *entry;
158
159 entry = atm_mpoa_search_qos(dst_ip);
160 if (entry != NULL) {
161 entry->qos = *qos;
162 return entry;
163 }
164
165 entry = kmalloc(sizeof(struct atm_mpoa_qos), GFP_KERNEL);
166 if (entry == NULL) {
167 printk("mpoa: atm_mpoa_add_qos: out of memory\n");
168 return entry;
169 }
170
171 entry->ipaddr = dst_ip;
172 entry->qos = *qos;
173
174 entry->next = qos_head;
175 qos_head = entry;
176
177 return entry;
178}
179
Al Viro30d492d2006-11-14 21:11:29 -0800180struct atm_mpoa_qos *atm_mpoa_search_qos(__be32 dst_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 struct atm_mpoa_qos *qos;
183
184 qos = qos_head;
185 while( qos != NULL ){
186 if(qos->ipaddr == dst_ip) {
187 break;
188 }
189 qos = qos->next;
190 }
191
192 return qos;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900193}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195/*
196 * Returns 0 for failure
197 */
198int atm_mpoa_delete_qos(struct atm_mpoa_qos *entry)
199{
200
201 struct atm_mpoa_qos *curr;
202
203 if (entry == NULL) return 0;
204 if (entry == qos_head) {
205 qos_head = qos_head->next;
206 kfree(entry);
207 return 1;
208 }
209
210 curr = qos_head;
211 while (curr != NULL) {
212 if (curr->next == entry) {
213 curr->next = entry->next;
214 kfree(entry);
215 return 1;
216 }
217 curr = curr->next;
218 }
219
220 return 0;
221}
222
223/* this is buggered - we need locking for qos_head */
224void atm_mpoa_disp_qos(struct seq_file *m)
225{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 struct atm_mpoa_qos *qos;
227
228 qos = qos_head;
229 seq_printf(m, "QoS entries for shortcuts:\n");
230 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");
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 while (qos != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 seq_printf(m, "%u.%u.%u.%u\n %-7d %-7d %-7d %-7d %-7d\n %-7d %-7d %-7d %-7d %-7d\n",
Al Viroff7512e2006-06-20 03:27:27 -0700234 NIPQUAD(qos->ipaddr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 qos->qos.txtp.max_pcr, qos->qos.txtp.pcr, qos->qos.txtp.min_pcr, qos->qos.txtp.max_cdv, qos->qos.txtp.max_sdu,
236 qos->qos.rxtp.max_pcr, qos->qos.rxtp.pcr, qos->qos.rxtp.min_pcr, qos->qos.rxtp.max_cdv, qos->qos.rxtp.max_sdu);
237 qos = qos->next;
238 }
239}
240
241static struct net_device *find_lec_by_itfnum(int itf)
242{
243 struct net_device *dev;
244 char name[IFNAMSIZ];
245
246 sprintf(name, "lec%d", itf);
247 dev = dev_get_by_name(name);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return dev;
250}
251
252static struct mpoa_client *alloc_mpc(void)
253{
254 struct mpoa_client *mpc;
255
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700256 mpc = kzalloc(sizeof (struct mpoa_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (mpc == NULL)
258 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 rwlock_init(&mpc->ingress_lock);
260 rwlock_init(&mpc->egress_lock);
261 mpc->next = mpcs;
262 atm_mpoa_init_cache(mpc);
263
264 mpc->parameters.mpc_p1 = MPC_P1;
265 mpc->parameters.mpc_p2 = MPC_P2;
266 memset(mpc->parameters.mpc_p3,0,sizeof(mpc->parameters.mpc_p3));
267 mpc->parameters.mpc_p4 = MPC_P4;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900268 mpc->parameters.mpc_p5 = MPC_P5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 mpc->parameters.mpc_p6 = MPC_P6;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 mpcs = mpc;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return mpc;
274}
275
276/*
277 *
278 * start_mpc() puts the MPC on line. All the packets destined
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900279 * to the lec underneath us are now being monitored and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * shortcuts will be established.
281 *
282 */
283static void start_mpc(struct mpoa_client *mpc, struct net_device *dev)
284{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900285
286 dprintk("mpoa: (%s) start_mpc:\n", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (dev->hard_start_xmit == NULL) {
288 printk("mpoa: (%s) start_mpc: dev->hard_start_xmit == NULL, not starting\n",
289 dev->name);
290 return;
291 }
292 mpc->old_hard_start_xmit = dev->hard_start_xmit;
293 dev->hard_start_xmit = mpc_send_packet;
294
295 return;
296}
297
298static void stop_mpc(struct mpoa_client *mpc)
299{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900300
301 dprintk("mpoa: (%s) stop_mpc:", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /* Lets not nullify lec device's dev->hard_start_xmit */
304 if (mpc->dev->hard_start_xmit != mpc_send_packet) {
305 dprintk(" mpc already stopped, not fatal\n");
306 return;
307 }
308 dprintk("\n");
309 mpc->dev->hard_start_xmit = mpc->old_hard_start_xmit;
310 mpc->old_hard_start_xmit = NULL;
311 /* close_shortcuts(mpc); ??? FIXME */
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return;
314}
315
316static const char *mpoa_device_type_string(char type) __attribute__ ((unused));
317
318static const char *mpoa_device_type_string(char type)
319{
320 switch(type) {
321 case NON_MPOA:
322 return "non-MPOA device";
323 break;
324 case MPS:
325 return "MPS";
326 break;
327 case MPC:
328 return "MPC";
329 break;
330 case MPS_AND_MPC:
331 return "both MPS and MPC";
332 break;
333 default:
334 return "unspecified (non-MPOA) device";
335 break;
336 }
337
338 return ""; /* not reached */
339}
340
341/*
342 * lec device calls this via its dev->priv->lane2_ops->associate_indicator()
343 * when it sees a TLV in LE_ARP packet.
344 * We fill in the pointer above when we see a LANE2 lec initializing
345 * See LANE2 spec 3.1.5
346 *
347 * Quite a big and ugly function but when you look at it
348 * all it does is to try to locate and parse MPOA Device
349 * Type TLV.
350 * We give our lec a pointer to this function and when the
351 * lec sees a TLV it uses the pointer to call this function.
352 *
353 */
354static void lane2_assoc_ind(struct net_device *dev, uint8_t *mac_addr,
355 uint8_t *tlvs, uint32_t sizeoftlvs)
356{
357 uint32_t type;
358 uint8_t length, mpoa_device_type, number_of_mps_macs;
359 uint8_t *end_of_tlvs;
360 struct mpoa_client *mpc;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 mpoa_device_type = number_of_mps_macs = 0; /* silence gcc */
363 dprintk("mpoa: (%s) lane2_assoc_ind: received TLV(s), ", dev->name);
364 dprintk("total length of all TLVs %d\n", sizeoftlvs);
365 mpc = find_mpc_by_lec(dev); /* Sampo-Fix: moved here from below */
366 if (mpc == NULL) {
367 printk("mpoa: (%s) lane2_assoc_ind: no mpc\n", dev->name);
368 return;
369 }
370 end_of_tlvs = tlvs + sizeoftlvs;
371 while (end_of_tlvs - tlvs >= 5) {
372 type = (tlvs[0] << 24) | (tlvs[1] << 16) | (tlvs[2] << 8) | tlvs[3];
373 length = tlvs[4];
374 tlvs += 5;
375 dprintk(" type 0x%x length %02x\n", type, length);
376 if (tlvs + length > end_of_tlvs) {
377 printk("TLV value extends past its buffer, aborting parse\n");
378 return;
379 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (type == 0) {
382 printk("mpoa: (%s) lane2_assoc_ind: TLV type was 0, returning\n", dev->name);
383 return;
384 }
385
386 if (type != TLV_MPOA_DEVICE_TYPE) {
387 tlvs += length;
388 continue; /* skip other TLVs */
389 }
390 mpoa_device_type = *tlvs++;
391 number_of_mps_macs = *tlvs++;
392 dprintk("mpoa: (%s) MPOA device type '%s', ", dev->name, mpoa_device_type_string(mpoa_device_type));
393 if (mpoa_device_type == MPS_AND_MPC &&
394 length < (42 + number_of_mps_macs*ETH_ALEN)) { /* :) */
395 printk("\nmpoa: (%s) lane2_assoc_ind: short MPOA Device Type TLV\n",
396 dev->name);
397 continue;
398 }
399 if ((mpoa_device_type == MPS || mpoa_device_type == MPC)
400 && length < 22 + number_of_mps_macs*ETH_ALEN) {
401 printk("\nmpoa: (%s) lane2_assoc_ind: short MPOA Device Type TLV\n",
402 dev->name);
403 continue;
404 }
405 if (mpoa_device_type != MPS && mpoa_device_type != MPS_AND_MPC) {
406 dprintk("ignoring non-MPS device\n");
407 if (mpoa_device_type == MPC) tlvs += 20;
408 continue; /* we are only interested in MPSs */
409 }
410 if (number_of_mps_macs == 0 && mpoa_device_type == MPS_AND_MPC) {
411 printk("\nmpoa: (%s) lane2_assoc_ind: MPS_AND_MPC has zero MACs\n", dev->name);
412 continue; /* someone should read the spec */
413 }
414 dprintk("this MPS has %d MAC addresses\n", number_of_mps_macs);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 /* ok, now we can go and tell our daemon the control address of MPS */
417 send_set_mps_ctrl_addr(tlvs, mpc);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 tlvs = copy_macs(mpc, mac_addr, tlvs, number_of_mps_macs, mpoa_device_type);
420 if (tlvs == NULL) return;
421 }
422 if (end_of_tlvs - tlvs != 0)
423 printk("mpoa: (%s) lane2_assoc_ind: ignoring %Zd bytes of trailing TLV carbage\n",
424 dev->name, end_of_tlvs - tlvs);
425 return;
426}
427
428/*
429 * Store at least advertizing router's MAC address
430 * plus the possible MAC address(es) to mpc->mps_macs.
431 * For a freshly allocated MPOA client mpc->mps_macs == 0.
432 */
433static uint8_t *copy_macs(struct mpoa_client *mpc, uint8_t *router_mac,
434 uint8_t *tlvs, uint8_t mps_macs, uint8_t device_type)
435{
436 int num_macs;
437 num_macs = (mps_macs > 1) ? mps_macs : 1;
438
439 if (mpc->number_of_mps_macs != num_macs) { /* need to reallocate? */
440 if (mpc->number_of_mps_macs != 0) kfree(mpc->mps_macs);
441 mpc->number_of_mps_macs = 0;
442 mpc->mps_macs = kmalloc(num_macs*ETH_ALEN, GFP_KERNEL);
443 if (mpc->mps_macs == NULL) {
444 printk("mpoa: (%s) copy_macs: out of mem\n", mpc->dev->name);
445 return NULL;
446 }
447 }
448 memcpy(mpc->mps_macs, router_mac, ETH_ALEN);
449 tlvs += 20; if (device_type == MPS_AND_MPC) tlvs += 20;
450 if (mps_macs > 0)
451 memcpy(mpc->mps_macs, tlvs, mps_macs*ETH_ALEN);
452 tlvs += mps_macs*ETH_ALEN;
453 mpc->number_of_mps_macs = num_macs;
454
455 return tlvs;
456}
457
458static int send_via_shortcut(struct sk_buff *skb, struct mpoa_client *mpc)
459{
460 in_cache_entry *entry;
461 struct iphdr *iph;
462 char *buff;
Al Viro30d492d2006-11-14 21:11:29 -0800463 __be32 ipaddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 static struct {
466 struct llc_snap_hdr hdr;
Al Viro30d492d2006-11-14 21:11:29 -0800467 __be32 tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 } tagged_llc_snap_hdr = {
469 {0xaa, 0xaa, 0x03, {0x00, 0x00, 0x00}, {0x88, 0x4c}},
470 0
471 };
472
473 buff = skb->data + mpc->dev->hard_header_len;
474 iph = (struct iphdr *)buff;
475 ipaddr = iph->daddr;
476
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900477 ddprintk("mpoa: (%s) send_via_shortcut: ipaddr 0x%x\n", mpc->dev->name, ipaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 entry = mpc->in_ops->get(ipaddr, mpc);
480 if (entry == NULL) {
481 entry = mpc->in_ops->add_entry(ipaddr, mpc);
482 if (entry != NULL) mpc->in_ops->put(entry);
483 return 1;
484 }
485 if (mpc->in_ops->cache_hit(entry, mpc) != OPEN){ /* threshold not exceeded or VCC not ready */
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900486 ddprintk("mpoa: (%s) send_via_shortcut: cache_hit: returns != OPEN\n", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 mpc->in_ops->put(entry);
488 return 1;
489 }
490
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900491 ddprintk("mpoa: (%s) send_via_shortcut: using shortcut\n", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /* MPOA spec A.1.4, MPOA client must decrement IP ttl at least by one */
493 if (iph->ttl <= 1) {
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900494 ddprintk("mpoa: (%s) send_via_shortcut: IP ttl = %u, using LANE\n", mpc->dev->name, iph->ttl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 mpc->in_ops->put(entry);
496 return 1;
497 }
498 iph->ttl--;
499 iph->check = 0;
500 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
501
502 if (entry->ctrl_info.tag != 0) {
503 ddprintk("mpoa: (%s) send_via_shortcut: adding tag 0x%x\n", mpc->dev->name, entry->ctrl_info.tag);
504 tagged_llc_snap_hdr.tag = entry->ctrl_info.tag;
505 skb_pull(skb, ETH_HLEN); /* get rid of Eth header */
506 skb_push(skb, sizeof(tagged_llc_snap_hdr)); /* add LLC/SNAP header */
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300507 skb_copy_to_linear_data(skb, &tagged_llc_snap_hdr,
508 sizeof(tagged_llc_snap_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 } else {
510 skb_pull(skb, ETH_HLEN); /* get rid of Eth header */
511 skb_push(skb, sizeof(struct llc_snap_hdr)); /* add LLC/SNAP header + tag */
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300512 skb_copy_to_linear_data(skb, &llc_snap_mpoa_data,
513 sizeof(struct llc_snap_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
516 atomic_add(skb->truesize, &sk_atm(entry->shortcut)->sk_wmem_alloc);
517 ATM_SKB(skb)->atm_options = entry->shortcut->atm_options;
518 entry->shortcut->send(entry->shortcut, skb);
519 entry->packets_fwded++;
520 mpc->in_ops->put(entry);
521
522 return 0;
523}
524
525/*
526 * Probably needs some error checks and locking, not sure...
527 */
528static int mpc_send_packet(struct sk_buff *skb, struct net_device *dev)
529{
530 int retval;
531 struct mpoa_client *mpc;
532 struct ethhdr *eth;
533 int i = 0;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 mpc = find_mpc_by_lec(dev); /* this should NEVER fail */
536 if(mpc == NULL) {
537 printk("mpoa: (%s) mpc_send_packet: no MPC found\n", dev->name);
538 goto non_ip;
539 }
540
541 eth = (struct ethhdr *)skb->data;
542 if (eth->h_proto != htons(ETH_P_IP))
543 goto non_ip; /* Multi-Protocol Over ATM :-) */
544
545 while (i < mpc->number_of_mps_macs) {
Kris Katterjohnd3f4a682006-01-09 16:01:43 -0800546 if (!compare_ether_addr(eth->h_dest, (mpc->mps_macs + i*ETH_ALEN)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if ( send_via_shortcut(skb, mpc) == 0 ) /* try shortcut */
548 return 0; /* success! */
549 i++;
550 }
551
552 non_ip:
553 retval = mpc->old_hard_start_xmit(skb,dev);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return retval;
556}
557
558static int atm_mpoa_vcc_attach(struct atm_vcc *vcc, void __user *arg)
559{
560 int bytes_left;
561 struct mpoa_client *mpc;
562 struct atmmpc_ioc ioc_data;
563 in_cache_entry *in_entry;
Al Viro30d492d2006-11-14 21:11:29 -0800564 __be32 ipaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 bytes_left = copy_from_user(&ioc_data, arg, sizeof(struct atmmpc_ioc));
567 if (bytes_left != 0) {
568 printk("mpoa: mpc_vcc_attach: Short read (missed %d bytes) from userland\n", bytes_left);
569 return -EFAULT;
570 }
571 ipaddr = ioc_data.ipaddr;
572 if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF)
573 return -EINVAL;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 mpc = find_mpc_by_itfnum(ioc_data.dev_num);
576 if (mpc == NULL)
577 return -EINVAL;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (ioc_data.type == MPC_SOCKET_INGRESS) {
580 in_entry = mpc->in_ops->get(ipaddr, mpc);
581 if (in_entry == NULL || in_entry->entry_state < INGRESS_RESOLVED) {
582 printk("mpoa: (%s) mpc_vcc_attach: did not find RESOLVED entry from ingress cache\n",
583 mpc->dev->name);
584 if (in_entry != NULL) mpc->in_ops->put(in_entry);
585 return -EINVAL;
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 printk("mpoa: (%s) mpc_vcc_attach: attaching ingress SVC, entry = %u.%u.%u.%u\n",
Al Virob4229932006-09-26 21:23:43 -0700588 mpc->dev->name, NIPQUAD(in_entry->ctrl_info.in_dst_ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 in_entry->shortcut = vcc;
590 mpc->in_ops->put(in_entry);
591 } else {
592 printk("mpoa: (%s) mpc_vcc_attach: attaching egress SVC\n", mpc->dev->name);
593 }
594
595 vcc->proto_data = mpc->dev;
596 vcc->push = mpc_push;
597
598 return 0;
599}
600
601/*
602 *
603 */
604static void mpc_vcc_close(struct atm_vcc *vcc, struct net_device *dev)
605{
606 struct mpoa_client *mpc;
607 in_cache_entry *in_entry;
608 eg_cache_entry *eg_entry;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 mpc = find_mpc_by_lec(dev);
611 if (mpc == NULL) {
612 printk("mpoa: (%s) mpc_vcc_close: close for unknown MPC\n", dev->name);
613 return;
614 }
615
616 dprintk("mpoa: (%s) mpc_vcc_close:\n", dev->name);
617 in_entry = mpc->in_ops->get_by_vcc(vcc, mpc);
618 if (in_entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 dprintk("mpoa: (%s) mpc_vcc_close: ingress SVC closed ip = %u.%u.%u.%u\n",
Al Virob4229932006-09-26 21:23:43 -0700620 mpc->dev->name, NIPQUAD(in_entry->ctrl_info.in_dst_ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 in_entry->shortcut = NULL;
622 mpc->in_ops->put(in_entry);
623 }
624 eg_entry = mpc->eg_ops->get_by_vcc(vcc, mpc);
625 if (eg_entry) {
626 dprintk("mpoa: (%s) mpc_vcc_close: egress SVC closed\n", mpc->dev->name);
627 eg_entry->shortcut = NULL;
628 mpc->eg_ops->put(eg_entry);
629 }
630
631 if (in_entry == NULL && eg_entry == NULL)
632 dprintk("mpoa: (%s) mpc_vcc_close: unused vcc closed\n", dev->name);
633
634 return;
635}
636
637static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb)
638{
639 struct net_device *dev = (struct net_device *)vcc->proto_data;
640 struct sk_buff *new_skb;
641 eg_cache_entry *eg;
642 struct mpoa_client *mpc;
Al Viro30d492d2006-11-14 21:11:29 -0800643 __be32 tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 char *tmp;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 ddprintk("mpoa: (%s) mpc_push:\n", dev->name);
647 if (skb == NULL) {
648 dprintk("mpoa: (%s) mpc_push: null skb, closing VCC\n", dev->name);
649 mpc_vcc_close(vcc, dev);
650 return;
651 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 skb->dev = dev;
654 if (memcmp(skb->data, &llc_snap_mpoa_ctrl, sizeof(struct llc_snap_hdr)) == 0) {
655 struct sock *sk = sk_atm(vcc);
656
657 dprintk("mpoa: (%s) mpc_push: control packet arrived\n", dev->name);
658 /* Pass control packets to daemon */
659 skb_queue_tail(&sk->sk_receive_queue, skb);
660 sk->sk_data_ready(sk, skb->len);
661 return;
662 }
663
664 /* data coming over the shortcut */
665 atm_return(vcc, skb->truesize);
666
667 mpc = find_mpc_by_lec(dev);
668 if (mpc == NULL) {
669 printk("mpoa: (%s) mpc_push: unknown MPC\n", dev->name);
670 return;
671 }
672
673 if (memcmp(skb->data, &llc_snap_mpoa_data_tagged, sizeof(struct llc_snap_hdr)) == 0) { /* MPOA tagged data */
674 ddprintk("mpoa: (%s) mpc_push: tagged data packet arrived\n", dev->name);
675
676 } else if (memcmp(skb->data, &llc_snap_mpoa_data, sizeof(struct llc_snap_hdr)) == 0) { /* MPOA data */
677 printk("mpoa: (%s) mpc_push: non-tagged data packet arrived\n", dev->name);
678 printk(" mpc_push: non-tagged data unsupported, purging\n");
679 dev_kfree_skb_any(skb);
680 return;
681 } else {
682 printk("mpoa: (%s) mpc_push: garbage arrived, purging\n", dev->name);
683 dev_kfree_skb_any(skb);
684 return;
685 }
686
687 tmp = skb->data + sizeof(struct llc_snap_hdr);
Al Viro30d492d2006-11-14 21:11:29 -0800688 tag = *(__be32 *)tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 eg = mpc->eg_ops->get_by_tag(tag, mpc);
691 if (eg == NULL) {
692 printk("mpoa: (%s) mpc_push: Didn't find egress cache entry, tag = %u\n",
693 dev->name,tag);
694 purge_egress_shortcut(vcc, NULL);
695 dev_kfree_skb_any(skb);
696 return;
697 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /*
700 * See if ingress MPC is using shortcut we opened as a return channel.
701 * This means we have a bi-directional vcc opened by us.
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900702 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (eg->shortcut == NULL) {
704 eg->shortcut = vcc;
705 printk("mpoa: (%s) mpc_push: egress SVC in use\n", dev->name);
706 }
707
708 skb_pull(skb, sizeof(struct llc_snap_hdr) + sizeof(tag)); /* get rid of LLC/SNAP header */
709 new_skb = skb_realloc_headroom(skb, eg->ctrl_info.DH_length); /* LLC/SNAP is shorter than MAC header :( */
710 dev_kfree_skb_any(skb);
711 if (new_skb == NULL){
712 mpc->eg_ops->put(eg);
713 return;
714 }
715 skb_push(new_skb, eg->ctrl_info.DH_length); /* add MAC header */
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300716 skb_copy_to_linear_data(new_skb, eg->ctrl_info.DLL_header,
717 eg->ctrl_info.DH_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 new_skb->protocol = eth_type_trans(new_skb, dev);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700719 skb_reset_network_header(new_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700721 eg->latest_ip_addr = ip_hdr(new_skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 eg->packets_rcvd++;
723 mpc->eg_ops->put(eg);
724
725 memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data));
726 netif_rx(new_skb);
727
728 return;
729}
730
731static struct atmdev_ops mpc_ops = { /* only send is required */
732 .close = mpoad_close,
733 .send = msg_from_mpoad
734};
735
736static struct atm_dev mpc_dev = {
737 .ops = &mpc_ops,
738 .type = "mpc",
739 .number = 42,
Milind Arun Choudhary4ef8d0a2007-04-26 01:37:44 -0700740 .lock = __SPIN_LOCK_UNLOCKED(mpc_dev.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /* members not explicitly initialised will be 0 */
742};
743
744static int atm_mpoa_mpoad_attach (struct atm_vcc *vcc, int arg)
745{
746 struct mpoa_client *mpc;
747 struct lec_priv *priv;
748 int err;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (mpcs == NULL) {
751 init_timer(&mpc_timer);
752 mpc_timer_refresh();
753
754 /* This lets us now how our LECs are doing */
755 err = register_netdevice_notifier(&mpoa_notifier);
756 if (err < 0) {
757 del_timer(&mpc_timer);
758 return err;
759 }
760 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 mpc = find_mpc_by_itfnum(arg);
763 if (mpc == NULL) {
764 dprintk("mpoa: mpoad_attach: allocating new mpc for itf %d\n", arg);
765 mpc = alloc_mpc();
766 if (mpc == NULL)
767 return -ENOMEM;
768 mpc->dev_num = arg;
769 mpc->dev = find_lec_by_itfnum(arg); /* NULL if there was no lec */
770 }
771 if (mpc->mpoad_vcc) {
772 printk("mpoa: mpoad_attach: mpoad is already present for itf %d\n", arg);
773 return -EADDRINUSE;
774 }
775
776 if (mpc->dev) { /* check if the lec is LANE2 capable */
777 priv = (struct lec_priv *)mpc->dev->priv;
778 if (priv->lane_version < 2) {
779 dev_put(mpc->dev);
780 mpc->dev = NULL;
781 } else
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900782 priv->lane2_ops->associate_indicator = lane2_assoc_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
785 mpc->mpoad_vcc = vcc;
786 vcc->dev = &mpc_dev;
787 vcc_insert_socket(sk_atm(vcc));
788 set_bit(ATM_VF_META,&vcc->flags);
789 set_bit(ATM_VF_READY,&vcc->flags);
790
791 if (mpc->dev) {
792 char empty[ATM_ESA_LEN];
793 memset(empty, 0, ATM_ESA_LEN);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 start_mpc(mpc, mpc->dev);
796 /* set address if mpcd e.g. gets killed and restarted.
797 * If we do not do it now we have to wait for the next LE_ARP
798 */
799 if ( memcmp(mpc->mps_ctrl_addr, empty, ATM_ESA_LEN) != 0 )
800 send_set_mps_ctrl_addr(mpc->mps_ctrl_addr, mpc);
801 }
802
803 __module_get(THIS_MODULE);
804 return arg;
805}
806
807static void send_set_mps_ctrl_addr(char *addr, struct mpoa_client *mpc)
808{
809 struct k_message mesg;
810
811 memcpy (mpc->mps_ctrl_addr, addr, ATM_ESA_LEN);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 mesg.type = SET_MPS_CTRL_ADDR;
814 memcpy(mesg.MPS_ctrl, addr, ATM_ESA_LEN);
815 msg_to_mpoad(&mesg, mpc);
816
817 return;
818}
819
820static void mpoad_close(struct atm_vcc *vcc)
821{
822 struct mpoa_client *mpc;
823 struct sk_buff *skb;
824
825 mpc = find_mpc_by_vcc(vcc);
826 if (mpc == NULL) {
827 printk("mpoa: mpoad_close: did not find MPC\n");
828 return;
829 }
830 if (!mpc->mpoad_vcc) {
831 printk("mpoa: mpoad_close: close for non-present mpoad\n");
832 return;
833 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 mpc->mpoad_vcc = NULL;
836 if (mpc->dev) {
837 struct lec_priv *priv = (struct lec_priv *)mpc->dev->priv;
838 priv->lane2_ops->associate_indicator = NULL;
839 stop_mpc(mpc);
840 dev_put(mpc->dev);
841 }
842
843 mpc->in_ops->destroy_cache(mpc);
844 mpc->eg_ops->destroy_cache(mpc);
845
846 while ((skb = skb_dequeue(&sk_atm(vcc)->sk_receive_queue))) {
847 atm_return(vcc, skb->truesize);
848 kfree_skb(skb);
849 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 printk("mpoa: (%s) going down\n",
852 (mpc->dev) ? mpc->dev->name : "<unknown>");
853 module_put(THIS_MODULE);
854
855 return;
856}
857
858/*
859 *
860 */
861static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb)
862{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 struct mpoa_client *mpc = find_mpc_by_vcc(vcc);
865 struct k_message *mesg = (struct k_message*)skb->data;
866 atomic_sub(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (mpc == NULL) {
869 printk("mpoa: msg_from_mpoad: no mpc found\n");
870 return 0;
871 }
872 dprintk("mpoa: (%s) msg_from_mpoad:", (mpc->dev) ? mpc->dev->name : "<unknown>");
873 switch(mesg->type) {
874 case MPOA_RES_REPLY_RCVD:
875 dprintk(" mpoa_res_reply_rcvd\n");
876 MPOA_res_reply_rcvd(mesg, mpc);
877 break;
878 case MPOA_TRIGGER_RCVD:
879 dprintk(" mpoa_trigger_rcvd\n");
880 MPOA_trigger_rcvd(mesg, mpc);
881 break;
882 case INGRESS_PURGE_RCVD:
883 dprintk(" nhrp_purge_rcvd\n");
884 ingress_purge_rcvd(mesg, mpc);
885 break;
886 case EGRESS_PURGE_RCVD:
887 dprintk(" egress_purge_reply_rcvd\n");
888 egress_purge_rcvd(mesg, mpc);
889 break;
890 case MPS_DEATH:
891 dprintk(" mps_death\n");
892 mps_death(mesg, mpc);
893 break;
894 case CACHE_IMPOS_RCVD:
895 dprintk(" cache_impos_rcvd\n");
896 MPOA_cache_impos_rcvd(mesg, mpc);
897 break;
898 case SET_MPC_CTRL_ADDR:
899 dprintk(" set_mpc_ctrl_addr\n");
900 set_mpc_ctrl_addr_rcvd(mesg, mpc);
901 break;
902 case SET_MPS_MAC_ADDR:
903 dprintk(" set_mps_mac_addr\n");
904 set_mps_mac_addr_rcvd(mesg, mpc);
905 break;
906 case CLEAN_UP_AND_EXIT:
907 dprintk(" clean_up_and_exit\n");
908 clean_up(mesg, mpc, DIE);
909 break;
910 case RELOAD:
911 dprintk(" reload\n");
912 clean_up(mesg, mpc, RELOAD);
913 break;
914 case SET_MPC_PARAMS:
915 dprintk(" set_mpc_params\n");
916 mpc->parameters = mesg->content.params;
917 break;
918 default:
919 dprintk(" unknown message %d\n", mesg->type);
920 break;
921 }
922 kfree_skb(skb);
923
924 return 0;
925}
926
927/* Remember that this function may not do things that sleep */
928int msg_to_mpoad(struct k_message *mesg, struct mpoa_client *mpc)
929{
930 struct sk_buff *skb;
931 struct sock *sk;
932
933 if (mpc == NULL || !mpc->mpoad_vcc) {
934 printk("mpoa: msg_to_mpoad: mesg %d to a non-existent mpoad\n", mesg->type);
935 return -ENXIO;
936 }
937
938 skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
939 if (skb == NULL)
940 return -ENOMEM;
941 skb_put(skb, sizeof(struct k_message));
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300942 skb_copy_to_linear_data(skb, mesg, sizeof(*mesg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 atm_force_charge(mpc->mpoad_vcc, skb->truesize);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 sk = sk_atm(mpc->mpoad_vcc);
946 skb_queue_tail(&sk->sk_receive_queue, skb);
947 sk->sk_data_ready(sk, skb->len);
948
949 return 0;
950}
951
952static int mpoa_event_listener(struct notifier_block *mpoa_notifier, unsigned long event, void *dev_ptr)
953{
954 struct net_device *dev;
955 struct mpoa_client *mpc;
956 struct lec_priv *priv;
957
958 dev = (struct net_device *)dev_ptr;
959 if (dev->name == NULL || strncmp(dev->name, "lec", 3))
960 return NOTIFY_DONE; /* we are only interested in lec:s */
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 switch (event) {
963 case NETDEV_REGISTER: /* a new lec device was allocated */
964 priv = (struct lec_priv *)dev->priv;
965 if (priv->lane_version < 2)
966 break;
967 priv->lane2_ops->associate_indicator = lane2_assoc_ind;
968 mpc = find_mpc_by_itfnum(priv->itfnum);
969 if (mpc == NULL) {
970 dprintk("mpoa: mpoa_event_listener: allocating new mpc for %s\n",
971 dev->name);
972 mpc = alloc_mpc();
973 if (mpc == NULL) {
974 printk("mpoa: mpoa_event_listener: no new mpc");
975 break;
976 }
977 }
978 mpc->dev_num = priv->itfnum;
979 mpc->dev = dev;
980 dev_hold(dev);
981 dprintk("mpoa: (%s) was initialized\n", dev->name);
982 break;
983 case NETDEV_UNREGISTER:
984 /* the lec device was deallocated */
985 mpc = find_mpc_by_lec(dev);
986 if (mpc == NULL)
987 break;
988 dprintk("mpoa: device (%s) was deallocated\n", dev->name);
989 stop_mpc(mpc);
990 dev_put(mpc->dev);
991 mpc->dev = NULL;
992 break;
993 case NETDEV_UP:
994 /* the dev was ifconfig'ed up */
995 mpc = find_mpc_by_lec(dev);
996 if (mpc == NULL)
997 break;
998 if (mpc->mpoad_vcc != NULL) {
999 start_mpc(mpc, dev);
1000 }
1001 break;
1002 case NETDEV_DOWN:
1003 /* the dev was ifconfig'ed down */
1004 /* this means that the flow of packets from the
1005 * upper layer stops
1006 */
1007 mpc = find_mpc_by_lec(dev);
1008 if (mpc == NULL)
1009 break;
1010 if (mpc->mpoad_vcc != NULL) {
1011 stop_mpc(mpc);
1012 }
1013 break;
1014 case NETDEV_REBOOT:
1015 case NETDEV_CHANGE:
1016 case NETDEV_CHANGEMTU:
1017 case NETDEV_CHANGEADDR:
1018 case NETDEV_GOING_DOWN:
1019 break;
1020 default:
1021 break;
1022 }
1023
1024 return NOTIFY_DONE;
1025}
1026
1027/*
1028 * Functions which are called after a message is received from mpcd.
1029 * Msg is reused on purpose.
1030 */
1031
1032
1033static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1034{
Al Viro30d492d2006-11-14 21:11:29 -08001035 __be32 dst_ip = msg->content.in_info.in_dst_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 in_cache_entry *entry;
1037
1038 entry = mpc->in_ops->get(dst_ip, mpc);
1039 if(entry == NULL){
1040 entry = mpc->in_ops->add_entry(dst_ip, mpc);
1041 entry->entry_state = INGRESS_RESOLVING;
1042 msg->type = SND_MPOA_RES_RQST;
1043 msg->content.in_info = entry->ctrl_info;
1044 msg_to_mpoad(msg, mpc);
1045 do_gettimeofday(&(entry->reply_wait));
1046 mpc->in_ops->put(entry);
1047 return;
1048 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if(entry->entry_state == INGRESS_INVALID){
1051 entry->entry_state = INGRESS_RESOLVING;
1052 msg->type = SND_MPOA_RES_RQST;
1053 msg->content.in_info = entry->ctrl_info;
1054 msg_to_mpoad(msg, mpc);
1055 do_gettimeofday(&(entry->reply_wait));
1056 mpc->in_ops->put(entry);
1057 return;
1058 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 printk("mpoa: (%s) MPOA_trigger_rcvd: entry already in resolving state\n",
1061 (mpc->dev) ? mpc->dev->name : "<unknown>");
1062 mpc->in_ops->put(entry);
1063 return;
1064}
1065
1066/*
1067 * Things get complicated because we have to check if there's an egress
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001068 * shortcut with suitable traffic parameters we could use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
1070static void check_qos_and_open_shortcut(struct k_message *msg, struct mpoa_client *client, in_cache_entry *entry)
1071{
Al Viro30d492d2006-11-14 21:11:29 -08001072 __be32 dst_ip = msg->content.in_info.in_dst_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 struct atm_mpoa_qos *qos = atm_mpoa_search_qos(dst_ip);
1074 eg_cache_entry *eg_entry = client->eg_ops->get_by_src_ip(dst_ip, client);
1075
1076 if(eg_entry && eg_entry->shortcut){
1077 if(eg_entry->shortcut->qos.txtp.traffic_class &
1078 msg->qos.txtp.traffic_class &
1079 (qos ? qos->qos.txtp.traffic_class : ATM_UBR | ATM_CBR)){
1080 if(eg_entry->shortcut->qos.txtp.traffic_class == ATM_UBR)
1081 entry->shortcut = eg_entry->shortcut;
1082 else if(eg_entry->shortcut->qos.txtp.max_pcr > 0)
1083 entry->shortcut = eg_entry->shortcut;
1084 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001085 if(entry->shortcut){
Al Viroff7512e2006-06-20 03:27:27 -07001086 dprintk("mpoa: (%s) using egress SVC to reach %u.%u.%u.%u\n",client->dev->name, NIPQUAD(dst_ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 client->eg_ops->put(eg_entry);
1088 return;
1089 }
1090 }
1091 if (eg_entry != NULL)
1092 client->eg_ops->put(eg_entry);
1093
1094 /* No luck in the egress cache we must open an ingress SVC */
1095 msg->type = OPEN_INGRESS_SVC;
1096 if (qos && (qos->qos.txtp.traffic_class == msg->qos.txtp.traffic_class))
1097 {
1098 msg->qos = qos->qos;
1099 printk("mpoa: (%s) trying to get a CBR shortcut\n",client->dev->name);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 else memset(&msg->qos,0,sizeof(struct atm_qos));
1102 msg_to_mpoad(msg, client);
1103 return;
1104}
1105
1106static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1107{
Al Viro30d492d2006-11-14 21:11:29 -08001108 __be32 dst_ip = msg->content.in_info.in_dst_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 in_cache_entry *entry = mpc->in_ops->get(dst_ip, mpc);
Andrew Morton1e4fd512006-06-26 00:01:58 -07001110
Al Viroff7512e2006-06-20 03:27:27 -07001111 dprintk("mpoa: (%s) MPOA_res_reply_rcvd: ip %u.%u.%u.%u\n", mpc->dev->name, NIPQUAD(dst_ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 ddprintk("mpoa: (%s) MPOA_res_reply_rcvd() entry = %p", mpc->dev->name, entry);
1113 if(entry == NULL){
1114 printk("\nmpoa: (%s) ARGH, received res. reply for an entry that doesn't exist.\n", mpc->dev->name);
1115 return;
1116 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001117 ddprintk(" entry_state = %d ", entry->entry_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 if (entry->entry_state == INGRESS_RESOLVED) {
1120 printk("\nmpoa: (%s) MPOA_res_reply_rcvd for RESOLVED entry!\n", mpc->dev->name);
1121 mpc->in_ops->put(entry);
1122 return;
1123 }
1124
1125 entry->ctrl_info = msg->content.in_info;
1126 do_gettimeofday(&(entry->tv));
1127 do_gettimeofday(&(entry->reply_wait)); /* Used in refreshing func from now on */
1128 entry->refresh_time = 0;
1129 ddprintk("entry->shortcut = %p\n", entry->shortcut);
1130
1131 if(entry->entry_state == INGRESS_RESOLVING && entry->shortcut != NULL){
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001132 entry->entry_state = INGRESS_RESOLVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 mpc->in_ops->put(entry);
1134 return; /* Shortcut already open... */
1135 }
1136
1137 if (entry->shortcut != NULL) {
1138 printk("mpoa: (%s) MPOA_res_reply_rcvd: entry->shortcut != NULL, impossible!\n",
1139 mpc->dev->name);
1140 mpc->in_ops->put(entry);
1141 return;
1142 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 check_qos_and_open_shortcut(msg, mpc, entry);
1145 entry->entry_state = INGRESS_RESOLVED;
1146 mpc->in_ops->put(entry);
1147
1148 return;
1149
1150}
1151
1152static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1153{
Al Viro30d492d2006-11-14 21:11:29 -08001154 __be32 dst_ip = msg->content.in_info.in_dst_ip;
1155 __be32 mask = msg->ip_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 in_cache_entry *entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1157
1158 if(entry == NULL){
1159 printk("mpoa: (%s) ingress_purge_rcvd: purge for a non-existing entry, ", mpc->dev->name);
Al Virob4229932006-09-26 21:23:43 -07001160 printk("ip = %u.%u.%u.%u\n", NIPQUAD(dst_ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return;
1162 }
1163
1164 do {
1165 dprintk("mpoa: (%s) ingress_purge_rcvd: removing an ingress entry, ip = %u.%u.%u.%u\n" ,
Al Virob4229932006-09-26 21:23:43 -07001166 mpc->dev->name, NIPQUAD(dst_ip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 write_lock_bh(&mpc->ingress_lock);
1168 mpc->in_ops->remove_entry(entry, mpc);
1169 write_unlock_bh(&mpc->ingress_lock);
1170 mpc->in_ops->put(entry);
1171 entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1172 } while (entry != NULL);
1173
1174 return;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001175}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1178{
Al Viro30d492d2006-11-14 21:11:29 -08001179 __be32 cache_id = msg->content.eg_info.cache_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(cache_id, mpc);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (entry == NULL) {
1183 dprintk("mpoa: (%s) egress_purge_rcvd: purge for a non-existing entry\n", mpc->dev->name);
1184 return;
1185 }
1186
1187 write_lock_irq(&mpc->egress_lock);
1188 mpc->eg_ops->remove_entry(entry, mpc);
1189 write_unlock_irq(&mpc->egress_lock);
1190
1191 mpc->eg_ops->put(entry);
1192
1193 return;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001194}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry)
1197{
1198 struct sock *sk;
1199 struct k_message *purge_msg;
1200 struct sk_buff *skb;
1201
1202 dprintk("mpoa: purge_egress_shortcut: entering\n");
1203 if (vcc == NULL) {
1204 printk("mpoa: purge_egress_shortcut: vcc == NULL\n");
1205 return;
1206 }
1207
1208 skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
1209 if (skb == NULL) {
1210 printk("mpoa: purge_egress_shortcut: out of memory\n");
1211 return;
1212 }
1213
1214 skb_put(skb, sizeof(struct k_message));
1215 memset(skb->data, 0, sizeof(struct k_message));
1216 purge_msg = (struct k_message *)skb->data;
1217 purge_msg->type = DATA_PLANE_PURGE;
1218 if (entry != NULL)
1219 purge_msg->content.eg_info = entry->ctrl_info;
1220
1221 atm_force_charge(vcc, skb->truesize);
1222
1223 sk = sk_atm(vcc);
1224 skb_queue_tail(&sk->sk_receive_queue, skb);
1225 sk->sk_data_ready(sk, skb->len);
1226 dprintk("mpoa: purge_egress_shortcut: exiting:\n");
1227
1228 return;
1229}
1230
1231/*
1232 * Our MPS died. Tell our daemon to send NHRP data plane purge to each
1233 * of the egress shortcuts we have.
1234 */
1235static void mps_death( struct k_message * msg, struct mpoa_client * mpc )
1236{
1237 eg_cache_entry *entry;
1238
1239 dprintk("mpoa: (%s) mps_death:\n", mpc->dev->name);
1240
1241 if(memcmp(msg->MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN)){
1242 printk("mpoa: (%s) mps_death: wrong MPS\n", mpc->dev->name);
1243 return;
1244 }
1245
1246 /* FIXME: This knows too much of the cache structure */
1247 read_lock_irq(&mpc->egress_lock);
1248 entry = mpc->eg_cache;
1249 while (entry != NULL) {
1250 purge_egress_shortcut(entry->shortcut, entry);
1251 entry = entry->next;
1252 }
1253 read_unlock_irq(&mpc->egress_lock);
1254
1255 mpc->in_ops->destroy_cache(mpc);
1256 mpc->eg_ops->destroy_cache(mpc);
1257
1258 return;
1259}
1260
1261static void MPOA_cache_impos_rcvd( struct k_message * msg, struct mpoa_client * mpc)
1262{
1263 uint16_t holding_time;
1264 eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(msg->content.eg_info.cache_id, mpc);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 holding_time = msg->content.eg_info.holding_time;
1267 dprintk("mpoa: (%s) MPOA_cache_impos_rcvd: entry = %p, holding_time = %u\n",
1268 mpc->dev->name, entry, holding_time);
1269 if(entry == NULL && holding_time) {
1270 entry = mpc->eg_ops->add_entry(msg, mpc);
1271 mpc->eg_ops->put(entry);
1272 return;
1273 }
1274 if(holding_time){
1275 mpc->eg_ops->update(entry, holding_time);
1276 return;
1277 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 write_lock_irq(&mpc->egress_lock);
1280 mpc->eg_ops->remove_entry(entry, mpc);
1281 write_unlock_irq(&mpc->egress_lock);
1282
1283 mpc->eg_ops->put(entry);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return;
1286}
1287
1288static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg, struct mpoa_client *mpc)
1289{
1290 struct lec_priv *priv;
1291 int i, retval ;
1292
1293 uint8_t tlv[4 + 1 + 1 + 1 + ATM_ESA_LEN];
1294
1295 tlv[0] = 00; tlv[1] = 0xa0; tlv[2] = 0x3e; tlv[3] = 0x2a; /* type */
1296 tlv[4] = 1 + 1 + ATM_ESA_LEN; /* length */
1297 tlv[5] = 0x02; /* MPOA client */
1298 tlv[6] = 0x00; /* number of MPS MAC addresses */
1299
1300 memcpy(&tlv[7], mesg->MPS_ctrl, ATM_ESA_LEN); /* MPC ctrl ATM addr */
1301 memcpy(mpc->our_ctrl_addr, mesg->MPS_ctrl, ATM_ESA_LEN);
1302
1303 dprintk("mpoa: (%s) setting MPC ctrl ATM address to ",
1304 (mpc->dev) ? mpc->dev->name : "<unknown>");
1305 for (i = 7; i < sizeof(tlv); i++)
1306 dprintk("%02x ", tlv[i]);
1307 dprintk("\n");
1308
1309 if (mpc->dev) {
1310 priv = (struct lec_priv *)mpc->dev->priv;
1311 retval = priv->lane2_ops->associate_req(mpc->dev, mpc->dev->dev_addr, tlv, sizeof(tlv));
1312 if (retval == 0)
1313 printk("mpoa: (%s) MPOA device type TLV association failed\n", mpc->dev->name);
1314 retval = priv->lane2_ops->resolve(mpc->dev, NULL, 1, NULL, NULL);
1315 if (retval < 0)
1316 printk("mpoa: (%s) targetless LE_ARP request failed\n", mpc->dev->name);
1317 }
1318
1319 return;
1320}
1321
1322static void set_mps_mac_addr_rcvd(struct k_message *msg, struct mpoa_client *client)
1323{
1324
1325 if(client->number_of_mps_macs)
1326 kfree(client->mps_macs);
1327 client->number_of_mps_macs = 0;
Arnaldo Carvalho de Melo2afe37c2006-11-21 01:14:33 -02001328 client->mps_macs = kmemdup(msg->MPS_ctrl, ETH_ALEN, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (client->mps_macs == NULL) {
1330 printk("mpoa: set_mps_mac_addr_rcvd: out of memory\n");
1331 return;
1332 }
1333 client->number_of_mps_macs = 1;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return;
1336}
1337
1338/*
1339 * purge egress cache and tell daemon to 'action' (DIE, RELOAD)
1340 */
1341static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action)
1342{
1343
1344 eg_cache_entry *entry;
1345 msg->type = SND_EGRESS_PURGE;
1346
1347
1348 /* FIXME: This knows too much of the cache structure */
1349 read_lock_irq(&mpc->egress_lock);
1350 entry = mpc->eg_cache;
1351 while (entry != NULL){
1352 msg->content.eg_info = entry->ctrl_info;
1353 dprintk("mpoa: cache_id %u\n", entry->ctrl_info.cache_id);
1354 msg_to_mpoad(msg, mpc);
1355 entry = entry->next;
1356 }
1357 read_unlock_irq(&mpc->egress_lock);
1358
1359 msg->type = action;
1360 msg_to_mpoad(msg, mpc);
1361 return;
1362}
1363
1364static void mpc_timer_refresh(void)
1365{
1366 mpc_timer.expires = jiffies + (MPC_P2 * HZ);
1367 mpc_timer.data = mpc_timer.expires;
1368 mpc_timer.function = mpc_cache_check;
1369 add_timer(&mpc_timer);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return;
1372}
1373
1374static void mpc_cache_check( unsigned long checking_time )
1375{
1376 struct mpoa_client *mpc = mpcs;
1377 static unsigned long previous_resolving_check_time;
1378 static unsigned long previous_refresh_time;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 while( mpc != NULL ){
1381 mpc->in_ops->clear_count(mpc);
1382 mpc->eg_ops->clear_expired(mpc);
1383 if(checking_time - previous_resolving_check_time > mpc->parameters.mpc_p4 * HZ ){
1384 mpc->in_ops->check_resolving(mpc);
1385 previous_resolving_check_time = checking_time;
1386 }
1387 if(checking_time - previous_refresh_time > mpc->parameters.mpc_p5 * HZ ){
1388 mpc->in_ops->refresh(mpc);
1389 previous_refresh_time = checking_time;
1390 }
1391 mpc = mpc->next;
1392 }
1393 mpc_timer_refresh();
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return;
1396}
1397
1398static int atm_mpoa_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1399{
1400 int err = 0;
1401 struct atm_vcc *vcc = ATM_SD(sock);
1402
1403 if (cmd != ATMMPC_CTRL && cmd != ATMMPC_DATA)
1404 return -ENOIOCTLCMD;
1405
1406 if (!capable(CAP_NET_ADMIN))
1407 return -EPERM;
1408
1409 switch (cmd) {
1410 case ATMMPC_CTRL:
1411 err = atm_mpoa_mpoad_attach(vcc, (int)arg);
1412 if (err >= 0)
1413 sock->state = SS_CONNECTED;
1414 break;
1415 case ATMMPC_DATA:
1416 err = atm_mpoa_vcc_attach(vcc, (void __user *)arg);
1417 break;
1418 default:
1419 break;
1420 }
1421 return err;
1422}
1423
1424
1425static struct atm_ioctl atm_ioctl_ops = {
1426 .owner = THIS_MODULE,
1427 .ioctl = atm_mpoa_ioctl,
1428};
1429
1430static __init int atm_mpoa_init(void)
1431{
1432 register_atm_ioctl(&atm_ioctl_ops);
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 if (mpc_proc_init() != 0)
1435 printk(KERN_INFO "mpoa: failed to initialize /proc/mpoa\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 printk("mpc.c: " __DATE__ " " __TIME__ " initialized\n");
1438
1439 return 0;
1440}
1441
1442static void __exit atm_mpoa_cleanup(void)
1443{
1444 struct mpoa_client *mpc, *tmp;
1445 struct atm_mpoa_qos *qos, *nextqos;
1446 struct lec_priv *priv;
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 mpc_proc_clean();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 del_timer(&mpc_timer);
1451 unregister_netdevice_notifier(&mpoa_notifier);
1452 deregister_atm_ioctl(&atm_ioctl_ops);
1453
1454 mpc = mpcs;
1455 mpcs = NULL;
1456 while (mpc != NULL) {
1457 tmp = mpc->next;
1458 if (mpc->dev != NULL) {
1459 stop_mpc(mpc);
1460 priv = (struct lec_priv *)mpc->dev->priv;
1461 if (priv->lane2_ops != NULL)
1462 priv->lane2_ops->associate_indicator = NULL;
1463 }
1464 ddprintk("mpoa: cleanup_module: about to clear caches\n");
1465 mpc->in_ops->destroy_cache(mpc);
1466 mpc->eg_ops->destroy_cache(mpc);
1467 ddprintk("mpoa: cleanup_module: caches cleared\n");
1468 kfree(mpc->mps_macs);
1469 memset(mpc, 0, sizeof(struct mpoa_client));
1470 ddprintk("mpoa: cleanup_module: about to kfree %p\n", mpc);
1471 kfree(mpc);
1472 ddprintk("mpoa: cleanup_module: next mpc is at %p\n", tmp);
1473 mpc = tmp;
1474 }
1475
1476 qos = qos_head;
1477 qos_head = NULL;
1478 while (qos != NULL) {
1479 nextqos = qos->next;
1480 dprintk("mpoa: cleanup_module: freeing qos entry %p\n", qos);
1481 kfree(qos);
1482 qos = nextqos;
1483 }
1484
1485 return;
1486}
1487
1488module_init(atm_mpoa_init);
1489module_exit(atm_mpoa_cleanup);
1490
1491MODULE_LICENSE("GPL");