blob: 75620c2f261723a915b74df013ac214479ba70c4 [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/* mpc_daemon -> kernel */
Joe Perches57100442010-01-26 11:40:09 +000068static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static 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);
Joe Perches57100442010-01-26 11:40:09 +000073static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
David Howellscba5cbd2008-07-30 16:31:46 -070082static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry);
87
David Howellscba5cbd2008-07-30 16:31:46 -070088static void send_set_mps_ctrl_addr(const char *addr, struct mpoa_client *mpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static 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);
Stephen Hemminger3c805a22009-08-31 19:50:42 +000093static netdev_tx_t mpc_send_packet(struct sk_buff *skb,
Joe Perches57100442010-01-26 11:40:09 +000094 struct net_device *dev);
95static int mpoa_event_listener(struct notifier_block *mpoa_notifier,
96 unsigned long event, void *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static void mpc_timer_refresh(void);
Kees Cook5c658e12017-10-24 01:46:45 -070098static void mpc_cache_check(struct timer_list *unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100static struct llc_snap_hdr llc_snap_mpoa_ctrl = {
101 0xaa, 0xaa, 0x03,
102 {0x00, 0x00, 0x5e},
103 {0x00, 0x03} /* For MPOA control PDUs */
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900104};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static 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 */
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900109};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static 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 */
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900114};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116static struct notifier_block mpoa_notifier = {
117 mpoa_event_listener,
118 NULL,
119 0
120};
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122struct mpoa_client *mpcs = NULL; /* FIXME */
123static struct atm_mpoa_qos *qos_head = NULL;
Kees Cook1e9aa742017-08-22 12:41:43 -0700124static DEFINE_TIMER(mpc_timer, mpc_cache_check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126
127static struct mpoa_client *find_mpc_by_itfnum(int itf)
128{
129 struct mpoa_client *mpc;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 mpc = mpcs; /* our global linked list */
132 while (mpc != NULL) {
133 if (mpc->dev_num == itf)
134 return mpc;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900135 mpc = mpc->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
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;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 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;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 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 */
Al Viro30d492d2006-11-14 21:11:29 -0800176struct atm_mpoa_qos *atm_mpoa_add_qos(__be32 dst_ip, struct atm_qos *qos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
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) {
Joe Perches57100442010-01-26 11:40:09 +0000188 pr_info("mpoa: out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 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
Al Viro30d492d2006-11-14 21:11:29 -0800201struct atm_mpoa_qos *atm_mpoa_search_qos(__be32 dst_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 struct atm_mpoa_qos *qos;
204
205 qos = qos_head;
Joe Perches57100442010-01-26 11:40:09 +0000206 while (qos) {
207 if (qos->ipaddr == dst_ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 qos = qos->next;
210 }
211
212 return qos;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900213}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215/*
216 * Returns 0 for failure
217 */
218int atm_mpoa_delete_qos(struct atm_mpoa_qos *entry)
219{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 struct atm_mpoa_qos *curr;
221
Joe Perches57100442010-01-26 11:40:09 +0000222 if (entry == NULL)
223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 while (qos != NULL) {
Harvey Harrison21454aa2008-10-31 00:54:56 -0700253 seq_printf(m, "%pI4\n %-7d %-7d %-7d %-7d %-7d\n %-7d %-7d %-7d %-7d %-7d\n",
Joe Perches57100442010-01-26 11:40:09 +0000254 &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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 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);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700275 dev = dev_get_by_name(&init_net, name);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return dev;
278}
279
280static struct mpoa_client *alloc_mpc(void)
281{
282 struct mpoa_client *mpc;
283
Joe Perches57100442010-01-26 11:40:09 +0000284 mpc = kzalloc(sizeof(struct mpoa_client), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (mpc == NULL)
286 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 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;
Joe Perches57100442010-01-26 11:40:09 +0000294 memset(mpc->parameters.mpc_p3, 0, sizeof(mpc->parameters.mpc_p3));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 mpc->parameters.mpc_p4 = MPC_P4;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900296 mpc->parameters.mpc_p5 = MPC_P5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 mpc->parameters.mpc_p6 = MPC_P6;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 mpcs = mpc;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return mpc;
302}
303
304/*
305 *
306 * start_mpc() puts the MPC on line. All the packets destined
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900307 * to the lec underneath us are now being monitored and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * shortcuts will be established.
309 *
310 */
311static void start_mpc(struct mpoa_client *mpc, struct net_device *dev)
312{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900313
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000314 dprintk("(%s)\n", mpc->dev->name);
Stephen Hemminger788dee02009-03-20 19:35:28 +0000315 if (!dev->netdev_ops)
Joe Perches57100442010-01-26 11:40:09 +0000316 pr_info("(%s) not starting\n", dev->name);
Stephen Hemminger788dee02009-03-20 19:35:28 +0000317 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325static void stop_mpc(struct mpoa_client *mpc)
326{
Stephen Hemminger788dee02009-03-20 19:35:28 +0000327 struct net_device *dev = mpc->dev;
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000328 dprintk("(%s)", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 /* Lets not nullify lec device's dev->hard_start_xmit */
Stephen Hemminger788dee02009-03-20 19:35:28 +0000331 if (dev->netdev_ops != &mpc->new_ops) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000332 dprintk_cont(" mpc already stopped, not fatal\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return;
334 }
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000335 dprintk_cont("\n");
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900336
Stephen Hemminger788dee02009-03-20 19:35:28 +0000337 dev->netdev_ops = mpc->old_ops;
338 mpc->old_ops = NULL;
339
340 /* close_shortcuts(mpc); ??? FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343static const char *mpoa_device_type_string(char type) __attribute__ ((unused));
344
345static const char *mpoa_device_type_string(char type)
346{
Joe Perches57100442010-01-26 11:40:09 +0000347 switch (type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 case NON_MPOA:
349 return "non-MPOA device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 case MPS:
351 return "MPS";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 case MPC:
353 return "MPC";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 case MPS_AND_MPC:
355 return "both MPS and MPC";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357
Joe Perches57100442010-01-26 11:40:09 +0000358 return "unspecified (non-MPOA) device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361/*
Wang Chenb74ca3a2008-12-08 01:14:16 -0800362 * lec device calls this via its netdev_priv(dev)->lane2_ops
363 * ->associate_indicator() when it sees a TLV in LE_ARP packet.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 * 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 */
David Howellscba5cbd2008-07-30 16:31:46 -0700374static void lane2_assoc_ind(struct net_device *dev, const u8 *mac_addr,
375 const u8 *tlvs, u32 sizeoftlvs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 uint32_t type;
378 uint8_t length, mpoa_device_type, number_of_mps_macs;
David Howellscba5cbd2008-07-30 16:31:46 -0700379 const uint8_t *end_of_tlvs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 struct mpoa_client *mpc;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 mpoa_device_type = number_of_mps_macs = 0; /* silence gcc */
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000383 dprintk("(%s) received TLV(s), ", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 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) {
Joe Perches57100442010-01-26 11:40:09 +0000387 pr_info("(%s) no mpc\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return;
389 }
390 end_of_tlvs = tlvs + sizeoftlvs;
391 while (end_of_tlvs - tlvs >= 5) {
Joe Perches57100442010-01-26 11:40:09 +0000392 type = ((tlvs[0] << 24) | (tlvs[1] << 16) |
393 (tlvs[2] << 8) | tlvs[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 length = tlvs[4];
395 tlvs += 5;
396 dprintk(" type 0x%x length %02x\n", type, length);
397 if (tlvs + length > end_of_tlvs) {
Joe Perches57100442010-01-26 11:40:09 +0000398 pr_info("TLV value extends past its buffer, aborting parse\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return;
400 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (type == 0) {
Joe Perches57100442010-01-26 11:40:09 +0000403 pr_info("mpoa: (%s) TLV type was 0, returning\n",
404 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 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++;
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000414 dprintk("(%s) MPOA device type '%s', ",
Joe Perches57100442010-01-26 11:40:09 +0000415 dev->name, mpoa_device_type_string(mpoa_device_type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (mpoa_device_type == MPS_AND_MPC &&
417 length < (42 + number_of_mps_macs*ETH_ALEN)) { /* :) */
Joe Perches57100442010-01-26 11:40:09 +0000418 pr_info("(%s) short MPOA Device Type TLV\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 dev->name);
420 continue;
421 }
Joe Perches57100442010-01-26 11:40:09 +0000422 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);
425 continue;
426 }
427 if (mpoa_device_type != MPS &&
428 mpoa_device_type != MPS_AND_MPC) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000429 dprintk("ignoring non-MPS device ");
Joe Perches57100442010-01-26 11:40:09 +0000430 if (mpoa_device_type == MPC)
431 tlvs += 20;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 continue; /* we are only interested in MPSs */
433 }
Joe Perches57100442010-01-26 11:40:09 +0000434 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 continue; /* someone should read the spec */
438 }
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000439 dprintk_cont("this MPS has %d MAC addresses\n",
440 number_of_mps_macs);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900441
Joe Perches57100442010-01-26 11:40:09 +0000442 /*
443 * ok, now we can go and tell our daemon
444 * the control address of MPS
445 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 send_set_mps_ctrl_addr(tlvs, mpc);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900447
Joe Perches57100442010-01-26 11:40:09 +0000448 tlvs = copy_macs(mpc, mac_addr, tlvs,
449 number_of_mps_macs, mpoa_device_type);
450 if (tlvs == NULL)
451 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 if (end_of_tlvs - tlvs != 0)
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -0800454 pr_info("(%s) ignoring %zd bytes of trailing TLV garbage\n",
Joe Perches57100442010-01-26 11:40:09 +0000455 dev->name, end_of_tlvs - tlvs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
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 */
David Howellscba5cbd2008-07-30 16:31:46 -0700463static 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
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? */
Joe Perches57100442010-01-26 11:40:09 +0000472 if (mpc->number_of_mps_macs != 0)
473 kfree(mpc->mps_macs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 mpc->number_of_mps_macs = 0;
Kees Cook6da2ec52018-06-12 13:55:00 -0700475 mpc->mps_macs = kmalloc_array(ETH_ALEN, num_macs, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (mpc->mps_macs == NULL) {
Joe Perches57100442010-01-26 11:40:09 +0000477 pr_info("(%s) out of mem\n", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return NULL;
479 }
480 }
Joe Perches116e8532014-01-20 09:52:16 -0800481 ether_addr_copy(mpc->mps_macs, router_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 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;
Al Viro30d492d2006-11-14 21:11:29 -0800496 __be32 ipaddr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 static struct {
499 struct llc_snap_hdr hdr;
Al Viro30d492d2006-11-14 21:11:29 -0800500 __be32 tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 } 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
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000510 ddprintk("(%s) ipaddr 0x%x\n",
Joe Perches57100442010-01-26 11:40:09 +0000511 mpc->dev->name, ipaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 entry = mpc->in_ops->get(ipaddr, mpc);
514 if (entry == NULL) {
515 entry = mpc->in_ops->add_entry(ipaddr, mpc);
Joe Perches57100442010-01-26 11:40:09 +0000516 if (entry != NULL)
517 mpc->in_ops->put(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return 1;
519 }
Joe Perches57100442010-01-26 11:40:09 +0000520 /* threshold not exceeded or VCC not ready */
521 if (mpc->in_ops->cache_hit(entry, mpc) != OPEN) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000522 ddprintk("(%s) cache_hit: returns != OPEN\n",
Joe Perches57100442010-01-26 11:40:09 +0000523 mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 mpc->in_ops->put(entry);
525 return 1;
526 }
527
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000528 ddprintk("(%s) using shortcut\n",
Joe Perches57100442010-01-26 11:40:09 +0000529 mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /* MPOA spec A.1.4, MPOA client must decrement IP ttl at least by one */
531 if (iph->ttl <= 1) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000532 ddprintk("(%s) IP ttl = %u, using LANE\n",
Joe Perches57100442010-01-26 11:40:09 +0000533 mpc->dev->name, iph->ttl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 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) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000542 ddprintk("(%s) adding tag 0x%x\n",
Joe Perches57100442010-01-26 11:40:09 +0000543 mpc->dev->name, entry->ctrl_info.tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 tagged_llc_snap_hdr.tag = entry->ctrl_info.tag;
Joe Perches57100442010-01-26 11:40:09 +0000545 skb_pull(skb, ETH_HLEN); /* get rid of Eth header */
546 skb_push(skb, sizeof(tagged_llc_snap_hdr));
547 /* add LLC/SNAP header */
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300548 skb_copy_to_linear_data(skb, &tagged_llc_snap_hdr,
549 sizeof(tagged_llc_snap_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 } else {
Joe Perches57100442010-01-26 11:40:09 +0000551 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 */
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300554 skb_copy_to_linear_data(skb, &llc_snap_mpoa_data,
555 sizeof(struct llc_snap_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557
Reshetova, Elena14afee42017-06-30 13:08:00 +0300558 refcount_add(skb->truesize, &sk_atm(entry->shortcut)->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 ATM_SKB(skb)->atm_options = entry->shortcut->atm_options;
560 entry->shortcut->send(entry->shortcut, skb);
561 entry->packets_fwded++;
562 mpc->in_ops->put(entry);
563
564 return 0;
565}
566
567/*
568 * Probably needs some error checks and locking, not sure...
569 */
Stephen Hemminger3c805a22009-08-31 19:50:42 +0000570static netdev_tx_t mpc_send_packet(struct sk_buff *skb,
571 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 struct mpoa_client *mpc;
574 struct ethhdr *eth;
575 int i = 0;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 mpc = find_mpc_by_lec(dev); /* this should NEVER fail */
Joe Perches57100442010-01-26 11:40:09 +0000578 if (mpc == NULL) {
579 pr_info("(%s) no MPC found\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 goto non_ip;
581 }
582
583 eth = (struct ethhdr *)skb->data;
584 if (eth->h_proto != htons(ETH_P_IP))
585 goto non_ip; /* Multi-Protocol Over ATM :-) */
586
Herbert Xu1c9b7aa2008-01-09 03:51:59 -0800587 /* Weed out funny packets (e.g., AF_PACKET or raw). */
588 if (skb->len < ETH_HLEN + sizeof(struct iphdr))
589 goto non_ip;
590 skb_set_network_header(skb, ETH_HLEN);
591 if (skb->len < ETH_HLEN + ip_hdr(skb)->ihl * 4 || ip_hdr(skb)->ihl < 5)
592 goto non_ip;
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 while (i < mpc->number_of_mps_macs) {
Joe Perches150238e2012-05-08 18:56:50 +0000595 if (ether_addr_equal(eth->h_dest, mpc->mps_macs + i * ETH_ALEN))
Joe Perches57100442010-01-26 11:40:09 +0000596 if (send_via_shortcut(skb, mpc) == 0) /* try shortcut */
597 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 i++;
599 }
600
Joe Perches57100442010-01-26 11:40:09 +0000601non_ip:
David S. Millerfa2dbdc2014-08-29 21:55:22 -0700602 return __netdev_start_xmit(mpc->old_ops, skb, dev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605static int atm_mpoa_vcc_attach(struct atm_vcc *vcc, void __user *arg)
606{
607 int bytes_left;
608 struct mpoa_client *mpc;
609 struct atmmpc_ioc ioc_data;
610 in_cache_entry *in_entry;
Al Viro30d492d2006-11-14 21:11:29 -0800611 __be32 ipaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 bytes_left = copy_from_user(&ioc_data, arg, sizeof(struct atmmpc_ioc));
614 if (bytes_left != 0) {
Joe Perches57100442010-01-26 11:40:09 +0000615 pr_info("mpoa:Short read (missed %d bytes) from userland\n",
616 bytes_left);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return -EFAULT;
618 }
619 ipaddr = ioc_data.ipaddr;
620 if (ioc_data.dev_num < 0 || ioc_data.dev_num >= MAX_LEC_ITF)
621 return -EINVAL;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 mpc = find_mpc_by_itfnum(ioc_data.dev_num);
624 if (mpc == NULL)
625 return -EINVAL;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (ioc_data.type == MPC_SOCKET_INGRESS) {
628 in_entry = mpc->in_ops->get(ipaddr, mpc);
Joe Perches57100442010-01-26 11:40:09 +0000629 if (in_entry == NULL ||
630 in_entry->entry_state < INGRESS_RESOLVED) {
631 pr_info("(%s) did not find RESOLVED entry from ingress cache\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 mpc->dev->name);
Joe Perches57100442010-01-26 11:40:09 +0000633 if (in_entry != NULL)
634 mpc->in_ops->put(in_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return -EINVAL;
636 }
Joe Perches57100442010-01-26 11:40:09 +0000637 pr_info("(%s) attaching ingress SVC, entry = %pI4\n",
638 mpc->dev->name, &in_entry->ctrl_info.in_dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 in_entry->shortcut = vcc;
640 mpc->in_ops->put(in_entry);
641 } else {
Joe Perches57100442010-01-26 11:40:09 +0000642 pr_info("(%s) attaching egress SVC\n", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
645 vcc->proto_data = mpc->dev;
646 vcc->push = mpc_push;
647
648 return 0;
649}
650
651/*
652 *
653 */
654static void mpc_vcc_close(struct atm_vcc *vcc, struct net_device *dev)
655{
656 struct mpoa_client *mpc;
657 in_cache_entry *in_entry;
658 eg_cache_entry *eg_entry;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 mpc = find_mpc_by_lec(dev);
661 if (mpc == NULL) {
Joe Perches57100442010-01-26 11:40:09 +0000662 pr_info("(%s) close for unknown MPC\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return;
664 }
665
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000666 dprintk("(%s)\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 in_entry = mpc->in_ops->get_by_vcc(vcc, mpc);
668 if (in_entry) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000669 dprintk("(%s) ingress SVC closed ip = %pI4\n",
Joe Perches57100442010-01-26 11:40:09 +0000670 mpc->dev->name, &in_entry->ctrl_info.in_dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 in_entry->shortcut = NULL;
672 mpc->in_ops->put(in_entry);
673 }
674 eg_entry = mpc->eg_ops->get_by_vcc(vcc, mpc);
675 if (eg_entry) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000676 dprintk("(%s) egress SVC closed\n", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 eg_entry->shortcut = NULL;
678 mpc->eg_ops->put(eg_entry);
679 }
680
681 if (in_entry == NULL && eg_entry == NULL)
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000682 dprintk("(%s) unused vcc closed\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685static void mpc_push(struct atm_vcc *vcc, struct sk_buff *skb)
686{
687 struct net_device *dev = (struct net_device *)vcc->proto_data;
688 struct sk_buff *new_skb;
689 eg_cache_entry *eg;
690 struct mpoa_client *mpc;
Al Viro30d492d2006-11-14 21:11:29 -0800691 __be32 tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 char *tmp;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900693
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000694 ddprintk("(%s)\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (skb == NULL) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000696 dprintk("(%s) null skb, closing VCC\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 mpc_vcc_close(vcc, dev);
698 return;
699 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 skb->dev = dev;
Joe Perches57100442010-01-26 11:40:09 +0000702 if (memcmp(skb->data, &llc_snap_mpoa_ctrl,
703 sizeof(struct llc_snap_hdr)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct sock *sk = sk_atm(vcc);
705
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000706 dprintk("(%s) control packet arrived\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /* Pass control packets to daemon */
708 skb_queue_tail(&sk->sk_receive_queue, skb);
David S. Miller676d2362014-04-11 16:15:36 -0400709 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return;
711 }
712
713 /* data coming over the shortcut */
714 atm_return(vcc, skb->truesize);
715
716 mpc = find_mpc_by_lec(dev);
717 if (mpc == NULL) {
Joe Perches57100442010-01-26 11:40:09 +0000718 pr_info("(%s) unknown MPC\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return;
720 }
721
Joe Perches57100442010-01-26 11:40:09 +0000722 if (memcmp(skb->data, &llc_snap_mpoa_data_tagged,
723 sizeof(struct llc_snap_hdr)) == 0) { /* MPOA tagged data */
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000724 ddprintk("(%s) tagged data packet arrived\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Joe Perches57100442010-01-26 11:40:09 +0000726 } else if (memcmp(skb->data, &llc_snap_mpoa_data,
727 sizeof(struct llc_snap_hdr)) == 0) { /* MPOA data */
728 pr_info("(%s) Unsupported non-tagged data packet arrived. Purging\n",
729 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 dev_kfree_skb_any(skb);
731 return;
732 } else {
Joe Perches57100442010-01-26 11:40:09 +0000733 pr_info("(%s) garbage arrived, purging\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 dev_kfree_skb_any(skb);
735 return;
736 }
737
738 tmp = skb->data + sizeof(struct llc_snap_hdr);
Al Viro30d492d2006-11-14 21:11:29 -0800739 tag = *(__be32 *)tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 eg = mpc->eg_ops->get_by_tag(tag, mpc);
742 if (eg == NULL) {
Joe Perches57100442010-01-26 11:40:09 +0000743 pr_info("mpoa: (%s) Didn't find egress cache entry, tag = %u\n",
744 dev->name, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 purge_egress_shortcut(vcc, NULL);
746 dev_kfree_skb_any(skb);
747 return;
748 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /*
751 * See if ingress MPC is using shortcut we opened as a return channel.
752 * This means we have a bi-directional vcc opened by us.
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900753 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (eg->shortcut == NULL) {
755 eg->shortcut = vcc;
Joe Perches57100442010-01-26 11:40:09 +0000756 pr_info("(%s) egress SVC in use\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
Joe Perches57100442010-01-26 11:40:09 +0000759 skb_pull(skb, sizeof(struct llc_snap_hdr) + sizeof(tag));
760 /* get rid of LLC/SNAP header */
761 new_skb = skb_realloc_headroom(skb, eg->ctrl_info.DH_length);
762 /* LLC/SNAP is shorter than MAC header :( */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 dev_kfree_skb_any(skb);
Joe Perches57100442010-01-26 11:40:09 +0000764 if (new_skb == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 mpc->eg_ops->put(eg);
766 return;
767 }
768 skb_push(new_skb, eg->ctrl_info.DH_length); /* add MAC header */
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300769 skb_copy_to_linear_data(new_skb, eg->ctrl_info.DLL_header,
770 eg->ctrl_info.DH_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 new_skb->protocol = eth_type_trans(new_skb, dev);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700772 skb_reset_network_header(new_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700774 eg->latest_ip_addr = ip_hdr(new_skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 eg->packets_rcvd++;
776 mpc->eg_ops->put(eg);
777
Jiri Slaby5518b292010-10-10 22:46:34 +0000778 memset(ATM_SKB(new_skb), 0, sizeof(struct atm_skb_data));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 netif_rx(new_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
Bhumika Goyal800bb472017-08-09 15:02:08 +0530782static const struct atmdev_ops mpc_ops = { /* only send is required */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 .close = mpoad_close,
784 .send = msg_from_mpoad
785};
786
787static struct atm_dev mpc_dev = {
788 .ops = &mpc_ops,
789 .type = "mpc",
790 .number = 42,
Milind Arun Choudhary4ef8d0a2007-04-26 01:37:44 -0700791 .lock = __SPIN_LOCK_UNLOCKED(mpc_dev.lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 /* members not explicitly initialised will be 0 */
793};
794
Joe Perches57100442010-01-26 11:40:09 +0000795static int atm_mpoa_mpoad_attach(struct atm_vcc *vcc, int arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 struct mpoa_client *mpc;
798 struct lec_priv *priv;
799 int err;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (mpcs == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 mpc_timer_refresh();
803
804 /* This lets us now how our LECs are doing */
805 err = register_netdevice_notifier(&mpoa_notifier);
806 if (err < 0) {
807 del_timer(&mpc_timer);
808 return err;
809 }
810 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 mpc = find_mpc_by_itfnum(arg);
813 if (mpc == NULL) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000814 dprintk("allocating new mpc for itf %d\n", arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 mpc = alloc_mpc();
816 if (mpc == NULL)
817 return -ENOMEM;
818 mpc->dev_num = arg;
Joe Perches57100442010-01-26 11:40:09 +0000819 mpc->dev = find_lec_by_itfnum(arg);
820 /* NULL if there was no lec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822 if (mpc->mpoad_vcc) {
Joe Perches57100442010-01-26 11:40:09 +0000823 pr_info("mpoad is already present for itf %d\n", arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return -EADDRINUSE;
825 }
826
827 if (mpc->dev) { /* check if the lec is LANE2 capable */
Wang Chen524ad0a2008-11-12 23:39:10 -0800828 priv = netdev_priv(mpc->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (priv->lane_version < 2) {
830 dev_put(mpc->dev);
831 mpc->dev = NULL;
832 } else
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900833 priv->lane2_ops->associate_indicator = lane2_assoc_ind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
836 mpc->mpoad_vcc = vcc;
837 vcc->dev = &mpc_dev;
838 vcc_insert_socket(sk_atm(vcc));
Joe Perches57100442010-01-26 11:40:09 +0000839 set_bit(ATM_VF_META, &vcc->flags);
840 set_bit(ATM_VF_READY, &vcc->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 if (mpc->dev) {
843 char empty[ATM_ESA_LEN];
844 memset(empty, 0, ATM_ESA_LEN);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 start_mpc(mpc, mpc->dev);
847 /* set address if mpcd e.g. gets killed and restarted.
848 * If we do not do it now we have to wait for the next LE_ARP
849 */
Joe Perches57100442010-01-26 11:40:09 +0000850 if (memcmp(mpc->mps_ctrl_addr, empty, ATM_ESA_LEN) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 send_set_mps_ctrl_addr(mpc->mps_ctrl_addr, mpc);
852 }
853
854 __module_get(THIS_MODULE);
855 return arg;
856}
857
David Howellscba5cbd2008-07-30 16:31:46 -0700858static void send_set_mps_ctrl_addr(const char *addr, struct mpoa_client *mpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 struct k_message mesg;
861
Joe Perches57100442010-01-26 11:40:09 +0000862 memcpy(mpc->mps_ctrl_addr, addr, ATM_ESA_LEN);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 mesg.type = SET_MPS_CTRL_ADDR;
865 memcpy(mesg.MPS_ctrl, addr, ATM_ESA_LEN);
866 msg_to_mpoad(&mesg, mpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
869static void mpoad_close(struct atm_vcc *vcc)
870{
871 struct mpoa_client *mpc;
872 struct sk_buff *skb;
873
874 mpc = find_mpc_by_vcc(vcc);
875 if (mpc == NULL) {
Joe Perches57100442010-01-26 11:40:09 +0000876 pr_info("did not find MPC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return;
878 }
879 if (!mpc->mpoad_vcc) {
Joe Perches57100442010-01-26 11:40:09 +0000880 pr_info("close for non-present mpoad\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return;
882 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 mpc->mpoad_vcc = NULL;
885 if (mpc->dev) {
Wang Chen524ad0a2008-11-12 23:39:10 -0800886 struct lec_priv *priv = netdev_priv(mpc->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 priv->lane2_ops->associate_indicator = NULL;
888 stop_mpc(mpc);
889 dev_put(mpc->dev);
890 }
891
892 mpc->in_ops->destroy_cache(mpc);
893 mpc->eg_ops->destroy_cache(mpc);
894
895 while ((skb = skb_dequeue(&sk_atm(vcc)->sk_receive_queue))) {
896 atm_return(vcc, skb->truesize);
897 kfree_skb(skb);
898 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900899
Joe Perches57100442010-01-26 11:40:09 +0000900 pr_info("(%s) going down\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 (mpc->dev) ? mpc->dev->name : "<unknown>");
902 module_put(THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
905/*
906 *
907 */
908static int msg_from_mpoad(struct atm_vcc *vcc, struct sk_buff *skb)
909{
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 struct mpoa_client *mpc = find_mpc_by_vcc(vcc);
Joe Perches57100442010-01-26 11:40:09 +0000912 struct k_message *mesg = (struct k_message *)skb->data;
Reshetova, Elena14afee42017-06-30 13:08:00 +0300913 WARN_ON(refcount_sub_and_test(skb->truesize, &sk_atm(vcc)->sk_wmem_alloc));
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (mpc == NULL) {
Joe Perches57100442010-01-26 11:40:09 +0000916 pr_info("no mpc found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return 0;
918 }
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000919 dprintk("(%s)", mpc->dev ? mpc->dev->name : "<unknown>");
Joe Perches57100442010-01-26 11:40:09 +0000920 switch (mesg->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 case MPOA_RES_REPLY_RCVD:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000922 dprintk_cont("mpoa_res_reply_rcvd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 MPOA_res_reply_rcvd(mesg, mpc);
924 break;
925 case MPOA_TRIGGER_RCVD:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000926 dprintk_cont("mpoa_trigger_rcvd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 MPOA_trigger_rcvd(mesg, mpc);
928 break;
929 case INGRESS_PURGE_RCVD:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000930 dprintk_cont("nhrp_purge_rcvd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 ingress_purge_rcvd(mesg, mpc);
932 break;
933 case EGRESS_PURGE_RCVD:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000934 dprintk_cont("egress_purge_reply_rcvd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 egress_purge_rcvd(mesg, mpc);
936 break;
937 case MPS_DEATH:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000938 dprintk_cont("mps_death\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 mps_death(mesg, mpc);
940 break;
941 case CACHE_IMPOS_RCVD:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000942 dprintk_cont("cache_impos_rcvd\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 MPOA_cache_impos_rcvd(mesg, mpc);
944 break;
945 case SET_MPC_CTRL_ADDR:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000946 dprintk_cont("set_mpc_ctrl_addr\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 set_mpc_ctrl_addr_rcvd(mesg, mpc);
948 break;
949 case SET_MPS_MAC_ADDR:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000950 dprintk_cont("set_mps_mac_addr\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 set_mps_mac_addr_rcvd(mesg, mpc);
952 break;
953 case CLEAN_UP_AND_EXIT:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000954 dprintk_cont("clean_up_and_exit\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 clean_up(mesg, mpc, DIE);
956 break;
957 case RELOAD:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000958 dprintk_cont("reload\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 clean_up(mesg, mpc, RELOAD);
960 break;
961 case SET_MPC_PARAMS:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000962 dprintk_cont("set_mpc_params\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 mpc->parameters = mesg->content.params;
964 break;
965 default:
Joe Perchesb50c2ea2010-01-26 11:40:20 +0000966 dprintk_cont("unknown message %d\n", mesg->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 break;
968 }
969 kfree_skb(skb);
970
971 return 0;
972}
973
974/* Remember that this function may not do things that sleep */
975int msg_to_mpoad(struct k_message *mesg, struct mpoa_client *mpc)
976{
977 struct sk_buff *skb;
978 struct sock *sk;
979
980 if (mpc == NULL || !mpc->mpoad_vcc) {
Joe Perches57100442010-01-26 11:40:09 +0000981 pr_info("mesg %d to a non-existent mpoad\n", mesg->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return -ENXIO;
983 }
984
985 skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
986 if (skb == NULL)
987 return -ENOMEM;
988 skb_put(skb, sizeof(struct k_message));
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300989 skb_copy_to_linear_data(skb, mesg, sizeof(*mesg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 atm_force_charge(mpc->mpoad_vcc, skb->truesize);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +0900991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 sk = sk_atm(mpc->mpoad_vcc);
993 skb_queue_tail(&sk->sk_receive_queue, skb);
David S. Miller676d2362014-04-11 16:15:36 -0400994 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 return 0;
997}
998
Joe Perches57100442010-01-26 11:40:09 +0000999static int mpoa_event_listener(struct notifier_block *mpoa_notifier,
Jiri Pirko351638e2013-05-28 01:30:21 +00001000 unsigned long event, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Jiri Pirko351638e2013-05-28 01:30:21 +00001002 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 struct mpoa_client *mpc;
1004 struct lec_priv *priv;
1005
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -07001006 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001007 return NOTIFY_DONE;
1008
Colin Ian King0d135e42016-08-16 12:08:42 +01001009 if (strncmp(dev->name, "lec", 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 return NOTIFY_DONE; /* we are only interested in lec:s */
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 switch (event) {
1013 case NETDEV_REGISTER: /* a new lec device was allocated */
Wang Chen524ad0a2008-11-12 23:39:10 -08001014 priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (priv->lane_version < 2)
1016 break;
1017 priv->lane2_ops->associate_indicator = lane2_assoc_ind;
1018 mpc = find_mpc_by_itfnum(priv->itfnum);
1019 if (mpc == NULL) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001020 dprintk("allocating new mpc for %s\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 mpc = alloc_mpc();
1022 if (mpc == NULL) {
Joe Perches57100442010-01-26 11:40:09 +00001023 pr_info("no new mpc");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 break;
1025 }
1026 }
1027 mpc->dev_num = priv->itfnum;
1028 mpc->dev = dev;
1029 dev_hold(dev);
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001030 dprintk("(%s) was initialized\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 break;
1032 case NETDEV_UNREGISTER:
1033 /* the lec device was deallocated */
1034 mpc = find_mpc_by_lec(dev);
1035 if (mpc == NULL)
1036 break;
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001037 dprintk("device (%s) was deallocated\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 stop_mpc(mpc);
1039 dev_put(mpc->dev);
1040 mpc->dev = NULL;
1041 break;
1042 case NETDEV_UP:
1043 /* the dev was ifconfig'ed up */
1044 mpc = find_mpc_by_lec(dev);
1045 if (mpc == NULL)
1046 break;
Joe Perches57100442010-01-26 11:40:09 +00001047 if (mpc->mpoad_vcc != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 start_mpc(mpc, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 break;
1050 case NETDEV_DOWN:
1051 /* the dev was ifconfig'ed down */
1052 /* this means that the flow of packets from the
1053 * upper layer stops
1054 */
1055 mpc = find_mpc_by_lec(dev);
1056 if (mpc == NULL)
1057 break;
Joe Perches57100442010-01-26 11:40:09 +00001058 if (mpc->mpoad_vcc != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 stop_mpc(mpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 break;
1061 case NETDEV_REBOOT:
1062 case NETDEV_CHANGE:
1063 case NETDEV_CHANGEMTU:
1064 case NETDEV_CHANGEADDR:
1065 case NETDEV_GOING_DOWN:
1066 break;
1067 default:
1068 break;
1069 }
1070
1071 return NOTIFY_DONE;
1072}
1073
1074/*
1075 * Functions which are called after a message is received from mpcd.
1076 * Msg is reused on purpose.
1077 */
1078
1079
1080static void MPOA_trigger_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1081{
Al Viro30d492d2006-11-14 21:11:29 -08001082 __be32 dst_ip = msg->content.in_info.in_dst_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 in_cache_entry *entry;
1084
1085 entry = mpc->in_ops->get(dst_ip, mpc);
Joe Perches57100442010-01-26 11:40:09 +00001086 if (entry == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 entry = mpc->in_ops->add_entry(dst_ip, mpc);
1088 entry->entry_state = INGRESS_RESOLVING;
1089 msg->type = SND_MPOA_RES_RQST;
1090 msg->content.in_info = entry->ctrl_info;
1091 msg_to_mpoad(msg, mpc);
Tina Ruchandanid750dbd2017-11-27 15:02:17 +01001092 entry->reply_wait = ktime_get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 mpc->in_ops->put(entry);
1094 return;
1095 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001096
Joe Perches57100442010-01-26 11:40:09 +00001097 if (entry->entry_state == INGRESS_INVALID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 entry->entry_state = INGRESS_RESOLVING;
1099 msg->type = SND_MPOA_RES_RQST;
1100 msg->content.in_info = entry->ctrl_info;
1101 msg_to_mpoad(msg, mpc);
Tina Ruchandanid750dbd2017-11-27 15:02:17 +01001102 entry->reply_wait = ktime_get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 mpc->in_ops->put(entry);
1104 return;
1105 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001106
Joe Perches57100442010-01-26 11:40:09 +00001107 pr_info("(%s) entry already in resolving state\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 (mpc->dev) ? mpc->dev->name : "<unknown>");
1109 mpc->in_ops->put(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
1112/*
1113 * Things get complicated because we have to check if there's an egress
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001114 * shortcut with suitable traffic parameters we could use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 */
Joe Perches57100442010-01-26 11:40:09 +00001116static void check_qos_and_open_shortcut(struct k_message *msg,
1117 struct mpoa_client *client,
1118 in_cache_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Al Viro30d492d2006-11-14 21:11:29 -08001120 __be32 dst_ip = msg->content.in_info.in_dst_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 struct atm_mpoa_qos *qos = atm_mpoa_search_qos(dst_ip);
1122 eg_cache_entry *eg_entry = client->eg_ops->get_by_src_ip(dst_ip, client);
1123
Joe Perches57100442010-01-26 11:40:09 +00001124 if (eg_entry && eg_entry->shortcut) {
1125 if (eg_entry->shortcut->qos.txtp.traffic_class &
1126 msg->qos.txtp.traffic_class &
1127 (qos ? qos->qos.txtp.traffic_class : ATM_UBR | ATM_CBR)) {
1128 if (eg_entry->shortcut->qos.txtp.traffic_class == ATM_UBR)
1129 entry->shortcut = eg_entry->shortcut;
1130 else if (eg_entry->shortcut->qos.txtp.max_pcr > 0)
1131 entry->shortcut = eg_entry->shortcut;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
Joe Perches57100442010-01-26 11:40:09 +00001133 if (entry->shortcut) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001134 dprintk("(%s) using egress SVC to reach %pI4\n",
Harvey Harrison21454aa2008-10-31 00:54:56 -07001135 client->dev->name, &dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 client->eg_ops->put(eg_entry);
1137 return;
1138 }
1139 }
1140 if (eg_entry != NULL)
1141 client->eg_ops->put(eg_entry);
1142
1143 /* No luck in the egress cache we must open an ingress SVC */
1144 msg->type = OPEN_INGRESS_SVC;
Joe Perches57100442010-01-26 11:40:09 +00001145 if (qos &&
1146 (qos->qos.txtp.traffic_class == msg->qos.txtp.traffic_class)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 msg->qos = qos->qos;
Joe Perches57100442010-01-26 11:40:09 +00001148 pr_info("(%s) trying to get a CBR shortcut\n",
1149 client->dev->name);
1150 } else
1151 memset(&msg->qos, 0, sizeof(struct atm_qos));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 msg_to_mpoad(msg, client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
1155static void MPOA_res_reply_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1156{
Al Viro30d492d2006-11-14 21:11:29 -08001157 __be32 dst_ip = msg->content.in_info.in_dst_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 in_cache_entry *entry = mpc->in_ops->get(dst_ip, mpc);
Andrew Morton1e4fd512006-06-26 00:01:58 -07001159
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001160 dprintk("(%s) ip %pI4\n",
Harvey Harrison21454aa2008-10-31 00:54:56 -07001161 mpc->dev->name, &dst_ip);
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001162 ddprintk("(%s) entry = %p",
Joe Perches57100442010-01-26 11:40:09 +00001163 mpc->dev->name, entry);
1164 if (entry == NULL) {
1165 pr_info("(%s) ARGH, received res. reply for an entry that doesn't exist.\n",
1166 mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return;
1168 }
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001169 ddprintk_cont(" entry_state = %d ", entry->entry_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 if (entry->entry_state == INGRESS_RESOLVED) {
Joe Perches57100442010-01-26 11:40:09 +00001172 pr_info("(%s) RESOLVED entry!\n", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 mpc->in_ops->put(entry);
1174 return;
1175 }
1176
1177 entry->ctrl_info = msg->content.in_info;
Tina Ruchandanid750dbd2017-11-27 15:02:17 +01001178 entry->time = ktime_get_seconds();
1179 /* Used in refreshing func from now on */
1180 entry->reply_wait = ktime_get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 entry->refresh_time = 0;
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001182 ddprintk_cont("entry->shortcut = %p\n", entry->shortcut);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Joe Perches57100442010-01-26 11:40:09 +00001184 if (entry->entry_state == INGRESS_RESOLVING &&
1185 entry->shortcut != NULL) {
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001186 entry->entry_state = INGRESS_RESOLVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 mpc->in_ops->put(entry);
1188 return; /* Shortcut already open... */
1189 }
1190
1191 if (entry->shortcut != NULL) {
Joe Perches57100442010-01-26 11:40:09 +00001192 pr_info("(%s) entry->shortcut != NULL, impossible!\n",
1193 mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 mpc->in_ops->put(entry);
1195 return;
1196 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 check_qos_and_open_shortcut(msg, mpc, entry);
1199 entry->entry_state = INGRESS_RESOLVED;
1200 mpc->in_ops->put(entry);
1201
1202 return;
1203
1204}
1205
1206static void ingress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1207{
Al Viro30d492d2006-11-14 21:11:29 -08001208 __be32 dst_ip = msg->content.in_info.in_dst_ip;
1209 __be32 mask = msg->ip_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 in_cache_entry *entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1211
Joe Perches57100442010-01-26 11:40:09 +00001212 if (entry == NULL) {
1213 pr_info("(%s) purge for a non-existing entry, ip = %pI4\n",
1214 mpc->dev->name, &dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return;
1216 }
1217
1218 do {
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001219 dprintk("(%s) removing an ingress entry, ip = %pI4\n",
Harvey Harrison21454aa2008-10-31 00:54:56 -07001220 mpc->dev->name, &dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 write_lock_bh(&mpc->ingress_lock);
1222 mpc->in_ops->remove_entry(entry, mpc);
1223 write_unlock_bh(&mpc->ingress_lock);
1224 mpc->in_ops->put(entry);
1225 entry = mpc->in_ops->get_with_mask(dst_ip, mpc, mask);
1226 } while (entry != NULL);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001227}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229static void egress_purge_rcvd(struct k_message *msg, struct mpoa_client *mpc)
1230{
Al Viro30d492d2006-11-14 21:11:29 -08001231 __be32 cache_id = msg->content.eg_info.cache_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 eg_cache_entry *entry = mpc->eg_ops->get_by_cache_id(cache_id, mpc);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (entry == NULL) {
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001235 dprintk("(%s) purge for a non-existing entry\n",
Joe Perches57100442010-01-26 11:40:09 +00001236 mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return;
1238 }
1239
1240 write_lock_irq(&mpc->egress_lock);
1241 mpc->eg_ops->remove_entry(entry, mpc);
1242 write_unlock_irq(&mpc->egress_lock);
1243
1244 mpc->eg_ops->put(entry);
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001245}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247static void purge_egress_shortcut(struct atm_vcc *vcc, eg_cache_entry *entry)
1248{
1249 struct sock *sk;
1250 struct k_message *purge_msg;
1251 struct sk_buff *skb;
1252
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001253 dprintk("entering\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (vcc == NULL) {
Joe Perches57100442010-01-26 11:40:09 +00001255 pr_info("vcc == NULL\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return;
1257 }
1258
1259 skb = alloc_skb(sizeof(struct k_message), GFP_ATOMIC);
1260 if (skb == NULL) {
Joe Perches57100442010-01-26 11:40:09 +00001261 pr_info("out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return;
1263 }
1264
1265 skb_put(skb, sizeof(struct k_message));
1266 memset(skb->data, 0, sizeof(struct k_message));
1267 purge_msg = (struct k_message *)skb->data;
1268 purge_msg->type = DATA_PLANE_PURGE;
1269 if (entry != NULL)
1270 purge_msg->content.eg_info = entry->ctrl_info;
1271
1272 atm_force_charge(vcc, skb->truesize);
1273
1274 sk = sk_atm(vcc);
1275 skb_queue_tail(&sk->sk_receive_queue, skb);
David S. Miller676d2362014-04-11 16:15:36 -04001276 sk->sk_data_ready(sk);
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001277 dprintk("exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
1280/*
1281 * Our MPS died. Tell our daemon to send NHRP data plane purge to each
1282 * of the egress shortcuts we have.
1283 */
Joe Perches57100442010-01-26 11:40:09 +00001284static void mps_death(struct k_message *msg, struct mpoa_client *mpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
1286 eg_cache_entry *entry;
1287
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001288 dprintk("(%s)\n", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Joe Perches57100442010-01-26 11:40:09 +00001290 if (memcmp(msg->MPS_ctrl, mpc->mps_ctrl_addr, ATM_ESA_LEN)) {
1291 pr_info("(%s) wrong MPS\n", mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return;
1293 }
1294
1295 /* FIXME: This knows too much of the cache structure */
1296 read_lock_irq(&mpc->egress_lock);
1297 entry = mpc->eg_cache;
1298 while (entry != NULL) {
1299 purge_egress_shortcut(entry->shortcut, entry);
1300 entry = entry->next;
1301 }
1302 read_unlock_irq(&mpc->egress_lock);
1303
1304 mpc->in_ops->destroy_cache(mpc);
1305 mpc->eg_ops->destroy_cache(mpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
Joe Perches57100442010-01-26 11:40:09 +00001308static void MPOA_cache_impos_rcvd(struct k_message *msg,
1309 struct mpoa_client *mpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 uint16_t holding_time;
1312 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 +09001313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 holding_time = msg->content.eg_info.holding_time;
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001315 dprintk("(%s) entry = %p, holding_time = %u\n",
Joe Perches57100442010-01-26 11:40:09 +00001316 mpc->dev->name, entry, holding_time);
1317 if (entry == NULL && holding_time) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 entry = mpc->eg_ops->add_entry(msg, mpc);
1319 mpc->eg_ops->put(entry);
1320 return;
1321 }
Joe Perches57100442010-01-26 11:40:09 +00001322 if (holding_time) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 mpc->eg_ops->update(entry, holding_time);
1324 return;
1325 }
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 write_lock_irq(&mpc->egress_lock);
1328 mpc->eg_ops->remove_entry(entry, mpc);
1329 write_unlock_irq(&mpc->egress_lock);
1330
1331 mpc->eg_ops->put(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Joe Perches57100442010-01-26 11:40:09 +00001334static void set_mpc_ctrl_addr_rcvd(struct k_message *mesg,
1335 struct mpoa_client *mpc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 struct lec_priv *priv;
1338 int i, retval ;
1339
1340 uint8_t tlv[4 + 1 + 1 + 1 + ATM_ESA_LEN];
1341
1342 tlv[0] = 00; tlv[1] = 0xa0; tlv[2] = 0x3e; tlv[3] = 0x2a; /* type */
1343 tlv[4] = 1 + 1 + ATM_ESA_LEN; /* length */
1344 tlv[5] = 0x02; /* MPOA client */
1345 tlv[6] = 0x00; /* number of MPS MAC addresses */
1346
1347 memcpy(&tlv[7], mesg->MPS_ctrl, ATM_ESA_LEN); /* MPC ctrl ATM addr */
1348 memcpy(mpc->our_ctrl_addr, mesg->MPS_ctrl, ATM_ESA_LEN);
1349
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001350 dprintk("(%s) setting MPC ctrl ATM address to",
1351 mpc->dev ? mpc->dev->name : "<unknown>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 for (i = 7; i < sizeof(tlv); i++)
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001353 dprintk_cont(" %02x", tlv[i]);
1354 dprintk_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 if (mpc->dev) {
Wang Chen524ad0a2008-11-12 23:39:10 -08001357 priv = netdev_priv(mpc->dev);
Joe Perches57100442010-01-26 11:40:09 +00001358 retval = priv->lane2_ops->associate_req(mpc->dev,
1359 mpc->dev->dev_addr,
1360 tlv, sizeof(tlv));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if (retval == 0)
Joe Perches57100442010-01-26 11:40:09 +00001362 pr_info("(%s) MPOA device type TLV association failed\n",
1363 mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 retval = priv->lane2_ops->resolve(mpc->dev, NULL, 1, NULL, NULL);
1365 if (retval < 0)
Joe Perches57100442010-01-26 11:40:09 +00001366 pr_info("(%s) targetless LE_ARP request failed\n",
1367 mpc->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
1370
Joe Perches57100442010-01-26 11:40:09 +00001371static void set_mps_mac_addr_rcvd(struct k_message *msg,
1372 struct mpoa_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374
Joe Perches57100442010-01-26 11:40:09 +00001375 if (client->number_of_mps_macs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 kfree(client->mps_macs);
1377 client->number_of_mps_macs = 0;
Arnaldo Carvalho de Melo2afe37c2006-11-21 01:14:33 -02001378 client->mps_macs = kmemdup(msg->MPS_ctrl, ETH_ALEN, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (client->mps_macs == NULL) {
Joe Perches57100442010-01-26 11:40:09 +00001380 pr_info("out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 return;
1382 }
1383 client->number_of_mps_macs = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
1386/*
1387 * purge egress cache and tell daemon to 'action' (DIE, RELOAD)
1388 */
1389static void clean_up(struct k_message *msg, struct mpoa_client *mpc, int action)
1390{
1391
1392 eg_cache_entry *entry;
1393 msg->type = SND_EGRESS_PURGE;
1394
1395
1396 /* FIXME: This knows too much of the cache structure */
1397 read_lock_irq(&mpc->egress_lock);
1398 entry = mpc->eg_cache;
Joe Perches57100442010-01-26 11:40:09 +00001399 while (entry != NULL) {
1400 msg->content.eg_info = entry->ctrl_info;
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001401 dprintk("cache_id %u\n", entry->ctrl_info.cache_id);
Joe Perches57100442010-01-26 11:40:09 +00001402 msg_to_mpoad(msg, mpc);
1403 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405 read_unlock_irq(&mpc->egress_lock);
1406
1407 msg->type = action;
1408 msg_to_mpoad(msg, mpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
1410
Kees Cook5c658e12017-10-24 01:46:45 -07001411static unsigned long checking_time;
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413static void mpc_timer_refresh(void)
1414{
1415 mpc_timer.expires = jiffies + (MPC_P2 * HZ);
Kees Cook5c658e12017-10-24 01:46:45 -07001416 checking_time = mpc_timer.expires;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 add_timer(&mpc_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
Kees Cook5c658e12017-10-24 01:46:45 -07001420static void mpc_cache_check(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
1422 struct mpoa_client *mpc = mpcs;
1423 static unsigned long previous_resolving_check_time;
1424 static unsigned long previous_refresh_time;
YOSHIFUJI Hideakif7d57452007-02-09 23:24:29 +09001425
Joe Perches57100442010-01-26 11:40:09 +00001426 while (mpc != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 mpc->in_ops->clear_count(mpc);
1428 mpc->eg_ops->clear_expired(mpc);
Joe Perches57100442010-01-26 11:40:09 +00001429 if (checking_time - previous_resolving_check_time >
1430 mpc->parameters.mpc_p4 * HZ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 mpc->in_ops->check_resolving(mpc);
1432 previous_resolving_check_time = checking_time;
1433 }
Joe Perches57100442010-01-26 11:40:09 +00001434 if (checking_time - previous_refresh_time >
1435 mpc->parameters.mpc_p5 * HZ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 mpc->in_ops->refresh(mpc);
1437 previous_refresh_time = checking_time;
1438 }
1439 mpc = mpc->next;
1440 }
1441 mpc_timer_refresh();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
Joe Perches57100442010-01-26 11:40:09 +00001444static int atm_mpoa_ioctl(struct socket *sock, unsigned int cmd,
1445 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 int err = 0;
1448 struct atm_vcc *vcc = ATM_SD(sock);
1449
1450 if (cmd != ATMMPC_CTRL && cmd != ATMMPC_DATA)
1451 return -ENOIOCTLCMD;
1452
1453 if (!capable(CAP_NET_ADMIN))
1454 return -EPERM;
1455
1456 switch (cmd) {
Joe Perches57100442010-01-26 11:40:09 +00001457 case ATMMPC_CTRL:
1458 err = atm_mpoa_mpoad_attach(vcc, (int)arg);
1459 if (err >= 0)
1460 sock->state = SS_CONNECTED;
1461 break;
1462 case ATMMPC_DATA:
1463 err = atm_mpoa_vcc_attach(vcc, (void __user *)arg);
1464 break;
1465 default:
1466 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
1468 return err;
1469}
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471static struct atm_ioctl atm_ioctl_ops = {
1472 .owner = THIS_MODULE,
1473 .ioctl = atm_mpoa_ioctl,
1474};
1475
1476static __init int atm_mpoa_init(void)
1477{
1478 register_atm_ioctl(&atm_ioctl_ops);
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (mpc_proc_init() != 0)
Joe Perches99824462010-01-26 11:40:00 +00001481 pr_info("failed to initialize /proc/mpoa\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Michal Marek36a9f772011-04-01 12:41:20 +02001483 pr_info("mpc.c: initialized\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 return 0;
1486}
1487
1488static void __exit atm_mpoa_cleanup(void)
1489{
1490 struct mpoa_client *mpc, *tmp;
1491 struct atm_mpoa_qos *qos, *nextqos;
1492 struct lec_priv *priv;
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 mpc_proc_clean();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Julia Lawall84275592014-03-26 22:33:40 +01001496 del_timer_sync(&mpc_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 unregister_netdevice_notifier(&mpoa_notifier);
1498 deregister_atm_ioctl(&atm_ioctl_ops);
1499
1500 mpc = mpcs;
1501 mpcs = NULL;
1502 while (mpc != NULL) {
1503 tmp = mpc->next;
1504 if (mpc->dev != NULL) {
1505 stop_mpc(mpc);
Wang Chen524ad0a2008-11-12 23:39:10 -08001506 priv = netdev_priv(mpc->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (priv->lane2_ops != NULL)
1508 priv->lane2_ops->associate_indicator = NULL;
1509 }
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001510 ddprintk("about to clear caches\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 mpc->in_ops->destroy_cache(mpc);
1512 mpc->eg_ops->destroy_cache(mpc);
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001513 ddprintk("caches cleared\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 kfree(mpc->mps_macs);
1515 memset(mpc, 0, sizeof(struct mpoa_client));
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001516 ddprintk("about to kfree %p\n", mpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 kfree(mpc);
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001518 ddprintk("next mpc is at %p\n", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 mpc = tmp;
1520 }
1521
1522 qos = qos_head;
1523 qos_head = NULL;
1524 while (qos != NULL) {
1525 nextqos = qos->next;
Joe Perchesb50c2ea2010-01-26 11:40:20 +00001526 dprintk("freeing qos entry %p\n", qos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 kfree(qos);
1528 qos = nextqos;
1529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
1531
1532module_init(atm_mpoa_init);
1533module_exit(atm_mpoa_cleanup);
1534
1535MODULE_LICENSE("GPL");