]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - net/atm/mpc.c
null_blk: remove duplicate check for report zone
[thirdparty/kernel/stable.git] / net / atm / mpc.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
99824461
JP
2#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
3
1da177e4
LT
4#include <linux/kernel.h>
5#include <linux/string.h>
5a0e3ad6 6#include <linux/slab.h>
1da177e4
LT
7#include <linux/timer.h>
8#include <linux/init.h>
9#include <linux/bitops.h>
4fc268d2 10#include <linux/capability.h>
1da177e4
LT
11#include <linux/seq_file.h>
12
13/* We are an ethernet device */
14#include <linux/if_ether.h>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
17#include <net/sock.h>
18#include <linux/skbuff.h>
19#include <linux/ip.h>
57100440 20#include <linux/uaccess.h>
1da177e4 21#include <asm/byteorder.h>
1da177e4
LT
22#include <net/checksum.h> /* for ip_fast_csum() */
23#include <net/arp.h>
24#include <net/dst.h>
25#include <linux/proc_fs.h>
26
27/* And atm device */
28#include <linux/atmdev.h>
29#include <linux/atmlec.h>
30#include <linux/atmmpc.h>
31/* Modular too */
1da177e4
LT
32#include <linux/module.h>
33
34#include "lec.h"
35#include "mpc.h"
36#include "resources.h"
37
38/*
f7d57453 39 * mpc.c: Implementation of MPOA client kernel part
1da177e4
LT
40 */
41
42#if 0
b50c2ea7
JP
43#define dprintk(format, args...) \
44 printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args)
45#define dprintk_cont(format, args...) printk(KERN_CONT format, ##args)
1da177e4 46#else
b50c2ea7
JP
47#define dprintk(format, args...) \
48 do { if (0) \
49 printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args);\
50 } while (0)
51#define dprintk_cont(format, args...) \
52 do { if (0) printk(KERN_CONT format, ##args); } while (0)
1da177e4
LT
53#endif
54
55#if 0
b50c2ea7
JP
56#define ddprintk(format, args...) \
57 printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args)
58#define ddprintk_cont(format, args...) printk(KERN_CONT format, ##args)
1da177e4 59#else
b50c2ea7
JP
60#define ddprintk(format, args...) \
61 do { if (0) \
62 printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args);\
63 } while (0)
64#define ddprintk_cont(format, args...) \
65 do { if (0) printk(KERN_CONT format, ##args); } while (0)
1da177e4
LT
66#endif
67
1da177e4 68/* mpc_daemon -> kernel */
57100440 69static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc);
1da177e4
LT
70static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc);
71static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
72static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
73static void mps_death(struct k_message *msg, struct mpoa_client *mpc);
57100440
JP
74static void clean_up(struct k_message *msg, struct mpoa_client *mpc,
75 int action);
76static void MPOA_cache_impos_rcvd(struct k_message *msg,
77 struct mpoa_client *mpc);
78static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg,
79 struct mpoa_client *mpc);
80static void set_mps_mac_addr_rcvd(struct k_message *mesg,
81 struct mpoa_client *mpc);
1da177e4 82
cba5cbd1
DH
83static const uint8_t *copy_macs(struct mpoa_client *mpc,
84 const uint8_t *router_mac,
85 const uint8_t *tlvs, uint8_t mps_macs,
86 uint8_t device_type);
1da177e4
LT
87static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry);
88
cba5cbd1 89static void send_set_mps_ctrl_addr(const char *addr, struct mpoa_client *mpc);
1da177e4
LT
90static void mpoad_close(struct atm_vcc *vcc);
91static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb);
92
93static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb);
3c805a22 94static netdev_tx_t mpc_send_packet(struct sk_buff *skb,
57100440
JP
95 struct net_device *dev);
96static int mpoa_event_listener(struct notifier_block *mpoa_notifier,
97 unsigned long event, void *dev);
1da177e4 98static void mpc_timer_refresh(void);
5c658e19 99static void mpc_cache_check(struct timer_list *unused);
1da177e4
LT
100
101static struct llc_snap_hdr llc_snap_mpoa_ctrl = {
102 0xaa, 0xaa, 0x03,
103 {0x00, 0x00, 0x5e},
104 {0x00, 0x03} /* For MPOA control PDUs */
f7d57453 105};
1da177e4
LT
106static struct llc_snap_hdr llc_snap_mpoa_data = {
107 0xaa, 0xaa, 0x03,
108 {0x00, 0x00, 0x00},
109 {0x08, 0x00} /* This is for IP PDUs only */
f7d57453 110};
1da177e4
LT
111static struct llc_snap_hdr llc_snap_mpoa_data_tagged = {
112 0xaa, 0xaa, 0x03,
113 {0x00, 0x00, 0x00},
114 {0x88, 0x4c} /* This is for tagged data PDUs */
f7d57453 115};
1da177e4
LT
116
117static struct notifier_block mpoa_notifier = {
118 mpoa_event_listener,
119 NULL,
120 0
121};
122
1da177e4
LT
123struct mpoa_client *mpcs = NULL; /* FIXME */
124static struct atm_mpoa_qos *qos_head = NULL;
1e9aa74e 125static DEFINE_TIMER(mpc_timer, mpc_cache_check);
1da177e4
LT
126
127
128static struct mpoa_client *find_mpc_by_itfnum(int itf)
129{
130 struct mpoa_client *mpc;
f7d57453 131
1da177e4
LT
132 mpc = mpcs; /* our global linked list */
133 while (mpc != NULL) {
134 if (mpc->dev_num == itf)
135 return mpc;
f7d57453 136 mpc = mpc->next;
1da177e4
LT
137 }
138
139 return NULL; /* not found */
140}
141
142static struct mpoa_client *find_mpc_by_vcc(struct atm_vcc *vcc)
143{
144 struct mpoa_client *mpc;
f7d57453 145
1da177e4
LT
146 mpc = mpcs; /* our global linked list */
147 while (mpc != NULL) {
148 if (mpc->mpoad_vcc == vcc)
149 return mpc;
150 mpc = mpc->next;
151 }
152
153 return NULL; /* not found */
154}
155
156static struct mpoa_client *find_mpc_by_lec(struct net_device *dev)
157{
158 struct mpoa_client *mpc;
f7d57453 159
1da177e4
LT
160 mpc = mpcs; /* our global linked list */
161 while (mpc != NULL) {
162 if (mpc->dev == dev)
163 return mpc;
164 mpc = mpc->next;
165 }
166
167 return NULL; /* not found */
168}
169
170/*
171 * Functions for managing QoS list
172 */
173
174/*
175 * Overwrites the old entry or makes a new one.
176 */
30d492da 177struct atm_mpoa_qos *atm_mpoa_add_qos(__be32 dst_ip, struct atm_qos *qos)
1da177e4
LT
178{
179 struct atm_mpoa_qos *entry;
180
181 entry = atm_mpoa_search_qos(dst_ip);
182 if (entry != NULL) {
183 entry->qos = *qos;
184 return entry;
185 }
186
187 entry = kmalloc(sizeof(struct atm_mpoa_qos), GFP_KERNEL);
188 if (entry == NULL) {
57100440 189 pr_info("mpoa: out of memory\n");
1da177e4
LT
190 return entry;
191 }
192
193 entry->ipaddr = dst_ip;
194 entry->qos = *qos;
195
196 entry->next = qos_head;
197 qos_head = entry;
198
199 return entry;
200}
201
30d492da 202struct atm_mpoa_qos *atm_mpoa_search_qos(__be32 dst_ip)
1da177e4
LT
203{
204 struct atm_mpoa_qos *qos;
205
206 qos = qos_head;
57100440
JP
207 while (qos) {
208 if (qos->ipaddr == dst_ip)
1da177e4 209 break;
1da177e4
LT
210 qos = qos->next;
211 }
212
213 return qos;
f7d57453 214}
1da177e4
LT
215
216/*
217 * Returns 0 for failure
218 */
219int atm_mpoa_delete_qos(struct atm_mpoa_qos *entry)
220{
1da177e4
LT
221 struct atm_mpoa_qos *curr;
222
57100440
JP
223 if (entry == NULL)
224 return 0;
1da177e4
LT
225 if (entry == qos_head) {
226 qos_head = qos_head->next;
227 kfree(entry);
228 return 1;
229 }
230
231 curr = qos_head;
232 while (curr != NULL) {
233 if (curr->next == entry) {
234 curr->next = entry->next;
235 kfree(entry);
236 return 1;
237 }
238 curr = curr->next;
239 }
240
241 return 0;
242}
243
244/* this is buggered - we need locking for qos_head */
245void atm_mpoa_disp_qos(struct seq_file *m)
246{
1da177e4
LT
247 struct atm_mpoa_qos *qos;
248
249 qos = qos_head;
250 seq_printf(m, "QoS entries for shortcuts:\n");
251 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");
252
1da177e4 253 while (qos != NULL) {
21454aaa 254 seq_printf(m, "%pI4\n %-7d %-7d %-7d %-7d %-7d\n %-7d %-7d %-7d %-7d %-7d\n",
57100440
JP
255 &qos->ipaddr,
256 qos->qos.txtp.max_pcr,
257 qos->qos.txtp.pcr,
258 qos->qos.txtp.min_pcr,
259 qos->qos.txtp.max_cdv,
260 qos->qos.txtp.max_sdu,
261 qos->qos.rxtp.max_pcr,
262 qos->qos.rxtp.pcr,
263 qos->qos.rxtp.min_pcr,
264 qos->qos.rxtp.max_cdv,
265 qos->qos.rxtp.max_sdu);
1da177e4
LT
266 qos = qos->next;
267 }
268}
269
270static struct net_device *find_lec_by_itfnum(int itf)
271{
272 struct net_device *dev;
273 char name[IFNAMSIZ];
274
275 sprintf(name, "lec%d", itf);
881d966b 276 dev = dev_get_by_name(&init_net, name);
f7d57453 277
1da177e4
LT
278 return dev;
279}
280
281static struct mpoa_client *alloc_mpc(void)
282{
283 struct mpoa_client *mpc;
284
57100440 285 mpc = kzalloc(sizeof(struct mpoa_client), GFP_KERNEL);
1da177e4
LT
286 if (mpc == NULL)
287 return NULL;
1da177e4
LT
288 rwlock_init(&mpc->ingress_lock);
289 rwlock_init(&mpc->egress_lock);
290 mpc->next = mpcs;
291 atm_mpoa_init_cache(mpc);
292
293 mpc->parameters.mpc_p1 = MPC_P1;
294 mpc->parameters.mpc_p2 = MPC_P2;
57100440 295 memset(mpc->parameters.mpc_p3, 0, sizeof(mpc->parameters.mpc_p3));
1da177e4 296 mpc->parameters.mpc_p4 = MPC_P4;
f7d57453 297 mpc->parameters.mpc_p5 = MPC_P5;
1da177e4 298 mpc->parameters.mpc_p6 = MPC_P6;
f7d57453 299
1da177e4 300 mpcs = mpc;
f7d57453 301
1da177e4
LT
302 return mpc;
303}
304
305/*
306 *
307 * start_mpc() puts the MPC on line. All the packets destined
f7d57453 308 * to the lec underneath us are now being monitored and
1da177e4
LT
309 * shortcuts will be established.
310 *
311 */
312static void start_mpc(struct mpoa_client *mpc, struct net_device *dev)
313{
f7d57453 314
b50c2ea7 315 dprintk("(%s)\n", mpc->dev->name);
788dee0a 316 if (!dev->netdev_ops)
57100440 317 pr_info("(%s) not starting\n", dev->name);
788dee0a
SH
318 else {
319 mpc->old_ops = dev->netdev_ops;
320 mpc->new_ops = *mpc->old_ops;
321 mpc->new_ops.ndo_start_xmit = mpc_send_packet;
322 dev->netdev_ops = &mpc->new_ops;
1da177e4 323 }
1da177e4
LT
324}
325
326static void stop_mpc(struct mpoa_client *mpc)
327{
788dee0a 328 struct net_device *dev = mpc->dev;
b50c2ea7 329 dprintk("(%s)", mpc->dev->name);
1da177e4
LT
330
331 /* Lets not nullify lec device's dev->hard_start_xmit */
788dee0a 332 if (dev->netdev_ops != &mpc->new_ops) {
b50c2ea7 333 dprintk_cont(" mpc already stopped, not fatal\n");
1da177e4
LT
334 return;
335 }
b50c2ea7 336 dprintk_cont("\n");
f7d57453 337
788dee0a
SH
338 dev->netdev_ops = mpc->old_ops;
339 mpc->old_ops = NULL;
340
341 /* close_shortcuts(mpc); ??? FIXME */
1da177e4
LT
342}
343
344static const char *mpoa_device_type_string(char type) __attribute__ ((unused));
345
346static const char *mpoa_device_type_string(char type)
347{
57100440 348 switch (type) {
1da177e4
LT
349 case NON_MPOA:
350 return "non-MPOA device";
1da177e4
LT
351 case MPS:
352 return "MPS";
1da177e4
LT
353 case MPC:
354 return "MPC";
1da177e4
LT
355 case MPS_AND_MPC:
356 return "both MPS and MPC";
1da177e4
LT
357 }
358
57100440 359 return "unspecified (non-MPOA) device";
1da177e4
LT
360}
361
362/*
b74ca3a8
WC
363 * lec device calls this via its netdev_priv(dev)->lane2_ops
364 * ->associate_indicator() when it sees a TLV in LE_ARP packet.
1da177e4
LT
365 * We fill in the pointer above when we see a LANE2 lec initializing
366 * See LANE2 spec 3.1.5
367 *
368 * Quite a big and ugly function but when you look at it
369 * all it does is to try to locate and parse MPOA Device
370 * Type TLV.
371 * We give our lec a pointer to this function and when the
372 * lec sees a TLV it uses the pointer to call this function.
373 *
374 */
cba5cbd1
DH
375static void lane2_assoc_ind(struct net_device *dev, const u8 *mac_addr,
376 const u8 *tlvs, u32 sizeoftlvs)
1da177e4
LT
377{
378 uint32_t type;
379 uint8_t length, mpoa_device_type, number_of_mps_macs;
cba5cbd1 380 const uint8_t *end_of_tlvs;
1da177e4 381 struct mpoa_client *mpc;
f7d57453 382
1da177e4 383 mpoa_device_type = number_of_mps_macs = 0; /* silence gcc */
b50c2ea7 384 dprintk("(%s) received TLV(s), ", dev->name);
1da177e4
LT
385 dprintk("total length of all TLVs %d\n", sizeoftlvs);
386 mpc = find_mpc_by_lec(dev); /* Sampo-Fix: moved here from below */
387 if (mpc == NULL) {
57100440 388 pr_info("(%s) no mpc\n", dev->name);
1da177e4
LT
389 return;
390 }
391 end_of_tlvs = tlvs + sizeoftlvs;
392 while (end_of_tlvs - tlvs >= 5) {
57100440
JP
393 type = ((tlvs[0] << 24) | (tlvs[1] << 16) |
394 (tlvs[2] << 8) | tlvs[3]);
1da177e4
LT
395 length = tlvs[4];
396 tlvs += 5;
397 dprintk(" type 0x%x length %02x\n", type, length);
398 if (tlvs + length > end_of_tlvs) {
57100440 399 pr_info("TLV value extends past its buffer, aborting parse\n");
1da177e4
LT
400 return;
401 }
f7d57453 402
1da177e4 403 if (type == 0) {
57100440
JP
404 pr_info("mpoa: (%s) TLV type was 0, returning\n",
405 dev->name);
1da177e4
LT
406 return;
407 }
408
409 if (type != TLV_MPOA_DEVICE_TYPE) {
410 tlvs += length;
411 continue; /* skip other TLVs */
412 }
413 mpoa_device_type = *tlvs++;
414 number_of_mps_macs = *tlvs++;
b50c2ea7 415 dprintk("(%s) MPOA device type '%s', ",
57100440 416 dev->name, mpoa_device_type_string(mpoa_device_type));
1da177e4
LT
417 if (mpoa_device_type == MPS_AND_MPC &&
418 length < (42 + number_of_mps_macs*ETH_ALEN)) { /* :) */
57100440
JP
419 pr_info("(%s) short MPOA Device Type TLV\n",
420 dev->name);
1da177e4
LT
421 continue;
422 }
57100440
JP
423 if ((mpoa_device_type == MPS || mpoa_device_type == MPC) &&
424 length < 22 + number_of_mps_macs*ETH_ALEN) {
425 pr_info("(%s) short MPOA Device Type TLV\n", dev->name);
1da177e4
LT
426 continue;
427 }
57100440
JP
428 if (mpoa_device_type != MPS &&
429 mpoa_device_type != MPS_AND_MPC) {
b50c2ea7 430 dprintk("ignoring non-MPS device ");
57100440
JP
431 if (mpoa_device_type == MPC)
432 tlvs += 20;
1da177e4
LT
433 continue; /* we are only interested in MPSs */
434 }
57100440
JP
435 if (number_of_mps_macs == 0 &&
436 mpoa_device_type == MPS_AND_MPC) {
437 pr_info("(%s) MPS_AND_MPC has zero MACs\n", dev->name);
1da177e4
LT
438 continue; /* someone should read the spec */
439 }
b50c2ea7
JP
440 dprintk_cont("this MPS has %d MAC addresses\n",
441 number_of_mps_macs);
f7d57453 442
57100440
JP
443 /*
444 * ok, now we can go and tell our daemon
445 * the control address of MPS
446 */
1da177e4 447 send_set_mps_ctrl_addr(tlvs, mpc);
f7d57453 448
57100440
JP
449 tlvs = copy_macs(mpc, mac_addr, tlvs,
450 number_of_mps_macs, mpoa_device_type);
451 if (tlvs == NULL)
452 return;
1da177e4
LT
453 }
454 if (end_of_tlvs - tlvs != 0)
5b5e0928 455 pr_info("(%s) ignoring %zd bytes of trailing TLV garbage\n",
57100440 456 dev->name, end_of_tlvs - tlvs);
1da177e4
LT
457}
458
459/*
460 * Store at least advertizing router's MAC address
461 * plus the possible MAC address(es) to mpc->mps_macs.
462 * For a freshly allocated MPOA client mpc->mps_macs == 0.
463 */
cba5cbd1
DH
464static const uint8_t *copy_macs(struct mpoa_client *mpc,
465 const uint8_t *router_mac,
466 const uint8_t *tlvs, uint8_t mps_macs,
467 uint8_t device_type)
1da177e4
LT
468{
469 int num_macs;
470 num_macs = (mps_macs > 1) ? mps_macs : 1;
471
472 if (mpc->number_of_mps_macs != num_macs) { /* need to reallocate? */
57100440
JP
473 if (mpc->number_of_mps_macs != 0)
474 kfree(mpc->mps_macs);
1da177e4 475 mpc->number_of_mps_macs = 0;
6da2ec56 476 mpc->mps_macs = kmalloc_array(ETH_ALEN, num_macs, GFP_KERNEL);
1da177e4 477 if (mpc->mps_macs == NULL) {
57100440 478 pr_info("(%s) out of mem\n", mpc->dev->name);
1da177e4
LT
479 return NULL;
480 }
481 }
116e853f 482 ether_addr_copy(mpc->mps_macs, router_mac);
1da177e4
LT
483 tlvs += 20; if (device_type == MPS_AND_MPC) tlvs += 20;
484 if (mps_macs > 0)
485 memcpy(mpc->mps_macs, tlvs, mps_macs*ETH_ALEN);
486 tlvs += mps_macs*ETH_ALEN;
487 mpc->number_of_mps_macs = num_macs;
488
489 return tlvs;
490}
491
492static int send_via_shortcut(struct sk_buff *skb, struct mpoa_client *mpc)
493{
494 in_cache_entry *entry;
495 struct iphdr *iph;
496 char *buff;
30d492da 497 __be32 ipaddr = 0;
1da177e4
LT
498
499 static struct {
500 struct llc_snap_hdr hdr;
30d492da 501 __be32 tag;
1da177e4
LT
502 } tagged_llc_snap_hdr = {
503 {0xaa, 0xaa, 0x03, {0x00, 0x00, 0x00}, {0x88, 0x4c}},
504 0
505 };
506
507 buff = skb->data + mpc->dev->hard_header_len;
508 iph = (struct iphdr *)buff;
509 ipaddr = iph->daddr;
510
b50c2ea7 511 ddprintk("(%s) ipaddr 0x%x\n",
57100440 512 mpc->dev->name, ipaddr);
1da177e4
LT
513
514 entry = mpc->in_ops->get(ipaddr, mpc);
515 if (entry == NULL) {
516 entry = mpc->in_ops->add_entry(ipaddr, mpc);
57100440
JP
517 if (entry != NULL)
518 mpc->in_ops->put(entry);
1da177e4
LT
519 return 1;
520 }
57100440
JP
521 /* threshold not exceeded or VCC not ready */
522 if (mpc->in_ops->cache_hit(entry, mpc) != OPEN) {
b50c2ea7 523 ddprintk("(%s) cache_hit: returns != OPEN\n",
57100440 524 mpc->dev->name);
1da177e4
LT
525 mpc->in_ops->put(entry);
526 return 1;
527 }
528
b50c2ea7 529 ddprintk("(%s) using shortcut\n",
57100440 530 mpc->dev->name);
1da177e4
LT
531 /* MPOA spec A.1.4, MPOA client must decrement IP ttl at least by one */
532 if (iph->ttl <= 1) {
b50c2ea7 533 ddprintk("(%s) IP ttl = %u, using LANE\n",
57100440 534 mpc->dev->name, iph->ttl);
1da177e4
LT
535 mpc->in_ops->put(entry);
536 return 1;
537 }
538 iph->ttl--;
539 iph->check = 0;
540 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
541
542 if (entry->ctrl_info.tag != 0) {
b50c2ea7 543 ddprintk("(%s) adding tag 0x%x\n",
57100440 544 mpc->dev->name, entry->ctrl_info.tag);
1da177e4 545 tagged_llc_snap_hdr.tag = entry->ctrl_info.tag;
57100440
JP
546 skb_pull(skb, ETH_HLEN); /* get rid of Eth header */
547 skb_push(skb, sizeof(tagged_llc_snap_hdr));
548 /* add LLC/SNAP header */
27d7ff46
ACM
549 skb_copy_to_linear_data(skb, &tagged_llc_snap_hdr,
550 sizeof(tagged_llc_snap_hdr));
1da177e4 551 } else {
57100440
JP
552 skb_pull(skb, ETH_HLEN); /* get rid of Eth header */
553 skb_push(skb, sizeof(struct llc_snap_hdr));
554 /* add LLC/SNAP header + tag */
27d7ff46
ACM
555 skb_copy_to_linear_data(skb, &llc_snap_mpoa_data,
556 sizeof(struct llc_snap_hdr));
1da177e4
LT
557 }
558
9bbe60a6 559 atm_account_tx(entry->shortcut, skb);
1da177e4
LT
560 entry->shortcut->send(entry->shortcut, skb);
561 entry->packets_fwded++;
562 mpc->in_ops->put(entry);
563
564 return 0;
565}
566
567/*
568 * Probably needs some error checks and locking, not sure...
569 */
3c805a22
SH
570static netdev_tx_t mpc_send_packet(struct sk_buff *skb,
571 struct net_device *dev)
1da177e4 572{
1da177e4
LT
573 struct mpoa_client *mpc;
574 struct ethhdr *eth;
575 int i = 0;
f7d57453 576
1da177e4 577 mpc = find_mpc_by_lec(dev); /* this should NEVER fail */
57100440
JP
578 if (mpc == NULL) {
579 pr_info("(%s) no MPC found\n", dev->name);
1da177e4
LT
580 goto non_ip;
581 }
582
583 eth = (struct ethhdr *)skb->data;
584 if (eth->h_proto != htons(ETH_P_IP))
585 goto non_ip; /* Multi-Protocol Over ATM :-) */
586
1c9b7aa1
HX
587 /* Weed out funny packets (e.g., AF_PACKET or raw). */
588 if (skb->len < ETH_HLEN + sizeof(struct iphdr))
589 goto non_ip;
590 skb_set_network_header(skb, ETH_HLEN);
591 if (skb->len < ETH_HLEN + ip_hdr(skb)->ihl * 4 || ip_hdr(skb)->ihl < 5)
592 goto non_ip;
593
1da177e4 594 while (i < mpc->number_of_mps_macs) {
150238eb 595 if (ether_addr_equal(eth->h_dest, mpc->mps_macs + i * ETH_ALEN))
57100440
JP
596 if (send_via_shortcut(skb, mpc) == 0) /* try shortcut */
597 return NETDEV_TX_OK;
1da177e4
LT
598 i++;
599 }
600
57100440 601non_ip:
fa2dbdc2 602 return __netdev_start_xmit(mpc->old_ops, skb, dev, false);
1da177e4
LT
603}
604
605static int atm_mpoa_vcc_attach(struct atm_vcc *vcc, void __user *arg)
606{
607 int bytes_left;
608 struct mpoa_client *mpc;
609 struct atmmpc_ioc ioc_data;
610 in_cache_entry *in_entry;
30d492da 611 __be32 ipaddr;
1da177e4
LT
612
613 bytes_left = copy_from_user(&ioc_data, arg, sizeof(struct atmmpc_ioc));
614 if (bytes_left != 0) {
57100440
JP
615 pr_info("mpoa:Short read (missed %d bytes) from userland\n",
616 bytes_left);
1da177e4
LT
617 return -EFAULT;
618 }
619 ipaddr = ioc_data.ipaddr;
620 if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF)
621 return -EINVAL;
f7d57453 622
1da177e4
LT
623 mpc = find_mpc_by_itfnum(ioc_data.dev_num);
624 if (mpc == NULL)
625 return -EINVAL;
f7d57453 626
1da177e4
LT
627 if (ioc_data.type == MPC_SOCKET_INGRESS) {
628 in_entry = mpc->in_ops->get(ipaddr, mpc);
57100440
JP
629 if (in_entry == NULL ||
630 in_entry->entry_state < INGRESS_RESOLVED) {
631 pr_info("(%s) did not find RESOLVED entry from ingress cache\n",
1da177e4 632 mpc->dev->name);
57100440
JP
633 if (in_entry != NULL)
634 mpc->in_ops->put(in_entry);
1da177e4
LT
635 return -EINVAL;
636 }
57100440
JP
637 pr_info("(%s) attaching ingress SVC, entry = %pI4\n",
638 mpc->dev->name, &in_entry->ctrl_info.in_dst_ip);
1da177e4
LT
639 in_entry->shortcut = vcc;
640 mpc->in_ops->put(in_entry);
641 } else {
57100440 642 pr_info("(%s) attaching egress SVC\n", mpc->dev->name);
1da177e4
LT
643 }
644
645 vcc->proto_data = mpc->dev;
646 vcc->push = mpc_push;
647
648 return 0;
649}
650
651/*
652 *
653 */
654static void mpc_vcc_close(struct atm_vcc *vcc, struct net_device *dev)
655{
656 struct mpoa_client *mpc;
657 in_cache_entry *in_entry;
658 eg_cache_entry *eg_entry;
f7d57453 659
1da177e4
LT
660 mpc = find_mpc_by_lec(dev);
661 if (mpc == NULL) {
57100440 662 pr_info("(%s) close for unknown MPC\n", dev->name);
1da177e4
LT
663 return;
664 }
665
b50c2ea7 666 dprintk("(%s)\n", dev->name);
1da177e4
LT
667 in_entry = mpc->in_ops->get_by_vcc(vcc, mpc);
668 if (in_entry) {
b50c2ea7 669 dprintk("(%s) ingress SVC closed ip = %pI4\n",
57100440 670 mpc->dev->name, &in_entry->ctrl_info.in_dst_ip);
1da177e4
LT
671 in_entry->shortcut = NULL;
672 mpc->in_ops->put(in_entry);
673 }
674 eg_entry = mpc->eg_ops->get_by_vcc(vcc, mpc);
675 if (eg_entry) {
b50c2ea7 676 dprintk("(%s) egress SVC closed\n", mpc->dev->name);
1da177e4
LT
677 eg_entry->shortcut = NULL;
678 mpc->eg_ops->put(eg_entry);
679 }
680
681 if (in_entry == NULL && eg_entry == NULL)
b50c2ea7 682 dprintk("(%s) unused vcc closed\n", dev->name);
1da177e4
LT
683}
684
685static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb)
686{
687 struct net_device *dev = (struct net_device *)vcc->proto_data;
688 struct sk_buff *new_skb;
689 eg_cache_entry *eg;
690 struct mpoa_client *mpc;
30d492da 691 __be32 tag;
1da177e4 692 char *tmp;
f7d57453 693
b50c2ea7 694 ddprintk("(%s)\n", dev->name);
1da177e4 695 if (skb == NULL) {
b50c2ea7 696 dprintk("(%s) null skb, closing VCC\n", dev->name);
1da177e4
LT
697 mpc_vcc_close(vcc, dev);
698 return;
699 }
f7d57453 700
1da177e4 701 skb->dev = dev;
57100440
JP
702 if (memcmp(skb->data, &llc_snap_mpoa_ctrl,
703 sizeof(struct llc_snap_hdr)) == 0) {
1da177e4
LT
704 struct sock *sk = sk_atm(vcc);
705
b50c2ea7 706 dprintk("(%s) control packet arrived\n", dev->name);
1da177e4
LT
707 /* Pass control packets to daemon */
708 skb_queue_tail(&sk->sk_receive_queue, skb);
676d2369 709 sk->sk_data_ready(sk);
1da177e4
LT
710 return;
711 }
712
713 /* data coming over the shortcut */
714 atm_return(vcc, skb->truesize);
715
716 mpc = find_mpc_by_lec(dev);
717 if (mpc == NULL) {
57100440 718 pr_info("(%s) unknown MPC\n", dev->name);
1da177e4
LT
719 return;
720 }
721
57100440
JP
722 if (memcmp(skb->data, &llc_snap_mpoa_data_tagged,
723 sizeof(struct llc_snap_hdr)) == 0) { /* MPOA tagged data */
b50c2ea7 724 ddprintk("(%s) tagged data packet arrived\n", dev->name);
1da177e4 725
57100440
JP
726 } else if (memcmp(skb->data, &llc_snap_mpoa_data,
727 sizeof(struct llc_snap_hdr)) == 0) { /* MPOA data */
728 pr_info("(%s) Unsupported non-tagged data packet arrived. Purging\n",
729 dev->name);
1da177e4
LT
730 dev_kfree_skb_any(skb);
731 return;
732 } else {
57100440 733 pr_info("(%s) garbage arrived, purging\n", dev->name);
1da177e4
LT
734 dev_kfree_skb_any(skb);
735 return;
736 }
737
738 tmp = skb->data + sizeof(struct llc_snap_hdr);
30d492da 739 tag = *(__be32 *)tmp;
1da177e4
LT
740
741 eg = mpc->eg_ops->get_by_tag(tag, mpc);
742 if (eg == NULL) {
57100440
JP
743 pr_info("mpoa: (%s) Didn't find egress cache entry, tag = %u\n",
744 dev->name, tag);
1da177e4
LT
745 purge_egress_shortcut(vcc, NULL);
746 dev_kfree_skb_any(skb);
747 return;
748 }
f7d57453 749
1da177e4
LT
750 /*
751 * See if ingress MPC is using shortcut we opened as a return channel.
752 * This means we have a bi-directional vcc opened by us.
f7d57453 753 */
1da177e4
LT
754 if (eg->shortcut == NULL) {
755 eg->shortcut = vcc;
57100440 756 pr_info("(%s) egress SVC in use\n", dev->name);
1da177e4
LT
757 }
758
57100440
JP
759 skb_pull(skb, sizeof(struct llc_snap_hdr) + sizeof(tag));
760 /* get rid of LLC/SNAP header */
761 new_skb = skb_realloc_headroom(skb, eg->ctrl_info.DH_length);
762 /* LLC/SNAP is shorter than MAC header :( */
1da177e4 763 dev_kfree_skb_any(skb);
57100440 764 if (new_skb == NULL) {
1da177e4
LT
765 mpc->eg_ops->put(eg);
766 return;
767 }
768 skb_push(new_skb, eg->ctrl_info.DH_length); /* add MAC header */
27d7ff46
ACM
769 skb_copy_to_linear_data(new_skb, eg->ctrl_info.DLL_header,
770 eg->ctrl_info.DH_length);
1da177e4 771 new_skb->protocol = eth_type_trans(new_skb, dev);
c1d2bbe1 772 skb_reset_network_header(new_skb);
1da177e4 773
eddc9ec5 774 eg->latest_ip_addr = ip_hdr(new_skb)->saddr;
1da177e4
LT
775 eg->packets_rcvd++;
776 mpc->eg_ops->put(eg);
777
5518b29f 778 memset(ATM_SKB(new_skb), 0, sizeof(struct atm_skb_data));
1da177e4 779 netif_rx(new_skb);
1da177e4
LT
780}
781
800bb47e 782static const struct atmdev_ops mpc_ops = { /* only send is required */
1da177e4
LT
783 .close = mpoad_close,
784 .send = msg_from_mpoad
785};
786
787static struct atm_dev mpc_dev = {
788 .ops = &mpc_ops,
789 .type = "mpc",
790 .number = 42,
4ef8d0ae 791 .lock = __SPIN_LOCK_UNLOCKED(mpc_dev.lock)
1da177e4
LT
792 /* members not explicitly initialised will be 0 */
793};
794
57100440 795static int atm_mpoa_mpoad_attach(struct atm_vcc *vcc, int arg)
1da177e4
LT
796{
797 struct mpoa_client *mpc;
798 struct lec_priv *priv;
799 int err;
f7d57453 800
1da177e4 801 if (mpcs == NULL) {
1da177e4
LT
802 mpc_timer_refresh();
803
804 /* This lets us now how our LECs are doing */
805 err = register_netdevice_notifier(&mpoa_notifier);
806 if (err < 0) {
807 del_timer(&mpc_timer);
808 return err;
809 }
810 }
f7d57453 811
1da177e4
LT
812 mpc = find_mpc_by_itfnum(arg);
813 if (mpc == NULL) {
b50c2ea7 814 dprintk("allocating new mpc for itf %d\n", arg);
1da177e4
LT
815 mpc = alloc_mpc();
816 if (mpc == NULL)
817 return -ENOMEM;
818 mpc->dev_num = arg;
57100440
JP
819 mpc->dev = find_lec_by_itfnum(arg);
820 /* NULL if there was no lec */
1da177e4
LT
821 }
822 if (mpc->mpoad_vcc) {
57100440 823 pr_info("mpoad is already present for itf %d\n", arg);
1da177e4
LT
824 return -EADDRINUSE;
825 }
826
827 if (mpc->dev) { /* check if the lec is LANE2 capable */
524ad0a7 828 priv = netdev_priv(mpc->dev);
1da177e4
LT
829 if (priv->lane_version < 2) {
830 dev_put(mpc->dev);
831 mpc->dev = NULL;
832 } else
f7d57453 833 priv->lane2_ops->associate_indicator = lane2_assoc_ind;
1da177e4
LT
834 }
835
836 mpc->mpoad_vcc = vcc;
837 vcc->dev = &mpc_dev;
838 vcc_insert_socket(sk_atm(vcc));
57100440
JP
839 set_bit(ATM_VF_META, &vcc->flags);
840 set_bit(ATM_VF_READY, &vcc->flags);
1da177e4
LT
841
842 if (mpc->dev) {
843 char empty[ATM_ESA_LEN];
844 memset(empty, 0, ATM_ESA_LEN);
f7d57453 845
1da177e4
LT
846 start_mpc(mpc, mpc->dev);
847 /* set address if mpcd e.g. gets killed and restarted.
848 * If we do not do it now we have to wait for the next LE_ARP
849 */
57100440 850 if (memcmp(mpc->mps_ctrl_addr, empty, ATM_ESA_LEN) != 0)
1da177e4
LT
851 send_set_mps_ctrl_addr(mpc->mps_ctrl_addr, mpc);
852 }
853
854 __module_get(THIS_MODULE);
855 return arg;
856}
857
cba5cbd1 858static void send_set_mps_ctrl_addr(const char *addr, struct mpoa_client *mpc)
1da177e4
LT
859{
860 struct k_message mesg;
861
57100440 862 memcpy(mpc->mps_ctrl_addr, addr, ATM_ESA_LEN);
f7d57453 863
1da177e4
LT
864 mesg.type = SET_MPS_CTRL_ADDR;
865 memcpy(mesg.MPS_ctrl, addr, ATM_ESA_LEN);
866 msg_to_mpoad(&mesg, mpc);
1da177e4
LT
867}
868
869static void mpoad_close(struct atm_vcc *vcc)
870{
871 struct mpoa_client *mpc;
872 struct sk_buff *skb;
873
874 mpc = find_mpc_by_vcc(vcc);
875 if (mpc == NULL) {
57100440 876 pr_info("did not find MPC\n");
1da177e4
LT
877 return;
878 }
879 if (!mpc->mpoad_vcc) {
57100440 880 pr_info("close for non-present mpoad\n");
1da177e4
LT
881 return;
882 }
f7d57453 883
1da177e4
LT
884 mpc->mpoad_vcc = NULL;
885 if (mpc->dev) {
524ad0a7 886 struct lec_priv *priv = netdev_priv(mpc->dev);
1da177e4
LT
887 priv->lane2_ops->associate_indicator = NULL;
888 stop_mpc(mpc);
889 dev_put(mpc->dev);
890 }
891
892 mpc->in_ops->destroy_cache(mpc);
893 mpc->eg_ops->destroy_cache(mpc);
894
895 while ((skb = skb_dequeue(&sk_atm(vcc)->sk_receive_queue))) {
896 atm_return(vcc, skb->truesize);
897 kfree_skb(skb);
898 }
f7d57453 899
57100440 900 pr_info("(%s) going down\n",
1da177e4
LT
901 (mpc->dev) ? mpc->dev->name : "<unknown>");
902 module_put(THIS_MODULE);
1da177e4
LT
903}
904
905/*
906 *
907 */
908static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb)
909{
f7d57453 910
1da177e4 911 struct mpoa_client *mpc = find_mpc_by_vcc(vcc);
57100440 912 struct k_message *mesg = (struct k_message *)skb->data;
14afee4b 913 WARN_ON(refcount_sub_and_test(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc));
f7d57453 914
1da177e4 915 if (mpc == NULL) {
57100440 916 pr_info("no mpc found\n");
1da177e4
LT
917 return 0;
918 }
b50c2ea7 919 dprintk("(%s)", mpc->dev ? mpc->dev->name : "<unknown>");
57100440 920 switch (mesg->type) {
1da177e4 921 case MPOA_RES_REPLY_RCVD:
b50c2ea7 922 dprintk_cont("mpoa_res_reply_rcvd\n");
1da177e4
LT
923 MPOA_res_reply_rcvd(mesg, mpc);
924 break;
925 case MPOA_TRIGGER_RCVD:
b50c2ea7 926 dprintk_cont("mpoa_trigger_rcvd\n");
1da177e4
LT
927 MPOA_trigger_rcvd(mesg, mpc);
928 break;
929 case INGRESS_PURGE_RCVD:
b50c2ea7 930 dprintk_cont("nhrp_purge_rcvd\n");
1da177e4
LT
931 ingress_purge_rcvd(mesg, mpc);
932 break;
933 case EGRESS_PURGE_RCVD:
b50c2ea7 934 dprintk_cont("egress_purge_reply_rcvd\n");
1da177e4
LT
935 egress_purge_rcvd(mesg, mpc);
936 break;
937 case MPS_DEATH:
b50c2ea7 938 dprintk_cont("mps_death\n");
1da177e4
LT
939 mps_death(mesg, mpc);
940 break;
941 case CACHE_IMPOS_RCVD:
b50c2ea7 942 dprintk_cont("cache_impos_rcvd\n");
1da177e4
LT
943 MPOA_cache_impos_rcvd(mesg, mpc);
944 break;
945 case SET_MPC_CTRL_ADDR:
b50c2ea7 946 dprintk_cont("set_mpc_ctrl_addr\n");
1da177e4
LT
947 set_mpc_ctrl_addr_rcvd(mesg, mpc);
948 break;
949 case SET_MPS_MAC_ADDR:
b50c2ea7 950 dprintk_cont("set_mps_mac_addr\n");
1da177e4
LT
951 set_mps_mac_addr_rcvd(mesg, mpc);
952 break;
953 case CLEAN_UP_AND_EXIT:
b50c2ea7 954 dprintk_cont("clean_up_and_exit\n");
1da177e4
LT
955 clean_up(mesg, mpc, DIE);
956 break;
957 case RELOAD:
b50c2ea7 958 dprintk_cont("reload\n");
1da177e4
LT
959 clean_up(mesg, mpc, RELOAD);
960 break;
961 case SET_MPC_PARAMS:
b50c2ea7 962 dprintk_cont("set_mpc_params\n");
1da177e4
LT
963 mpc->parameters = mesg->content.params;
964 break;
965 default:
b50c2ea7 966 dprintk_cont("unknown message %d\n", mesg->type);
1da177e4
LT
967 break;
968 }
969 kfree_skb(skb);
970
971 return 0;
972}
973
974/* Remember that this function may not do things that sleep */
975int msg_to_mpoad(struct k_message *mesg, struct mpoa_client *mpc)
976{
977 struct sk_buff *skb;
978 struct sock *sk;
979
980 if (mpc == NULL || !mpc->mpoad_vcc) {
57100440 981 pr_info("mesg %d to a non-existent mpoad\n", mesg->type);
1da177e4
LT
982 return -ENXIO;
983 }
984
985 skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
986 if (skb == NULL)
987 return -ENOMEM;
988 skb_put(skb, sizeof(struct k_message));
27d7ff46 989 skb_copy_to_linear_data(skb, mesg, sizeof(*mesg));
1da177e4 990 atm_force_charge(mpc->mpoad_vcc, skb->truesize);
f7d57453 991
1da177e4
LT
992 sk = sk_atm(mpc->mpoad_vcc);
993 skb_queue_tail(&sk->sk_receive_queue, skb);
676d2369 994 sk->sk_data_ready(sk);
1da177e4
LT
995
996 return 0;
997}
998
57100440 999static int mpoa_event_listener(struct notifier_block *mpoa_notifier,
351638e7 1000 unsigned long event, void *ptr)
1da177e4 1001{
351638e7 1002 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1da177e4
LT
1003 struct mpoa_client *mpc;
1004 struct lec_priv *priv;
1005
721499e8 1006 if (!net_eq(dev_net(dev), &init_net))
e9dc8653
EB
1007 return NOTIFY_DONE;
1008
0d135e4f 1009 if (strncmp(dev->name, "lec", 3))
1da177e4 1010 return NOTIFY_DONE; /* we are only interested in lec:s */
f7d57453 1011
1da177e4
LT
1012 switch (event) {
1013 case NETDEV_REGISTER: /* a new lec device was allocated */
524ad0a7 1014 priv = netdev_priv(dev);
1da177e4
LT
1015 if (priv->lane_version < 2)
1016 break;
1017 priv->lane2_ops->associate_indicator = lane2_assoc_ind;
1018 mpc = find_mpc_by_itfnum(priv->itfnum);
1019 if (mpc == NULL) {
b50c2ea7 1020 dprintk("allocating new mpc for %s\n", dev->name);
1da177e4
LT
1021 mpc = alloc_mpc();
1022 if (mpc == NULL) {
57100440 1023 pr_info("no new mpc");
1da177e4
LT
1024 break;
1025 }
1026 }
1027 mpc->dev_num = priv->itfnum;
1028 mpc->dev = dev;
1029 dev_hold(dev);
b50c2ea7 1030 dprintk("(%s) was initialized\n", dev->name);
1da177e4
LT
1031 break;
1032 case NETDEV_UNREGISTER:
1033 /* the lec device was deallocated */
1034 mpc = find_mpc_by_lec(dev);
1035 if (mpc == NULL)
1036 break;
b50c2ea7 1037 dprintk("device (%s) was deallocated\n", dev->name);
1da177e4
LT
1038 stop_mpc(mpc);
1039 dev_put(mpc->dev);
1040 mpc->dev = NULL;
1041 break;
1042 case NETDEV_UP:
1043 /* the dev was ifconfig'ed up */
1044 mpc = find_mpc_by_lec(dev);
1045 if (mpc == NULL)
1046 break;
57100440 1047 if (mpc->mpoad_vcc != NULL)
1da177e4 1048 start_mpc(mpc, dev);
1da177e4
LT
1049 break;
1050 case NETDEV_DOWN:
1051 /* the dev was ifconfig'ed down */
1052 /* this means that the flow of packets from the
1053 * upper layer stops
1054 */
1055 mpc = find_mpc_by_lec(dev);
1056 if (mpc == NULL)
1057 break;
57100440 1058 if (mpc->mpoad_vcc != NULL)
1da177e4 1059 stop_mpc(mpc);
1da177e4
LT
1060 break;
1061 case NETDEV_REBOOT:
1062 case NETDEV_CHANGE:
1063 case NETDEV_CHANGEMTU:
1064 case NETDEV_CHANGEADDR:
1065 case NETDEV_GOING_DOWN:
1066 break;
1067 default:
1068 break;
1069 }
1070
1071 return NOTIFY_DONE;
1072}
1073
1074/*
1075 * Functions which are called after a message is received from mpcd.
1076 * Msg is reused on purpose.
1077 */
1078
1079
1080static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1081{
30d492da 1082 __be32 dst_ip = msg->content.in_info.in_dst_ip;
1da177e4
LT
1083 in_cache_entry *entry;
1084
1085 entry = mpc->in_ops->get(dst_ip, mpc);
57100440 1086 if (entry == NULL) {
1da177e4
LT
1087 entry = mpc->in_ops->add_entry(dst_ip, mpc);
1088 entry->entry_state = INGRESS_RESOLVING;
1089 msg->type = SND_MPOA_RES_RQST;
1090 msg->content.in_info = entry->ctrl_info;
1091 msg_to_mpoad(msg, mpc);
d750dbdc 1092 entry->reply_wait = ktime_get_seconds();
1da177e4
LT
1093 mpc->in_ops->put(entry);
1094 return;
1095 }
f7d57453 1096
57100440 1097 if (entry->entry_state == INGRESS_INVALID) {
1da177e4
LT
1098 entry->entry_state = INGRESS_RESOLVING;
1099 msg->type = SND_MPOA_RES_RQST;
1100 msg->content.in_info = entry->ctrl_info;
1101 msg_to_mpoad(msg, mpc);
d750dbdc 1102 entry->reply_wait = ktime_get_seconds();
1da177e4
LT
1103 mpc->in_ops->put(entry);
1104 return;
1105 }
f7d57453 1106
57100440 1107 pr_info("(%s) entry already in resolving state\n",
1da177e4
LT
1108 (mpc->dev) ? mpc->dev->name : "<unknown>");
1109 mpc->in_ops->put(entry);
1da177e4
LT
1110}
1111
1112/*
1113 * Things get complicated because we have to check if there's an egress
f7d57453 1114 * shortcut with suitable traffic parameters we could use.
1da177e4 1115 */
57100440
JP
1116static void check_qos_and_open_shortcut(struct k_message *msg,
1117 struct mpoa_client *client,
1118 in_cache_entry *entry)
1da177e4 1119{
30d492da 1120 __be32 dst_ip = msg->content.in_info.in_dst_ip;
1da177e4
LT
1121 struct atm_mpoa_qos *qos = atm_mpoa_search_qos(dst_ip);
1122 eg_cache_entry *eg_entry = client->eg_ops->get_by_src_ip(dst_ip, client);
1123
57100440
JP
1124 if (eg_entry && eg_entry->shortcut) {
1125 if (eg_entry->shortcut->qos.txtp.traffic_class &
1126 msg->qos.txtp.traffic_class &
1127 (qos ? qos->qos.txtp.traffic_class : ATM_UBR | ATM_CBR)) {
1128 if (eg_entry->shortcut->qos.txtp.traffic_class == ATM_UBR)
1129 entry->shortcut = eg_entry->shortcut;
1130 else if (eg_entry->shortcut->qos.txtp.max_pcr > 0)
1131 entry->shortcut = eg_entry->shortcut;
1da177e4 1132 }
57100440 1133 if (entry->shortcut) {
b50c2ea7 1134 dprintk("(%s) using egress SVC to reach %pI4\n",
21454aaa 1135 client->dev->name, &dst_ip);
1da177e4
LT
1136 client->eg_ops->put(eg_entry);
1137 return;
1138 }
1139 }
1140 if (eg_entry != NULL)
1141 client->eg_ops->put(eg_entry);
1142
1143 /* No luck in the egress cache we must open an ingress SVC */
1144 msg->type = OPEN_INGRESS_SVC;
57100440
JP
1145 if (qos &&
1146 (qos->qos.txtp.traffic_class == msg->qos.txtp.traffic_class)) {
1da177e4 1147 msg->qos = qos->qos;
57100440
JP
1148 pr_info("(%s) trying to get a CBR shortcut\n",
1149 client->dev->name);
1150 } else
1151 memset(&msg->qos, 0, sizeof(struct atm_qos));
1da177e4 1152 msg_to_mpoad(msg, client);
1da177e4
LT
1153}
1154
1155static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1156{
30d492da 1157 __be32 dst_ip = msg->content.in_info.in_dst_ip;
1da177e4 1158 in_cache_entry *entry = mpc->in_ops->get(dst_ip, mpc);
1e4fd51e 1159
b50c2ea7 1160 dprintk("(%s) ip %pI4\n",
21454aaa 1161 mpc->dev->name, &dst_ip);
b50c2ea7 1162 ddprintk("(%s) entry = %p",
57100440
JP
1163 mpc->dev->name, entry);
1164 if (entry == NULL) {
1165 pr_info("(%s) ARGH, received res. reply for an entry that doesn't exist.\n",
1166 mpc->dev->name);
1da177e4
LT
1167 return;
1168 }
b50c2ea7 1169 ddprintk_cont(" entry_state = %d ", entry->entry_state);
1da177e4
LT
1170
1171 if (entry->entry_state == INGRESS_RESOLVED) {
57100440 1172 pr_info("(%s) RESOLVED entry!\n", mpc->dev->name);
1da177e4
LT
1173 mpc->in_ops->put(entry);
1174 return;
1175 }
1176
1177 entry->ctrl_info = msg->content.in_info;
d750dbdc
TR
1178 entry->time = ktime_get_seconds();
1179 /* Used in refreshing func from now on */
1180 entry->reply_wait = ktime_get_seconds();
1da177e4 1181 entry->refresh_time = 0;
b50c2ea7 1182 ddprintk_cont("entry->shortcut = %p\n", entry->shortcut);
1da177e4 1183
57100440
JP
1184 if (entry->entry_state == INGRESS_RESOLVING &&
1185 entry->shortcut != NULL) {
f7d57453 1186 entry->entry_state = INGRESS_RESOLVED;
1da177e4
LT
1187 mpc->in_ops->put(entry);
1188 return; /* Shortcut already open... */
1189 }
1190
1191 if (entry->shortcut != NULL) {
57100440
JP
1192 pr_info("(%s) entry->shortcut != NULL, impossible!\n",
1193 mpc->dev->name);
1da177e4
LT
1194 mpc->in_ops->put(entry);
1195 return;
1196 }
f7d57453 1197
1da177e4
LT
1198 check_qos_and_open_shortcut(msg, mpc, entry);
1199 entry->entry_state = INGRESS_RESOLVED;
1200 mpc->in_ops->put(entry);
1201
1202 return;
1203
1204}
1205
1206static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1207{
30d492da
AV
1208 __be32 dst_ip = msg->content.in_info.in_dst_ip;
1209 __be32 mask = msg->ip_mask;
1da177e4
LT
1210 in_cache_entry *entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1211
57100440
JP
1212 if (entry == NULL) {
1213 pr_info("(%s) purge for a non-existing entry, ip = %pI4\n",
1214 mpc->dev->name, &dst_ip);
1da177e4
LT
1215 return;
1216 }
1217
1218 do {
b50c2ea7 1219 dprintk("(%s) removing an ingress entry, ip = %pI4\n",
21454aaa 1220 mpc->dev->name, &dst_ip);
1da177e4
LT
1221 write_lock_bh(&mpc->ingress_lock);
1222 mpc->in_ops->remove_entry(entry, mpc);
1223 write_unlock_bh(&mpc->ingress_lock);
1224 mpc->in_ops->put(entry);
1225 entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1226 } while (entry != NULL);
f7d57453 1227}
1da177e4
LT
1228
1229static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1230{
30d492da 1231 __be32 cache_id = msg->content.eg_info.cache_id;
1da177e4 1232 eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(cache_id, mpc);
f7d57453 1233
1da177e4 1234 if (entry == NULL) {
b50c2ea7 1235 dprintk("(%s) purge for a non-existing entry\n",
57100440 1236 mpc->dev->name);
1da177e4
LT
1237 return;
1238 }
1239
1240 write_lock_irq(&mpc->egress_lock);
1241 mpc->eg_ops->remove_entry(entry, mpc);
1242 write_unlock_irq(&mpc->egress_lock);
1243
1244 mpc->eg_ops->put(entry);
f7d57453 1245}
1da177e4
LT
1246
1247static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry)
1248{
1249 struct sock *sk;
1250 struct k_message *purge_msg;
1251 struct sk_buff *skb;
1252
b50c2ea7 1253 dprintk("entering\n");
1da177e4 1254 if (vcc == NULL) {
57100440 1255 pr_info("vcc == NULL\n");
1da177e4
LT
1256 return;
1257 }
1258
1259 skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
1260 if (skb == NULL) {
57100440 1261 pr_info("out of memory\n");
1da177e4
LT
1262 return;
1263 }
1264
1265 skb_put(skb, sizeof(struct k_message));
1266 memset(skb->data, 0, sizeof(struct k_message));
1267 purge_msg = (struct k_message *)skb->data;
1268 purge_msg->type = DATA_PLANE_PURGE;
1269 if (entry != NULL)
1270 purge_msg->content.eg_info = entry->ctrl_info;
1271
1272 atm_force_charge(vcc, skb->truesize);
1273
1274 sk = sk_atm(vcc);
1275 skb_queue_tail(&sk->sk_receive_queue, skb);
676d2369 1276 sk->sk_data_ready(sk);
b50c2ea7 1277 dprintk("exiting\n");
1da177e4
LT
1278}
1279
1280/*
1281 * Our MPS died. Tell our daemon to send NHRP data plane purge to each
1282 * of the egress shortcuts we have.
1283 */
57100440 1284static void mps_death(struct k_message *msg, struct mpoa_client *mpc)
1da177e4
LT
1285{
1286 eg_cache_entry *entry;
1287
b50c2ea7 1288 dprintk("(%s)\n", mpc->dev->name);
1da177e4 1289
57100440
JP
1290 if (memcmp(msg->MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN)) {
1291 pr_info("(%s) wrong MPS\n", mpc->dev->name);
1da177e4
LT
1292 return;
1293 }
1294
1295 /* FIXME: This knows too much of the cache structure */
1296 read_lock_irq(&mpc->egress_lock);
1297 entry = mpc->eg_cache;
1298 while (entry != NULL) {
1299 purge_egress_shortcut(entry->shortcut, entry);
1300 entry = entry->next;
1301 }
1302 read_unlock_irq(&mpc->egress_lock);
1303
1304 mpc->in_ops->destroy_cache(mpc);
1305 mpc->eg_ops->destroy_cache(mpc);
1da177e4
LT
1306}
1307
57100440
JP
1308static void MPOA_cache_impos_rcvd(struct k_message *msg,
1309 struct mpoa_client *mpc)
1da177e4
LT
1310{
1311 uint16_t holding_time;
1312 eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(msg->content.eg_info.cache_id, mpc);
f7d57453 1313
1da177e4 1314 holding_time = msg->content.eg_info.holding_time;
b50c2ea7 1315 dprintk("(%s) entry = %p, holding_time = %u\n",
57100440
JP
1316 mpc->dev->name, entry, holding_time);
1317 if (entry == NULL && holding_time) {
1da177e4
LT
1318 entry = mpc->eg_ops->add_entry(msg, mpc);
1319 mpc->eg_ops->put(entry);
1320 return;
1321 }
57100440 1322 if (holding_time) {
1da177e4
LT
1323 mpc->eg_ops->update(entry, holding_time);
1324 return;
1325 }
f7d57453 1326
1da177e4
LT
1327 write_lock_irq(&mpc->egress_lock);
1328 mpc->eg_ops->remove_entry(entry, mpc);
1329 write_unlock_irq(&mpc->egress_lock);
1330
1331 mpc->eg_ops->put(entry);
1da177e4
LT
1332}
1333
57100440
JP
1334static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg,
1335 struct mpoa_client *mpc)
1da177e4
LT
1336{
1337 struct lec_priv *priv;
1338 int i, retval ;
1339
1340 uint8_t tlv[4 + 1 + 1 + 1 + ATM_ESA_LEN];
1341
1342 tlv[0] = 00; tlv[1] = 0xa0; tlv[2] = 0x3e; tlv[3] = 0x2a; /* type */
1343 tlv[4] = 1 + 1 + ATM_ESA_LEN; /* length */
1344 tlv[5] = 0x02; /* MPOA client */
1345 tlv[6] = 0x00; /* number of MPS MAC addresses */
1346
1347 memcpy(&tlv[7], mesg->MPS_ctrl, ATM_ESA_LEN); /* MPC ctrl ATM addr */
1348 memcpy(mpc->our_ctrl_addr, mesg->MPS_ctrl, ATM_ESA_LEN);
1349
b50c2ea7
JP
1350 dprintk("(%s) setting MPC ctrl ATM address to",
1351 mpc->dev ? mpc->dev->name : "<unknown>");
1da177e4 1352 for (i = 7; i < sizeof(tlv); i++)
b50c2ea7
JP
1353 dprintk_cont(" %02x", tlv[i]);
1354 dprintk_cont("\n");
1da177e4
LT
1355
1356 if (mpc->dev) {
524ad0a7 1357 priv = netdev_priv(mpc->dev);
57100440
JP
1358 retval = priv->lane2_ops->associate_req(mpc->dev,
1359 mpc->dev->dev_addr,
1360 tlv, sizeof(tlv));
1da177e4 1361 if (retval == 0)
57100440
JP
1362 pr_info("(%s) MPOA device type TLV association failed\n",
1363 mpc->dev->name);
1da177e4
LT
1364 retval = priv->lane2_ops->resolve(mpc->dev, NULL, 1, NULL, NULL);
1365 if (retval < 0)
57100440
JP
1366 pr_info("(%s) targetless LE_ARP request failed\n",
1367 mpc->dev->name);
1da177e4 1368 }
1da177e4
LT
1369}
1370
57100440
JP
1371static void set_mps_mac_addr_rcvd(struct k_message *msg,
1372 struct mpoa_client *client)
1da177e4
LT
1373{
1374
57100440 1375 if (client->number_of_mps_macs)
1da177e4
LT
1376 kfree(client->mps_macs);
1377 client->number_of_mps_macs = 0;
2afe37cd 1378 client->mps_macs = kmemdup(msg->MPS_ctrl, ETH_ALEN, GFP_KERNEL);
1da177e4 1379 if (client->mps_macs == NULL) {
57100440 1380 pr_info("out of memory\n");
1da177e4
LT
1381 return;
1382 }
1383 client->number_of_mps_macs = 1;
1da177e4
LT
1384}
1385
1386/*
1387 * purge egress cache and tell daemon to 'action' (DIE, RELOAD)
1388 */
1389static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action)
1390{
1391
1392 eg_cache_entry *entry;
1393 msg->type = SND_EGRESS_PURGE;
1394
1395
1396 /* FIXME: This knows too much of the cache structure */
1397 read_lock_irq(&mpc->egress_lock);
1398 entry = mpc->eg_cache;
57100440
JP
1399 while (entry != NULL) {
1400 msg->content.eg_info = entry->ctrl_info;
b50c2ea7 1401 dprintk("cache_id %u\n", entry->ctrl_info.cache_id);
57100440
JP
1402 msg_to_mpoad(msg, mpc);
1403 entry = entry->next;
1da177e4
LT
1404 }
1405 read_unlock_irq(&mpc->egress_lock);
1406
1407 msg->type = action;
1408 msg_to_mpoad(msg, mpc);
1da177e4
LT
1409}
1410
5c658e19
KC
1411static unsigned long checking_time;
1412
1da177e4
LT
1413static void mpc_timer_refresh(void)
1414{
1415 mpc_timer.expires = jiffies + (MPC_P2 * HZ);
5c658e19 1416 checking_time = mpc_timer.expires;
1da177e4 1417 add_timer(&mpc_timer);
1da177e4
LT
1418}
1419
5c658e19 1420static void mpc_cache_check(struct timer_list *unused)
1da177e4
LT
1421{
1422 struct mpoa_client *mpc = mpcs;
1423 static unsigned long previous_resolving_check_time;
1424 static unsigned long previous_refresh_time;
f7d57453 1425
57100440 1426 while (mpc != NULL) {
1da177e4
LT
1427 mpc->in_ops->clear_count(mpc);
1428 mpc->eg_ops->clear_expired(mpc);
57100440
JP
1429 if (checking_time - previous_resolving_check_time >
1430 mpc->parameters.mpc_p4 * HZ) {
1da177e4
LT
1431 mpc->in_ops->check_resolving(mpc);
1432 previous_resolving_check_time = checking_time;
1433 }
57100440
JP
1434 if (checking_time - previous_refresh_time >
1435 mpc->parameters.mpc_p5 * HZ) {
1da177e4
LT
1436 mpc->in_ops->refresh(mpc);
1437 previous_refresh_time = checking_time;
1438 }
1439 mpc = mpc->next;
1440 }
1441 mpc_timer_refresh();
1da177e4
LT
1442}
1443
57100440
JP
1444static int atm_mpoa_ioctl(struct socket *sock, unsigned int cmd,
1445 unsigned long arg)
1da177e4
LT
1446{
1447 int err = 0;
1448 struct atm_vcc *vcc = ATM_SD(sock);
1449
1450 if (cmd != ATMMPC_CTRL && cmd != ATMMPC_DATA)
1451 return -ENOIOCTLCMD;
1452
1453 if (!capable(CAP_NET_ADMIN))
1454 return -EPERM;
1455
1456 switch (cmd) {
57100440
JP
1457 case ATMMPC_CTRL:
1458 err = atm_mpoa_mpoad_attach(vcc, (int)arg);
1459 if (err >= 0)
1460 sock->state = SS_CONNECTED;
1461 break;
1462 case ATMMPC_DATA:
1463 err = atm_mpoa_vcc_attach(vcc, (void __user *)arg);
1464 break;
1465 default:
1466 break;
1da177e4
LT
1467 }
1468 return err;
1469}
1470
1da177e4
LT
1471static struct atm_ioctl atm_ioctl_ops = {
1472 .owner = THIS_MODULE,
1473 .ioctl = atm_mpoa_ioctl,
1474};
1475
1476static __init int atm_mpoa_init(void)
1477{
1478 register_atm_ioctl(&atm_ioctl_ops);
1479
1da177e4 1480 if (mpc_proc_init() != 0)
99824461 1481 pr_info("failed to initialize /proc/mpoa\n");
1da177e4 1482
36a9f77e 1483 pr_info("mpc.c: initialized\n");
1da177e4
LT
1484
1485 return 0;
1486}
1487
1488static void __exit atm_mpoa_cleanup(void)
1489{
1490 struct mpoa_client *mpc, *tmp;
1491 struct atm_mpoa_qos *qos, *nextqos;
1492 struct lec_priv *priv;
1493
1da177e4 1494 mpc_proc_clean();
1da177e4 1495
84275593 1496 del_timer_sync(&mpc_timer);
1da177e4
LT
1497 unregister_netdevice_notifier(&mpoa_notifier);
1498 deregister_atm_ioctl(&atm_ioctl_ops);
1499
1500 mpc = mpcs;
1501 mpcs = NULL;
1502 while (mpc != NULL) {
1503 tmp = mpc->next;
1504 if (mpc->dev != NULL) {
1505 stop_mpc(mpc);
524ad0a7 1506 priv = netdev_priv(mpc->dev);
1da177e4
LT
1507 if (priv->lane2_ops != NULL)
1508 priv->lane2_ops->associate_indicator = NULL;
1509 }
b50c2ea7 1510 ddprintk("about to clear caches\n");
1da177e4
LT
1511 mpc->in_ops->destroy_cache(mpc);
1512 mpc->eg_ops->destroy_cache(mpc);
b50c2ea7 1513 ddprintk("caches cleared\n");
1da177e4
LT
1514 kfree(mpc->mps_macs);
1515 memset(mpc, 0, sizeof(struct mpoa_client));
b50c2ea7 1516 ddprintk("about to kfree %p\n", mpc);
1da177e4 1517 kfree(mpc);
b50c2ea7 1518 ddprintk("next mpc is at %p\n", tmp);
1da177e4
LT
1519 mpc = tmp;
1520 }
1521
1522 qos = qos_head;
1523 qos_head = NULL;
1524 while (qos != NULL) {
1525 nextqos = qos->next;
b50c2ea7 1526 dprintk("freeing qos entry %p\n", qos);
1da177e4
LT
1527 kfree(qos);
1528 qos = nextqos;
1529 }
1da177e4
LT
1530}
1531
1532module_init(atm_mpoa_init);
1533module_exit(atm_mpoa_cleanup);
1534
1535MODULE_LICENSE("GPL");