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