blob: 74bcc662c3dd8c5e7ec33a393560cc9bbf313b57 [file] [log] [blame]
Joe Perches99824462010-01-26 11:40:00 +00001#define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
2
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/kernel.h>
4#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/timer.h>
7#include <linux/init.h>
8#include <linux/bitops.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -08009#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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>
Joe Perches57100442010-01-26 11:40:09 +000019#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/byteorder.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32
33#include "lec.h"
34#include "mpc.h"
35#include "resources.h"
36
37/*
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +090038 * mpc.c: Implementation of MPOA client kernel part
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
40
41#if 0
Joe Perchesb50c2ea2010-01-26 11:40:20 +000042#define dprintk(format, args...) \
43 printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args)
44#define dprintk_cont(format, args...) printk(KERN_CONT format, ##args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#else
Joe Perchesb50c2ea2010-01-26 11:40:20 +000046#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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#endif
53
54#if 0
Joe Perchesb50c2ea2010-01-26 11:40:20 +000055#define ddprintk(format, args...) \
56 printk(KERN_DEBUG "mpoa:%s: " format, __func__, ##args)
57#define ddprintk_cont(format, args...) printk(KERN_CONT format, ##args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#else
Joe Perchesb50c2ea2010-01-26 11:40:20 +000059#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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#endif
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define MPOA_TAG_LEN 4
68
69/* mpc_daemon -> kernel */
Joe Perches57100442010-01-26 11:40:09 +000070static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc);
72static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
73static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc);
74static void mps_death(struct k_message *msg, struct mpoa_client *mpc);
Joe Perches57100442010-01-26 11:40:09 +000075static void clean_up(struct k_message *msg, struct mpoa_client *mpc,
76 int action);
77static void MPOA_cache_impos_rcvd(struct k_message *msg,
78 struct mpoa_client *mpc);
79static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg,
80 struct mpoa_client *mpc);
81static void set_mps_mac_addr_rcvd(struct k_message *mesg,
82 struct mpoa_client *mpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
David Howellscba5cbd2008-07-30 16:31:46 -070084static const uint8_t *copy_macs(struct mpoa_client *mpc,
85 const uint8_t *router_mac,
86 const uint8_t *tlvs, uint8_t mps_macs,
87 uint8_t device_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry);
89
David Howellscba5cbd2008-07-30 16:31:46 -070090static void send_set_mps_ctrl_addr(const char *addr, struct mpoa_client *mpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static void mpoad_close(struct atm_vcc *vcc);
92static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb);
93
94static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb);
Stephen Hemminger3c805a22009-08-31 19:50:42 +000095static netdev_tx_t mpc_send_packet(struct sk_buff *skb,
Joe Perches57100442010-01-26 11:40:09 +000096 struct net_device *dev);
97static int mpoa_event_listener(struct notifier_block *mpoa_notifier,
98 unsigned long event, void *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static void mpc_timer_refresh(void);
Joe Perches57100442010-01-26 11:40:09 +0000100static void mpc_cache_check(unsigned long checking_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102static struct llc_snap_hdr llc_snap_mpoa_ctrl = {
103 0xaa, 0xaa, 0x03,
104 {0x00, 0x00, 0x5e},
105 {0x00, 0x03} /* For MPOA control PDUs */
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900106};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static struct llc_snap_hdr llc_snap_mpoa_data = {
108 0xaa, 0xaa, 0x03,
109 {0x00, 0x00, 0x00},
110 {0x08, 0x00} /* This is for IP PDUs only */
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900111};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static struct llc_snap_hdr llc_snap_mpoa_data_tagged = {
113 0xaa, 0xaa, 0x03,
114 {0x00, 0x00, 0x00},
115 {0x88, 0x4c} /* This is for tagged data PDUs */
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900116};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118static struct notifier_block mpoa_notifier = {
119 mpoa_event_listener,
120 NULL,
121 0
122};
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124struct mpoa_client *mpcs = NULL; /* FIXME */
125static struct atm_mpoa_qos *qos_head = NULL;
Ingo Molnar8d06afa2005-09-09 13:10:40 -0700126static DEFINE_TIMER(mpc_timer, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128
129static struct mpoa_client *find_mpc_by_itfnum(int itf)
130{
131 struct mpoa_client *mpc;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 mpc = mpcs; /* our global linked list */
134 while (mpc != NULL) {
135 if (mpc->dev_num == itf)
136 return mpc;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900137 mpc = mpc->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139
140 return NULL; /* not found */
141}
142
143static struct mpoa_client *find_mpc_by_vcc(struct atm_vcc *vcc)
144{
145 struct mpoa_client *mpc;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 mpc = mpcs; /* our global linked list */
148 while (mpc != NULL) {
149 if (mpc->mpoad_vcc == vcc)
150 return mpc;
151 mpc = mpc->next;
152 }
153
154 return NULL; /* not found */
155}
156
157static struct mpoa_client *find_mpc_by_lec(struct net_device *dev)
158{
159 struct mpoa_client *mpc;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 mpc = mpcs; /* our global linked list */
162 while (mpc != NULL) {
163 if (mpc->dev == dev)
164 return mpc;
165 mpc = mpc->next;
166 }
167
168 return NULL; /* not found */
169}
170
171/*
172 * Functions for managing QoS list
173 */
174
175/*
176 * Overwrites the old entry or makes a new one.
177 */
Al Viro30d492d2006-11-14 21:11:29 -0800178struct atm_mpoa_qos *atm_mpoa_add_qos(__be32 dst_ip, struct atm_qos *qos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct atm_mpoa_qos *entry;
181
182 entry = atm_mpoa_search_qos(dst_ip);
183 if (entry != NULL) {
184 entry->qos = *qos;
185 return entry;
186 }
187
188 entry = kmalloc(sizeof(struct atm_mpoa_qos), GFP_KERNEL);
189 if (entry == NULL) {
Joe Perches57100442010-01-26 11:40:09 +0000190 pr_info("mpoa: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return entry;
192 }
193
194 entry->ipaddr = dst_ip;
195 entry->qos = *qos;
196
197 entry->next = qos_head;
198 qos_head = entry;
199
200 return entry;
201}
202
Al Viro30d492d2006-11-14 21:11:29 -0800203struct atm_mpoa_qos *atm_mpoa_search_qos(__be32 dst_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 struct atm_mpoa_qos *qos;
206
207 qos = qos_head;
Joe Perches57100442010-01-26 11:40:09 +0000208 while (qos) {
209 if (qos->ipaddr == dst_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 qos = qos->next;
212 }
213
214 return qos;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900215}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217/*
218 * Returns 0 for failure
219 */
220int atm_mpoa_delete_qos(struct atm_mpoa_qos *entry)
221{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 struct atm_mpoa_qos *curr;
223
Joe Perches57100442010-01-26 11:40:09 +0000224 if (entry == NULL)
225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (entry == qos_head) {
227 qos_head = qos_head->next;
228 kfree(entry);
229 return 1;
230 }
231
232 curr = qos_head;
233 while (curr != NULL) {
234 if (curr->next == entry) {
235 curr->next = entry->next;
236 kfree(entry);
237 return 1;
238 }
239 curr = curr->next;
240 }
241
242 return 0;
243}
244
245/* this is buggered - we need locking for qos_head */
246void atm_mpoa_disp_qos(struct seq_file *m)
247{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 struct atm_mpoa_qos *qos;
249
250 qos = qos_head;
251 seq_printf(m, "QoS entries for shortcuts:\n");
252 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");
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 while (qos != NULL) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700255 seq_printf(m, "%pI4\n %-7d %-7d %-7d %-7d %-7d\n %-7d %-7d %-7d %-7d %-7d\n",
Joe Perches57100442010-01-26 11:40:09 +0000256 &qos->ipaddr,
257 qos->qos.txtp.max_pcr,
258 qos->qos.txtp.pcr,
259 qos->qos.txtp.min_pcr,
260 qos->qos.txtp.max_cdv,
261 qos->qos.txtp.max_sdu,
262 qos->qos.rxtp.max_pcr,
263 qos->qos.rxtp.pcr,
264 qos->qos.rxtp.min_pcr,
265 qos->qos.rxtp.max_cdv,
266 qos->qos.rxtp.max_sdu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 qos = qos->next;
268 }
269}
270
271static struct net_device *find_lec_by_itfnum(int itf)
272{
273 struct net_device *dev;
274 char name[IFNAMSIZ];
275
276 sprintf(name, "lec%d", itf);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700277 dev = dev_get_by_name(&init_net, name);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return dev;
280}
281
282static struct mpoa_client *alloc_mpc(void)
283{
284 struct mpoa_client *mpc;
285
Joe Perches57100442010-01-26 11:40:09 +0000286 mpc = kzalloc(sizeof(struct mpoa_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (mpc == NULL)
288 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 rwlock_init(&mpc->ingress_lock);
290 rwlock_init(&mpc->egress_lock);
291 mpc->next = mpcs;
292 atm_mpoa_init_cache(mpc);
293
294 mpc->parameters.mpc_p1 = MPC_P1;
295 mpc->parameters.mpc_p2 = MPC_P2;
Joe Perches57100442010-01-26 11:40:09 +0000296 memset(mpc->parameters.mpc_p3, 0, sizeof(mpc->parameters.mpc_p3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 mpc->parameters.mpc_p4 = MPC_P4;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900298 mpc->parameters.mpc_p5 = MPC_P5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 mpc->parameters.mpc_p6 = MPC_P6;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 mpcs = mpc;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return mpc;
304}
305
306/*
307 *
308 * start_mpc() puts the MPC on line. All the packets destined
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900309 * to the lec underneath us are now being monitored and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * shortcuts will be established.
311 *
312 */
313static void start_mpc(struct mpoa_client *mpc, struct net_device *dev)
314{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900315
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000316 dprintk("(%s)\n", mpc->dev->name);
Stephen Hemminger788dee02009-03-20 19:35:28 +0000317 if (!dev->netdev_ops)
Joe Perches57100442010-01-26 11:40:09 +0000318 pr_info("(%s) not starting\n", dev->name);
Stephen Hemminger788dee02009-03-20 19:35:28 +0000319 else {
320 mpc->old_ops = dev->netdev_ops;
321 mpc->new_ops = *mpc->old_ops;
322 mpc->new_ops.ndo_start_xmit = mpc_send_packet;
323 dev->netdev_ops = &mpc->new_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
327static void stop_mpc(struct mpoa_client *mpc)
328{
Stephen Hemminger788dee02009-03-20 19:35:28 +0000329 struct net_device *dev = mpc->dev;
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000330 dprintk("(%s)", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* Lets not nullify lec device's dev->hard_start_xmit */
Stephen Hemminger788dee02009-03-20 19:35:28 +0000333 if (dev->netdev_ops != &mpc->new_ops) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000334 dprintk_cont(" mpc already stopped, not fatal\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return;
336 }
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000337 dprintk_cont("\n");
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900338
Stephen Hemminger788dee02009-03-20 19:35:28 +0000339 dev->netdev_ops = mpc->old_ops;
340 mpc->old_ops = NULL;
341
342 /* close_shortcuts(mpc); ??? FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345static const char *mpoa_device_type_string(char type) __attribute__ ((unused));
346
347static const char *mpoa_device_type_string(char type)
348{
Joe Perches57100442010-01-26 11:40:09 +0000349 switch (type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 case NON_MPOA:
351 return "non-MPOA device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 case MPS:
353 return "MPS";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 case MPC:
355 return "MPC";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 case MPS_AND_MPC:
357 return "both MPS and MPC";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
Joe Perches57100442010-01-26 11:40:09 +0000360 return "unspecified (non-MPOA) device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363/*
Wang Chenb74ca3a2008-12-08 01:14:16 -0800364 * lec device calls this via its netdev_priv(dev)->lane2_ops
365 * ->associate_indicator() when it sees a TLV in LE_ARP packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * We fill in the pointer above when we see a LANE2 lec initializing
367 * See LANE2 spec 3.1.5
368 *
369 * Quite a big and ugly function but when you look at it
370 * all it does is to try to locate and parse MPOA Device
371 * Type TLV.
372 * We give our lec a pointer to this function and when the
373 * lec sees a TLV it uses the pointer to call this function.
374 *
375 */
David Howellscba5cbd2008-07-30 16:31:46 -0700376static void lane2_assoc_ind(struct net_device *dev, const u8 *mac_addr,
377 const u8 *tlvs, u32 sizeoftlvs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 uint32_t type;
380 uint8_t length, mpoa_device_type, number_of_mps_macs;
David Howellscba5cbd2008-07-30 16:31:46 -0700381 const uint8_t *end_of_tlvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct mpoa_client *mpc;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 mpoa_device_type = number_of_mps_macs = 0; /* silence gcc */
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000385 dprintk("(%s) received TLV(s), ", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 dprintk("total length of all TLVs %d\n", sizeoftlvs);
387 mpc = find_mpc_by_lec(dev); /* Sampo-Fix: moved here from below */
388 if (mpc == NULL) {
Joe Perches57100442010-01-26 11:40:09 +0000389 pr_info("(%s) no mpc\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return;
391 }
392 end_of_tlvs = tlvs + sizeoftlvs;
393 while (end_of_tlvs - tlvs >= 5) {
Joe Perches57100442010-01-26 11:40:09 +0000394 type = ((tlvs[0] << 24) | (tlvs[1] << 16) |
395 (tlvs[2] << 8) | tlvs[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 length = tlvs[4];
397 tlvs += 5;
398 dprintk(" type 0x%x length %02x\n", type, length);
399 if (tlvs + length > end_of_tlvs) {
Joe Perches57100442010-01-26 11:40:09 +0000400 pr_info("TLV value extends past its buffer, aborting parse\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return;
402 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 if (type == 0) {
Joe Perches57100442010-01-26 11:40:09 +0000405 pr_info("mpoa: (%s) TLV type was 0, returning\n",
406 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return;
408 }
409
410 if (type != TLV_MPOA_DEVICE_TYPE) {
411 tlvs += length;
412 continue; /* skip other TLVs */
413 }
414 mpoa_device_type = *tlvs++;
415 number_of_mps_macs = *tlvs++;
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000416 dprintk("(%s) MPOA device type '%s', ",
Joe Perches57100442010-01-26 11:40:09 +0000417 dev->name, mpoa_device_type_string(mpoa_device_type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (mpoa_device_type == MPS_AND_MPC &&
419 length < (42 + number_of_mps_macs*ETH_ALEN)) { /* :) */
Joe Perches57100442010-01-26 11:40:09 +0000420 pr_info("(%s) short MPOA Device Type TLV\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 dev->name);
422 continue;
423 }
Joe Perches57100442010-01-26 11:40:09 +0000424 if ((mpoa_device_type == MPS || mpoa_device_type == MPC) &&
425 length < 22 + number_of_mps_macs*ETH_ALEN) {
426 pr_info("(%s) short MPOA Device Type TLV\n", dev->name);
427 continue;
428 }
429 if (mpoa_device_type != MPS &&
430 mpoa_device_type != MPS_AND_MPC) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000431 dprintk("ignoring non-MPS device ");
Joe Perches57100442010-01-26 11:40:09 +0000432 if (mpoa_device_type == MPC)
433 tlvs += 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 continue; /* we are only interested in MPSs */
435 }
Joe Perches57100442010-01-26 11:40:09 +0000436 if (number_of_mps_macs == 0 &&
437 mpoa_device_type == MPS_AND_MPC) {
438 pr_info("(%s) MPS_AND_MPC has zero MACs\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 continue; /* someone should read the spec */
440 }
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000441 dprintk_cont("this MPS has %d MAC addresses\n",
442 number_of_mps_macs);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900443
Joe Perches57100442010-01-26 11:40:09 +0000444 /*
445 * ok, now we can go and tell our daemon
446 * the control address of MPS
447 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 send_set_mps_ctrl_addr(tlvs, mpc);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900449
Joe Perches57100442010-01-26 11:40:09 +0000450 tlvs = copy_macs(mpc, mac_addr, tlvs,
451 number_of_mps_macs, mpoa_device_type);
452 if (tlvs == NULL)
453 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455 if (end_of_tlvs - tlvs != 0)
Joe Perches57100442010-01-26 11:40:09 +0000456 pr_info("(%s) ignoring %Zd bytes of trailing TLV garbage\n",
457 dev->name, end_of_tlvs - tlvs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460/*
461 * Store at least advertizing router's MAC address
462 * plus the possible MAC address(es) to mpc->mps_macs.
463 * For a freshly allocated MPOA client mpc->mps_macs == 0.
464 */
David Howellscba5cbd2008-07-30 16:31:46 -0700465static const uint8_t *copy_macs(struct mpoa_client *mpc,
466 const uint8_t *router_mac,
467 const uint8_t *tlvs, uint8_t mps_macs,
468 uint8_t device_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 int num_macs;
471 num_macs = (mps_macs > 1) ? mps_macs : 1;
472
473 if (mpc->number_of_mps_macs != num_macs) { /* need to reallocate? */
Joe Perches57100442010-01-26 11:40:09 +0000474 if (mpc->number_of_mps_macs != 0)
475 kfree(mpc->mps_macs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 mpc->number_of_mps_macs = 0;
Joe Perches57100442010-01-26 11:40:09 +0000477 mpc->mps_macs = kmalloc(num_macs * ETH_ALEN, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (mpc->mps_macs == NULL) {
Joe Perches57100442010-01-26 11:40:09 +0000479 pr_info("(%s) out of mem\n", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return NULL;
481 }
482 }
483 memcpy(mpc->mps_macs, router_mac, ETH_ALEN);
484 tlvs += 20; if (device_type == MPS_AND_MPC) tlvs += 20;
485 if (mps_macs > 0)
486 memcpy(mpc->mps_macs, tlvs, mps_macs*ETH_ALEN);
487 tlvs += mps_macs*ETH_ALEN;
488 mpc->number_of_mps_macs = num_macs;
489
490 return tlvs;
491}
492
493static int send_via_shortcut(struct sk_buff *skb, struct mpoa_client *mpc)
494{
495 in_cache_entry *entry;
496 struct iphdr *iph;
497 char *buff;
Al Viro30d492d2006-11-14 21:11:29 -0800498 __be32 ipaddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 static struct {
501 struct llc_snap_hdr hdr;
Al Viro30d492d2006-11-14 21:11:29 -0800502 __be32 tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 } tagged_llc_snap_hdr = {
504 {0xaa, 0xaa, 0x03, {0x00, 0x00, 0x00}, {0x88, 0x4c}},
505 0
506 };
507
508 buff = skb->data + mpc->dev->hard_header_len;
509 iph = (struct iphdr *)buff;
510 ipaddr = iph->daddr;
511
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000512 ddprintk("(%s) ipaddr 0x%x\n",
Joe Perches57100442010-01-26 11:40:09 +0000513 mpc->dev->name, ipaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 entry = mpc->in_ops->get(ipaddr, mpc);
516 if (entry == NULL) {
517 entry = mpc->in_ops->add_entry(ipaddr, mpc);
Joe Perches57100442010-01-26 11:40:09 +0000518 if (entry != NULL)
519 mpc->in_ops->put(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return 1;
521 }
Joe Perches57100442010-01-26 11:40:09 +0000522 /* threshold not exceeded or VCC not ready */
523 if (mpc->in_ops->cache_hit(entry, mpc) != OPEN) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000524 ddprintk("(%s) cache_hit: returns != OPEN\n",
Joe Perches57100442010-01-26 11:40:09 +0000525 mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 mpc->in_ops->put(entry);
527 return 1;
528 }
529
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000530 ddprintk("(%s) using shortcut\n",
Joe Perches57100442010-01-26 11:40:09 +0000531 mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* MPOA spec A.1.4, MPOA client must decrement IP ttl at least by one */
533 if (iph->ttl <= 1) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000534 ddprintk("(%s) IP ttl = %u, using LANE\n",
Joe Perches57100442010-01-26 11:40:09 +0000535 mpc->dev->name, iph->ttl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 mpc->in_ops->put(entry);
537 return 1;
538 }
539 iph->ttl--;
540 iph->check = 0;
541 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
542
543 if (entry->ctrl_info.tag != 0) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000544 ddprintk("(%s) adding tag 0x%x\n",
Joe Perches57100442010-01-26 11:40:09 +0000545 mpc->dev->name, entry->ctrl_info.tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 tagged_llc_snap_hdr.tag = entry->ctrl_info.tag;
Joe Perches57100442010-01-26 11:40:09 +0000547 skb_pull(skb, ETH_HLEN); /* get rid of Eth header */
548 skb_push(skb, sizeof(tagged_llc_snap_hdr));
549 /* add LLC/SNAP header */
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300550 skb_copy_to_linear_data(skb, &tagged_llc_snap_hdr,
551 sizeof(tagged_llc_snap_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 } else {
Joe Perches57100442010-01-26 11:40:09 +0000553 skb_pull(skb, ETH_HLEN); /* get rid of Eth header */
554 skb_push(skb, sizeof(struct llc_snap_hdr));
555 /* add LLC/SNAP header + tag */
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300556 skb_copy_to_linear_data(skb, &llc_snap_mpoa_data,
557 sizeof(struct llc_snap_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
560 atomic_add(skb->truesize, &sk_atm(entry->shortcut)->sk_wmem_alloc);
561 ATM_SKB(skb)->atm_options = entry->shortcut->atm_options;
562 entry->shortcut->send(entry->shortcut, skb);
563 entry->packets_fwded++;
564 mpc->in_ops->put(entry);
565
566 return 0;
567}
568
569/*
570 * Probably needs some error checks and locking, not sure...
571 */
Stephen Hemminger3c805a22009-08-31 19:50:42 +0000572static netdev_tx_t mpc_send_packet(struct sk_buff *skb,
573 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct mpoa_client *mpc;
576 struct ethhdr *eth;
577 int i = 0;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 mpc = find_mpc_by_lec(dev); /* this should NEVER fail */
Joe Perches57100442010-01-26 11:40:09 +0000580 if (mpc == NULL) {
581 pr_info("(%s) no MPC found\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 goto non_ip;
583 }
584
585 eth = (struct ethhdr *)skb->data;
586 if (eth->h_proto != htons(ETH_P_IP))
587 goto non_ip; /* Multi-Protocol Over ATM :-) */
588
Herbert Xu1c9b7aa2008-01-09 03:51:59 -0800589 /* Weed out funny packets (e.g., AF_PACKET or raw). */
590 if (skb->len < ETH_HLEN + sizeof(struct iphdr))
591 goto non_ip;
592 skb_set_network_header(skb, ETH_HLEN);
593 if (skb->len < ETH_HLEN + ip_hdr(skb)->ihl * 4 || ip_hdr(skb)->ihl < 5)
594 goto non_ip;
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 while (i < mpc->number_of_mps_macs) {
Joe Perches57100442010-01-26 11:40:09 +0000597 if (!compare_ether_addr(eth->h_dest,
598 (mpc->mps_macs + i*ETH_ALEN)))
599 if (send_via_shortcut(skb, mpc) == 0) /* try shortcut */
600 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 i++;
602 }
603
Joe Perches57100442010-01-26 11:40:09 +0000604non_ip:
605 return mpc->old_ops->ndo_start_xmit(skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608static int atm_mpoa_vcc_attach(struct atm_vcc *vcc, void __user *arg)
609{
610 int bytes_left;
611 struct mpoa_client *mpc;
612 struct atmmpc_ioc ioc_data;
613 in_cache_entry *in_entry;
Al Viro30d492d2006-11-14 21:11:29 -0800614 __be32 ipaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 bytes_left = copy_from_user(&ioc_data, arg, sizeof(struct atmmpc_ioc));
617 if (bytes_left != 0) {
Joe Perches57100442010-01-26 11:40:09 +0000618 pr_info("mpoa:Short read (missed %d bytes) from userland\n",
619 bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return -EFAULT;
621 }
622 ipaddr = ioc_data.ipaddr;
623 if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF)
624 return -EINVAL;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 mpc = find_mpc_by_itfnum(ioc_data.dev_num);
627 if (mpc == NULL)
628 return -EINVAL;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (ioc_data.type == MPC_SOCKET_INGRESS) {
631 in_entry = mpc->in_ops->get(ipaddr, mpc);
Joe Perches57100442010-01-26 11:40:09 +0000632 if (in_entry == NULL ||
633 in_entry->entry_state < INGRESS_RESOLVED) {
634 pr_info("(%s) did not find RESOLVED entry from ingress cache\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 mpc->dev->name);
Joe Perches57100442010-01-26 11:40:09 +0000636 if (in_entry != NULL)
637 mpc->in_ops->put(in_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return -EINVAL;
639 }
Joe Perches57100442010-01-26 11:40:09 +0000640 pr_info("(%s) attaching ingress SVC, entry = %pI4\n",
641 mpc->dev->name, &in_entry->ctrl_info.in_dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 in_entry->shortcut = vcc;
643 mpc->in_ops->put(in_entry);
644 } else {
Joe Perches57100442010-01-26 11:40:09 +0000645 pr_info("(%s) attaching egress SVC\n", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647
648 vcc->proto_data = mpc->dev;
649 vcc->push = mpc_push;
650
651 return 0;
652}
653
654/*
655 *
656 */
657static void mpc_vcc_close(struct atm_vcc *vcc, struct net_device *dev)
658{
659 struct mpoa_client *mpc;
660 in_cache_entry *in_entry;
661 eg_cache_entry *eg_entry;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 mpc = find_mpc_by_lec(dev);
664 if (mpc == NULL) {
Joe Perches57100442010-01-26 11:40:09 +0000665 pr_info("(%s) close for unknown MPC\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return;
667 }
668
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000669 dprintk("(%s)\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 in_entry = mpc->in_ops->get_by_vcc(vcc, mpc);
671 if (in_entry) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000672 dprintk("(%s) ingress SVC closed ip = %pI4\n",
Joe Perches57100442010-01-26 11:40:09 +0000673 mpc->dev->name, &in_entry->ctrl_info.in_dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 in_entry->shortcut = NULL;
675 mpc->in_ops->put(in_entry);
676 }
677 eg_entry = mpc->eg_ops->get_by_vcc(vcc, mpc);
678 if (eg_entry) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000679 dprintk("(%s) egress SVC closed\n", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 eg_entry->shortcut = NULL;
681 mpc->eg_ops->put(eg_entry);
682 }
683
684 if (in_entry == NULL && eg_entry == NULL)
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000685 dprintk("(%s) unused vcc closed\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb)
689{
690 struct net_device *dev = (struct net_device *)vcc->proto_data;
691 struct sk_buff *new_skb;
692 eg_cache_entry *eg;
693 struct mpoa_client *mpc;
Al Viro30d492d2006-11-14 21:11:29 -0800694 __be32 tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 char *tmp;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900696
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000697 ddprintk("(%s)\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (skb == NULL) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000699 dprintk("(%s) null skb, closing VCC\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 mpc_vcc_close(vcc, dev);
701 return;
702 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 skb->dev = dev;
Joe Perches57100442010-01-26 11:40:09 +0000705 if (memcmp(skb->data, &llc_snap_mpoa_ctrl,
706 sizeof(struct llc_snap_hdr)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 struct sock *sk = sk_atm(vcc);
708
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000709 dprintk("(%s) control packet arrived\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* Pass control packets to daemon */
711 skb_queue_tail(&sk->sk_receive_queue, skb);
712 sk->sk_data_ready(sk, skb->len);
713 return;
714 }
715
716 /* data coming over the shortcut */
717 atm_return(vcc, skb->truesize);
718
719 mpc = find_mpc_by_lec(dev);
720 if (mpc == NULL) {
Joe Perches57100442010-01-26 11:40:09 +0000721 pr_info("(%s) unknown MPC\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return;
723 }
724
Joe Perches57100442010-01-26 11:40:09 +0000725 if (memcmp(skb->data, &llc_snap_mpoa_data_tagged,
726 sizeof(struct llc_snap_hdr)) == 0) { /* MPOA tagged data */
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000727 ddprintk("(%s) tagged data packet arrived\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Joe Perches57100442010-01-26 11:40:09 +0000729 } else if (memcmp(skb->data, &llc_snap_mpoa_data,
730 sizeof(struct llc_snap_hdr)) == 0) { /* MPOA data */
731 pr_info("(%s) Unsupported non-tagged data packet arrived. Purging\n",
732 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 dev_kfree_skb_any(skb);
734 return;
735 } else {
Joe Perches57100442010-01-26 11:40:09 +0000736 pr_info("(%s) garbage arrived, purging\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 dev_kfree_skb_any(skb);
738 return;
739 }
740
741 tmp = skb->data + sizeof(struct llc_snap_hdr);
Al Viro30d492d2006-11-14 21:11:29 -0800742 tag = *(__be32 *)tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 eg = mpc->eg_ops->get_by_tag(tag, mpc);
745 if (eg == NULL) {
Joe Perches57100442010-01-26 11:40:09 +0000746 pr_info("mpoa: (%s) Didn't find egress cache entry, tag = %u\n",
747 dev->name, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 purge_egress_shortcut(vcc, NULL);
749 dev_kfree_skb_any(skb);
750 return;
751 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 /*
754 * See if ingress MPC is using shortcut we opened as a return channel.
755 * This means we have a bi-directional vcc opened by us.
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900756 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (eg->shortcut == NULL) {
758 eg->shortcut = vcc;
Joe Perches57100442010-01-26 11:40:09 +0000759 pr_info("(%s) egress SVC in use\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761
Joe Perches57100442010-01-26 11:40:09 +0000762 skb_pull(skb, sizeof(struct llc_snap_hdr) + sizeof(tag));
763 /* get rid of LLC/SNAP header */
764 new_skb = skb_realloc_headroom(skb, eg->ctrl_info.DH_length);
765 /* LLC/SNAP is shorter than MAC header :( */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 dev_kfree_skb_any(skb);
Joe Perches57100442010-01-26 11:40:09 +0000767 if (new_skb == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 mpc->eg_ops->put(eg);
769 return;
770 }
771 skb_push(new_skb, eg->ctrl_info.DH_length); /* add MAC header */
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300772 skb_copy_to_linear_data(new_skb, eg->ctrl_info.DLL_header,
773 eg->ctrl_info.DH_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 new_skb->protocol = eth_type_trans(new_skb, dev);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700775 skb_reset_network_header(new_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700777 eg->latest_ip_addr = ip_hdr(new_skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 eg->packets_rcvd++;
779 mpc->eg_ops->put(eg);
780
Jiri Slaby5518b292010-10-10 22:46:34 +0000781 memset(ATM_SKB(new_skb), 0, sizeof(struct atm_skb_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 netif_rx(new_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
785static struct atmdev_ops mpc_ops = { /* only send is required */
786 .close = mpoad_close,
787 .send = msg_from_mpoad
788};
789
790static struct atm_dev mpc_dev = {
791 .ops = &mpc_ops,
792 .type = "mpc",
793 .number = 42,
Milind Arun Choudhary4ef8d0a2007-04-26 01:37:44 -0700794 .lock = __SPIN_LOCK_UNLOCKED(mpc_dev.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /* members not explicitly initialised will be 0 */
796};
797
Joe Perches57100442010-01-26 11:40:09 +0000798static int atm_mpoa_mpoad_attach(struct atm_vcc *vcc, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 struct mpoa_client *mpc;
801 struct lec_priv *priv;
802 int err;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (mpcs == NULL) {
805 init_timer(&mpc_timer);
806 mpc_timer_refresh();
807
808 /* This lets us now how our LECs are doing */
809 err = register_netdevice_notifier(&mpoa_notifier);
810 if (err < 0) {
811 del_timer(&mpc_timer);
812 return err;
813 }
814 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 mpc = find_mpc_by_itfnum(arg);
817 if (mpc == NULL) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000818 dprintk("allocating new mpc for itf %d\n", arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 mpc = alloc_mpc();
820 if (mpc == NULL)
821 return -ENOMEM;
822 mpc->dev_num = arg;
Joe Perches57100442010-01-26 11:40:09 +0000823 mpc->dev = find_lec_by_itfnum(arg);
824 /* NULL if there was no lec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826 if (mpc->mpoad_vcc) {
Joe Perches57100442010-01-26 11:40:09 +0000827 pr_info("mpoad is already present for itf %d\n", arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return -EADDRINUSE;
829 }
830
831 if (mpc->dev) { /* check if the lec is LANE2 capable */
Wang Chen524ad0a2008-11-12 23:39:10 -0800832 priv = netdev_priv(mpc->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (priv->lane_version < 2) {
834 dev_put(mpc->dev);
835 mpc->dev = NULL;
836 } else
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900837 priv->lane2_ops->associate_indicator = lane2_assoc_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839
840 mpc->mpoad_vcc = vcc;
841 vcc->dev = &mpc_dev;
842 vcc_insert_socket(sk_atm(vcc));
Joe Perches57100442010-01-26 11:40:09 +0000843 set_bit(ATM_VF_META, &vcc->flags);
844 set_bit(ATM_VF_READY, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 if (mpc->dev) {
847 char empty[ATM_ESA_LEN];
848 memset(empty, 0, ATM_ESA_LEN);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 start_mpc(mpc, mpc->dev);
851 /* set address if mpcd e.g. gets killed and restarted.
852 * If we do not do it now we have to wait for the next LE_ARP
853 */
Joe Perches57100442010-01-26 11:40:09 +0000854 if (memcmp(mpc->mps_ctrl_addr, empty, ATM_ESA_LEN) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 send_set_mps_ctrl_addr(mpc->mps_ctrl_addr, mpc);
856 }
857
858 __module_get(THIS_MODULE);
859 return arg;
860}
861
David Howellscba5cbd2008-07-30 16:31:46 -0700862static void send_set_mps_ctrl_addr(const char *addr, struct mpoa_client *mpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 struct k_message mesg;
865
Joe Perches57100442010-01-26 11:40:09 +0000866 memcpy(mpc->mps_ctrl_addr, addr, ATM_ESA_LEN);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 mesg.type = SET_MPS_CTRL_ADDR;
869 memcpy(mesg.MPS_ctrl, addr, ATM_ESA_LEN);
870 msg_to_mpoad(&mesg, mpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
873static void mpoad_close(struct atm_vcc *vcc)
874{
875 struct mpoa_client *mpc;
876 struct sk_buff *skb;
877
878 mpc = find_mpc_by_vcc(vcc);
879 if (mpc == NULL) {
Joe Perches57100442010-01-26 11:40:09 +0000880 pr_info("did not find MPC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return;
882 }
883 if (!mpc->mpoad_vcc) {
Joe Perches57100442010-01-26 11:40:09 +0000884 pr_info("close for non-present mpoad\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return;
886 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 mpc->mpoad_vcc = NULL;
889 if (mpc->dev) {
Wang Chen524ad0a2008-11-12 23:39:10 -0800890 struct lec_priv *priv = netdev_priv(mpc->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 priv->lane2_ops->associate_indicator = NULL;
892 stop_mpc(mpc);
893 dev_put(mpc->dev);
894 }
895
896 mpc->in_ops->destroy_cache(mpc);
897 mpc->eg_ops->destroy_cache(mpc);
898
899 while ((skb = skb_dequeue(&sk_atm(vcc)->sk_receive_queue))) {
900 atm_return(vcc, skb->truesize);
901 kfree_skb(skb);
902 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900903
Joe Perches57100442010-01-26 11:40:09 +0000904 pr_info("(%s) going down\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 (mpc->dev) ? mpc->dev->name : "<unknown>");
906 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909/*
910 *
911 */
912static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb)
913{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct mpoa_client *mpc = find_mpc_by_vcc(vcc);
Joe Perches57100442010-01-26 11:40:09 +0000916 struct k_message *mesg = (struct k_message *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 atomic_sub(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (mpc == NULL) {
Joe Perches57100442010-01-26 11:40:09 +0000920 pr_info("no mpc found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 0;
922 }
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000923 dprintk("(%s)", mpc->dev ? mpc->dev->name : "<unknown>");
Joe Perches57100442010-01-26 11:40:09 +0000924 switch (mesg->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 case MPOA_RES_REPLY_RCVD:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000926 dprintk_cont("mpoa_res_reply_rcvd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 MPOA_res_reply_rcvd(mesg, mpc);
928 break;
929 case MPOA_TRIGGER_RCVD:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000930 dprintk_cont("mpoa_trigger_rcvd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 MPOA_trigger_rcvd(mesg, mpc);
932 break;
933 case INGRESS_PURGE_RCVD:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000934 dprintk_cont("nhrp_purge_rcvd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 ingress_purge_rcvd(mesg, mpc);
936 break;
937 case EGRESS_PURGE_RCVD:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000938 dprintk_cont("egress_purge_reply_rcvd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 egress_purge_rcvd(mesg, mpc);
940 break;
941 case MPS_DEATH:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000942 dprintk_cont("mps_death\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 mps_death(mesg, mpc);
944 break;
945 case CACHE_IMPOS_RCVD:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000946 dprintk_cont("cache_impos_rcvd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 MPOA_cache_impos_rcvd(mesg, mpc);
948 break;
949 case SET_MPC_CTRL_ADDR:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000950 dprintk_cont("set_mpc_ctrl_addr\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 set_mpc_ctrl_addr_rcvd(mesg, mpc);
952 break;
953 case SET_MPS_MAC_ADDR:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000954 dprintk_cont("set_mps_mac_addr\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 set_mps_mac_addr_rcvd(mesg, mpc);
956 break;
957 case CLEAN_UP_AND_EXIT:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000958 dprintk_cont("clean_up_and_exit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 clean_up(mesg, mpc, DIE);
960 break;
961 case RELOAD:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000962 dprintk_cont("reload\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 clean_up(mesg, mpc, RELOAD);
964 break;
965 case SET_MPC_PARAMS:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000966 dprintk_cont("set_mpc_params\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 mpc->parameters = mesg->content.params;
968 break;
969 default:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000970 dprintk_cont("unknown message %d\n", mesg->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 break;
972 }
973 kfree_skb(skb);
974
975 return 0;
976}
977
978/* Remember that this function may not do things that sleep */
979int msg_to_mpoad(struct k_message *mesg, struct mpoa_client *mpc)
980{
981 struct sk_buff *skb;
982 struct sock *sk;
983
984 if (mpc == NULL || !mpc->mpoad_vcc) {
Joe Perches57100442010-01-26 11:40:09 +0000985 pr_info("mesg %d to a non-existent mpoad\n", mesg->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return -ENXIO;
987 }
988
989 skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
990 if (skb == NULL)
991 return -ENOMEM;
992 skb_put(skb, sizeof(struct k_message));
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300993 skb_copy_to_linear_data(skb, mesg, sizeof(*mesg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 atm_force_charge(mpc->mpoad_vcc, skb->truesize);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 sk = sk_atm(mpc->mpoad_vcc);
997 skb_queue_tail(&sk->sk_receive_queue, skb);
998 sk->sk_data_ready(sk, skb->len);
999
1000 return 0;
1001}
1002
Joe Perches57100442010-01-26 11:40:09 +00001003static int mpoa_event_listener(struct notifier_block *mpoa_notifier,
1004 unsigned long event, void *dev_ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 struct net_device *dev;
1007 struct mpoa_client *mpc;
1008 struct lec_priv *priv;
1009
1010 dev = (struct net_device *)dev_ptr;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001011
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001012 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001013 return NOTIFY_DONE;
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (dev->name == NULL || strncmp(dev->name, "lec", 3))
1016 return NOTIFY_DONE; /* we are only interested in lec:s */
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 switch (event) {
1019 case NETDEV_REGISTER: /* a new lec device was allocated */
Wang Chen524ad0a2008-11-12 23:39:10 -08001020 priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (priv->lane_version < 2)
1022 break;
1023 priv->lane2_ops->associate_indicator = lane2_assoc_ind;
1024 mpc = find_mpc_by_itfnum(priv->itfnum);
1025 if (mpc == NULL) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001026 dprintk("allocating new mpc for %s\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 mpc = alloc_mpc();
1028 if (mpc == NULL) {
Joe Perches57100442010-01-26 11:40:09 +00001029 pr_info("no new mpc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 break;
1031 }
1032 }
1033 mpc->dev_num = priv->itfnum;
1034 mpc->dev = dev;
1035 dev_hold(dev);
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001036 dprintk("(%s) was initialized\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 break;
1038 case NETDEV_UNREGISTER:
1039 /* the lec device was deallocated */
1040 mpc = find_mpc_by_lec(dev);
1041 if (mpc == NULL)
1042 break;
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001043 dprintk("device (%s) was deallocated\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 stop_mpc(mpc);
1045 dev_put(mpc->dev);
1046 mpc->dev = NULL;
1047 break;
1048 case NETDEV_UP:
1049 /* the dev was ifconfig'ed up */
1050 mpc = find_mpc_by_lec(dev);
1051 if (mpc == NULL)
1052 break;
Joe Perches57100442010-01-26 11:40:09 +00001053 if (mpc->mpoad_vcc != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 start_mpc(mpc, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 break;
1056 case NETDEV_DOWN:
1057 /* the dev was ifconfig'ed down */
1058 /* this means that the flow of packets from the
1059 * upper layer stops
1060 */
1061 mpc = find_mpc_by_lec(dev);
1062 if (mpc == NULL)
1063 break;
Joe Perches57100442010-01-26 11:40:09 +00001064 if (mpc->mpoad_vcc != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 stop_mpc(mpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 break;
1067 case NETDEV_REBOOT:
1068 case NETDEV_CHANGE:
1069 case NETDEV_CHANGEMTU:
1070 case NETDEV_CHANGEADDR:
1071 case NETDEV_GOING_DOWN:
1072 break;
1073 default:
1074 break;
1075 }
1076
1077 return NOTIFY_DONE;
1078}
1079
1080/*
1081 * Functions which are called after a message is received from mpcd.
1082 * Msg is reused on purpose.
1083 */
1084
1085
1086static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1087{
Al Viro30d492d2006-11-14 21:11:29 -08001088 __be32 dst_ip = msg->content.in_info.in_dst_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 in_cache_entry *entry;
1090
1091 entry = mpc->in_ops->get(dst_ip, mpc);
Joe Perches57100442010-01-26 11:40:09 +00001092 if (entry == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 entry = mpc->in_ops->add_entry(dst_ip, mpc);
1094 entry->entry_state = INGRESS_RESOLVING;
1095 msg->type = SND_MPOA_RES_RQST;
1096 msg->content.in_info = entry->ctrl_info;
1097 msg_to_mpoad(msg, mpc);
1098 do_gettimeofday(&(entry->reply_wait));
1099 mpc->in_ops->put(entry);
1100 return;
1101 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001102
Joe Perches57100442010-01-26 11:40:09 +00001103 if (entry->entry_state == INGRESS_INVALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 entry->entry_state = INGRESS_RESOLVING;
1105 msg->type = SND_MPOA_RES_RQST;
1106 msg->content.in_info = entry->ctrl_info;
1107 msg_to_mpoad(msg, mpc);
1108 do_gettimeofday(&(entry->reply_wait));
1109 mpc->in_ops->put(entry);
1110 return;
1111 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001112
Joe Perches57100442010-01-26 11:40:09 +00001113 pr_info("(%s) entry already in resolving state\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 (mpc->dev) ? mpc->dev->name : "<unknown>");
1115 mpc->in_ops->put(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
1118/*
1119 * Things get complicated because we have to check if there's an egress
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001120 * shortcut with suitable traffic parameters we could use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 */
Joe Perches57100442010-01-26 11:40:09 +00001122static void check_qos_and_open_shortcut(struct k_message *msg,
1123 struct mpoa_client *client,
1124 in_cache_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Al Viro30d492d2006-11-14 21:11:29 -08001126 __be32 dst_ip = msg->content.in_info.in_dst_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 struct atm_mpoa_qos *qos = atm_mpoa_search_qos(dst_ip);
1128 eg_cache_entry *eg_entry = client->eg_ops->get_by_src_ip(dst_ip, client);
1129
Joe Perches57100442010-01-26 11:40:09 +00001130 if (eg_entry && eg_entry->shortcut) {
1131 if (eg_entry->shortcut->qos.txtp.traffic_class &
1132 msg->qos.txtp.traffic_class &
1133 (qos ? qos->qos.txtp.traffic_class : ATM_UBR | ATM_CBR)) {
1134 if (eg_entry->shortcut->qos.txtp.traffic_class == ATM_UBR)
1135 entry->shortcut = eg_entry->shortcut;
1136 else if (eg_entry->shortcut->qos.txtp.max_pcr > 0)
1137 entry->shortcut = eg_entry->shortcut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
Joe Perches57100442010-01-26 11:40:09 +00001139 if (entry->shortcut) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001140 dprintk("(%s) using egress SVC to reach %pI4\n",
Harvey Harrison21454aa2008-10-31 00:54:56 -07001141 client->dev->name, &dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 client->eg_ops->put(eg_entry);
1143 return;
1144 }
1145 }
1146 if (eg_entry != NULL)
1147 client->eg_ops->put(eg_entry);
1148
1149 /* No luck in the egress cache we must open an ingress SVC */
1150 msg->type = OPEN_INGRESS_SVC;
Joe Perches57100442010-01-26 11:40:09 +00001151 if (qos &&
1152 (qos->qos.txtp.traffic_class == msg->qos.txtp.traffic_class)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 msg->qos = qos->qos;
Joe Perches57100442010-01-26 11:40:09 +00001154 pr_info("(%s) trying to get a CBR shortcut\n",
1155 client->dev->name);
1156 } else
1157 memset(&msg->qos, 0, sizeof(struct atm_qos));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 msg_to_mpoad(msg, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
1161static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1162{
Al Viro30d492d2006-11-14 21:11:29 -08001163 __be32 dst_ip = msg->content.in_info.in_dst_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 in_cache_entry *entry = mpc->in_ops->get(dst_ip, mpc);
Andrew Morton1e4fd512006-06-26 00:01:58 -07001165
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001166 dprintk("(%s) ip %pI4\n",
Harvey Harrison21454aa2008-10-31 00:54:56 -07001167 mpc->dev->name, &dst_ip);
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001168 ddprintk("(%s) entry = %p",
Joe Perches57100442010-01-26 11:40:09 +00001169 mpc->dev->name, entry);
1170 if (entry == NULL) {
1171 pr_info("(%s) ARGH, received res. reply for an entry that doesn't exist.\n",
1172 mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return;
1174 }
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001175 ddprintk_cont(" entry_state = %d ", entry->entry_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 if (entry->entry_state == INGRESS_RESOLVED) {
Joe Perches57100442010-01-26 11:40:09 +00001178 pr_info("(%s) RESOLVED entry!\n", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 mpc->in_ops->put(entry);
1180 return;
1181 }
1182
1183 entry->ctrl_info = msg->content.in_info;
1184 do_gettimeofday(&(entry->tv));
1185 do_gettimeofday(&(entry->reply_wait)); /* Used in refreshing func from now on */
1186 entry->refresh_time = 0;
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001187 ddprintk_cont("entry->shortcut = %p\n", entry->shortcut);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Joe Perches57100442010-01-26 11:40:09 +00001189 if (entry->entry_state == INGRESS_RESOLVING &&
1190 entry->shortcut != NULL) {
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001191 entry->entry_state = INGRESS_RESOLVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 mpc->in_ops->put(entry);
1193 return; /* Shortcut already open... */
1194 }
1195
1196 if (entry->shortcut != NULL) {
Joe Perches57100442010-01-26 11:40:09 +00001197 pr_info("(%s) entry->shortcut != NULL, impossible!\n",
1198 mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 mpc->in_ops->put(entry);
1200 return;
1201 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 check_qos_and_open_shortcut(msg, mpc, entry);
1204 entry->entry_state = INGRESS_RESOLVED;
1205 mpc->in_ops->put(entry);
1206
1207 return;
1208
1209}
1210
1211static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1212{
Al Viro30d492d2006-11-14 21:11:29 -08001213 __be32 dst_ip = msg->content.in_info.in_dst_ip;
1214 __be32 mask = msg->ip_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 in_cache_entry *entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1216
Joe Perches57100442010-01-26 11:40:09 +00001217 if (entry == NULL) {
1218 pr_info("(%s) purge for a non-existing entry, ip = %pI4\n",
1219 mpc->dev->name, &dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return;
1221 }
1222
1223 do {
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001224 dprintk("(%s) removing an ingress entry, ip = %pI4\n",
Harvey Harrison21454aa2008-10-31 00:54:56 -07001225 mpc->dev->name, &dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 write_lock_bh(&mpc->ingress_lock);
1227 mpc->in_ops->remove_entry(entry, mpc);
1228 write_unlock_bh(&mpc->ingress_lock);
1229 mpc->in_ops->put(entry);
1230 entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1231 } while (entry != NULL);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001232}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1235{
Al Viro30d492d2006-11-14 21:11:29 -08001236 __be32 cache_id = msg->content.eg_info.cache_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(cache_id, mpc);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (entry == NULL) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001240 dprintk("(%s) purge for a non-existing entry\n",
Joe Perches57100442010-01-26 11:40:09 +00001241 mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return;
1243 }
1244
1245 write_lock_irq(&mpc->egress_lock);
1246 mpc->eg_ops->remove_entry(entry, mpc);
1247 write_unlock_irq(&mpc->egress_lock);
1248
1249 mpc->eg_ops->put(entry);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001250}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry)
1253{
1254 struct sock *sk;
1255 struct k_message *purge_msg;
1256 struct sk_buff *skb;
1257
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001258 dprintk("entering\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (vcc == NULL) {
Joe Perches57100442010-01-26 11:40:09 +00001260 pr_info("vcc == NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return;
1262 }
1263
1264 skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
1265 if (skb == NULL) {
Joe Perches57100442010-01-26 11:40:09 +00001266 pr_info("out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return;
1268 }
1269
1270 skb_put(skb, sizeof(struct k_message));
1271 memset(skb->data, 0, sizeof(struct k_message));
1272 purge_msg = (struct k_message *)skb->data;
1273 purge_msg->type = DATA_PLANE_PURGE;
1274 if (entry != NULL)
1275 purge_msg->content.eg_info = entry->ctrl_info;
1276
1277 atm_force_charge(vcc, skb->truesize);
1278
1279 sk = sk_atm(vcc);
1280 skb_queue_tail(&sk->sk_receive_queue, skb);
1281 sk->sk_data_ready(sk, skb->len);
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001282 dprintk("exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
1285/*
1286 * Our MPS died. Tell our daemon to send NHRP data plane purge to each
1287 * of the egress shortcuts we have.
1288 */
Joe Perches57100442010-01-26 11:40:09 +00001289static void mps_death(struct k_message *msg, struct mpoa_client *mpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 eg_cache_entry *entry;
1292
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001293 dprintk("(%s)\n", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Joe Perches57100442010-01-26 11:40:09 +00001295 if (memcmp(msg->MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN)) {
1296 pr_info("(%s) wrong MPS\n", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return;
1298 }
1299
1300 /* FIXME: This knows too much of the cache structure */
1301 read_lock_irq(&mpc->egress_lock);
1302 entry = mpc->eg_cache;
1303 while (entry != NULL) {
1304 purge_egress_shortcut(entry->shortcut, entry);
1305 entry = entry->next;
1306 }
1307 read_unlock_irq(&mpc->egress_lock);
1308
1309 mpc->in_ops->destroy_cache(mpc);
1310 mpc->eg_ops->destroy_cache(mpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
Joe Perches57100442010-01-26 11:40:09 +00001313static void MPOA_cache_impos_rcvd(struct k_message *msg,
1314 struct mpoa_client *mpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
1316 uint16_t holding_time;
1317 eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(msg->content.eg_info.cache_id, mpc);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 holding_time = msg->content.eg_info.holding_time;
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001320 dprintk("(%s) entry = %p, holding_time = %u\n",
Joe Perches57100442010-01-26 11:40:09 +00001321 mpc->dev->name, entry, holding_time);
1322 if (entry == NULL && holding_time) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 entry = mpc->eg_ops->add_entry(msg, mpc);
1324 mpc->eg_ops->put(entry);
1325 return;
1326 }
Joe Perches57100442010-01-26 11:40:09 +00001327 if (holding_time) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 mpc->eg_ops->update(entry, holding_time);
1329 return;
1330 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 write_lock_irq(&mpc->egress_lock);
1333 mpc->eg_ops->remove_entry(entry, mpc);
1334 write_unlock_irq(&mpc->egress_lock);
1335
1336 mpc->eg_ops->put(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
Joe Perches57100442010-01-26 11:40:09 +00001339static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg,
1340 struct mpoa_client *mpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 struct lec_priv *priv;
1343 int i, retval ;
1344
1345 uint8_t tlv[4 + 1 + 1 + 1 + ATM_ESA_LEN];
1346
1347 tlv[0] = 00; tlv[1] = 0xa0; tlv[2] = 0x3e; tlv[3] = 0x2a; /* type */
1348 tlv[4] = 1 + 1 + ATM_ESA_LEN; /* length */
1349 tlv[5] = 0x02; /* MPOA client */
1350 tlv[6] = 0x00; /* number of MPS MAC addresses */
1351
1352 memcpy(&tlv[7], mesg->MPS_ctrl, ATM_ESA_LEN); /* MPC ctrl ATM addr */
1353 memcpy(mpc->our_ctrl_addr, mesg->MPS_ctrl, ATM_ESA_LEN);
1354
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001355 dprintk("(%s) setting MPC ctrl ATM address to",
1356 mpc->dev ? mpc->dev->name : "<unknown>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 for (i = 7; i < sizeof(tlv); i++)
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001358 dprintk_cont(" %02x", tlv[i]);
1359 dprintk_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 if (mpc->dev) {
Wang Chen524ad0a2008-11-12 23:39:10 -08001362 priv = netdev_priv(mpc->dev);
Joe Perches57100442010-01-26 11:40:09 +00001363 retval = priv->lane2_ops->associate_req(mpc->dev,
1364 mpc->dev->dev_addr,
1365 tlv, sizeof(tlv));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (retval == 0)
Joe Perches57100442010-01-26 11:40:09 +00001367 pr_info("(%s) MPOA device type TLV association failed\n",
1368 mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 retval = priv->lane2_ops->resolve(mpc->dev, NULL, 1, NULL, NULL);
1370 if (retval < 0)
Joe Perches57100442010-01-26 11:40:09 +00001371 pr_info("(%s) targetless LE_ARP request failed\n",
1372 mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
Joe Perches57100442010-01-26 11:40:09 +00001376static void set_mps_mac_addr_rcvd(struct k_message *msg,
1377 struct mpoa_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379
Joe Perches57100442010-01-26 11:40:09 +00001380 if (client->number_of_mps_macs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 kfree(client->mps_macs);
1382 client->number_of_mps_macs = 0;
Arnaldo Carvalho de Melo2afe37c2006-11-21 01:14:33 -02001383 client->mps_macs = kmemdup(msg->MPS_ctrl, ETH_ALEN, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (client->mps_macs == NULL) {
Joe Perches57100442010-01-26 11:40:09 +00001385 pr_info("out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 return;
1387 }
1388 client->number_of_mps_macs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
1391/*
1392 * purge egress cache and tell daemon to 'action' (DIE, RELOAD)
1393 */
1394static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action)
1395{
1396
1397 eg_cache_entry *entry;
1398 msg->type = SND_EGRESS_PURGE;
1399
1400
1401 /* FIXME: This knows too much of the cache structure */
1402 read_lock_irq(&mpc->egress_lock);
1403 entry = mpc->eg_cache;
Joe Perches57100442010-01-26 11:40:09 +00001404 while (entry != NULL) {
1405 msg->content.eg_info = entry->ctrl_info;
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001406 dprintk("cache_id %u\n", entry->ctrl_info.cache_id);
Joe Perches57100442010-01-26 11:40:09 +00001407 msg_to_mpoad(msg, mpc);
1408 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410 read_unlock_irq(&mpc->egress_lock);
1411
1412 msg->type = action;
1413 msg_to_mpoad(msg, mpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
1416static void mpc_timer_refresh(void)
1417{
1418 mpc_timer.expires = jiffies + (MPC_P2 * HZ);
1419 mpc_timer.data = mpc_timer.expires;
1420 mpc_timer.function = mpc_cache_check;
1421 add_timer(&mpc_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
Joe Perches57100442010-01-26 11:40:09 +00001424static void mpc_cache_check(unsigned long checking_time)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
1426 struct mpoa_client *mpc = mpcs;
1427 static unsigned long previous_resolving_check_time;
1428 static unsigned long previous_refresh_time;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001429
Joe Perches57100442010-01-26 11:40:09 +00001430 while (mpc != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 mpc->in_ops->clear_count(mpc);
1432 mpc->eg_ops->clear_expired(mpc);
Joe Perches57100442010-01-26 11:40:09 +00001433 if (checking_time - previous_resolving_check_time >
1434 mpc->parameters.mpc_p4 * HZ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 mpc->in_ops->check_resolving(mpc);
1436 previous_resolving_check_time = checking_time;
1437 }
Joe Perches57100442010-01-26 11:40:09 +00001438 if (checking_time - previous_refresh_time >
1439 mpc->parameters.mpc_p5 * HZ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 mpc->in_ops->refresh(mpc);
1441 previous_refresh_time = checking_time;
1442 }
1443 mpc = mpc->next;
1444 }
1445 mpc_timer_refresh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
Joe Perches57100442010-01-26 11:40:09 +00001448static int atm_mpoa_ioctl(struct socket *sock, unsigned int cmd,
1449 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 int err = 0;
1452 struct atm_vcc *vcc = ATM_SD(sock);
1453
1454 if (cmd != ATMMPC_CTRL && cmd != ATMMPC_DATA)
1455 return -ENOIOCTLCMD;
1456
1457 if (!capable(CAP_NET_ADMIN))
1458 return -EPERM;
1459
1460 switch (cmd) {
Joe Perches57100442010-01-26 11:40:09 +00001461 case ATMMPC_CTRL:
1462 err = atm_mpoa_mpoad_attach(vcc, (int)arg);
1463 if (err >= 0)
1464 sock->state = SS_CONNECTED;
1465 break;
1466 case ATMMPC_DATA:
1467 err = atm_mpoa_vcc_attach(vcc, (void __user *)arg);
1468 break;
1469 default:
1470 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472 return err;
1473}
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475static struct atm_ioctl atm_ioctl_ops = {
1476 .owner = THIS_MODULE,
1477 .ioctl = atm_mpoa_ioctl,
1478};
1479
1480static __init int atm_mpoa_init(void)
1481{
1482 register_atm_ioctl(&atm_ioctl_ops);
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (mpc_proc_init() != 0)
Joe Perches99824462010-01-26 11:40:00 +00001485 pr_info("failed to initialize /proc/mpoa\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Joe Perches57100442010-01-26 11:40:09 +00001487 pr_info("mpc.c: " __DATE__ " " __TIME__ " initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 return 0;
1490}
1491
1492static void __exit atm_mpoa_cleanup(void)
1493{
1494 struct mpoa_client *mpc, *tmp;
1495 struct atm_mpoa_qos *qos, *nextqos;
1496 struct lec_priv *priv;
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 mpc_proc_clean();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 del_timer(&mpc_timer);
1501 unregister_netdevice_notifier(&mpoa_notifier);
1502 deregister_atm_ioctl(&atm_ioctl_ops);
1503
1504 mpc = mpcs;
1505 mpcs = NULL;
1506 while (mpc != NULL) {
1507 tmp = mpc->next;
1508 if (mpc->dev != NULL) {
1509 stop_mpc(mpc);
Wang Chen524ad0a2008-11-12 23:39:10 -08001510 priv = netdev_priv(mpc->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (priv->lane2_ops != NULL)
1512 priv->lane2_ops->associate_indicator = NULL;
1513 }
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001514 ddprintk("about to clear caches\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 mpc->in_ops->destroy_cache(mpc);
1516 mpc->eg_ops->destroy_cache(mpc);
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001517 ddprintk("caches cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 kfree(mpc->mps_macs);
1519 memset(mpc, 0, sizeof(struct mpoa_client));
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001520 ddprintk("about to kfree %p\n", mpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 kfree(mpc);
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001522 ddprintk("next mpc is at %p\n", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 mpc = tmp;
1524 }
1525
1526 qos = qos_head;
1527 qos_head = NULL;
1528 while (qos != NULL) {
1529 nextqos = qos->next;
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001530 dprintk("freeing qos entry %p\n", qos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 kfree(qos);
1532 qos = nextqos;
1533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
1536module_init(atm_mpoa_init);
1537module_exit(atm_mpoa_cleanup);
1538
1539MODULE_LICENSE("GPL");