1/*
2 *	Ioctl handler
3 *	Linux ethernet bridge
4 *
5 *	Authors:
6 *	Lennert Buytenhek		<buytenh@gnu.org>
7 *
8 *	This program is free software; you can redistribute it and/or
9 *	modify it under the terms of the GNU General Public License
10 *	as published by the Free Software Foundation; either version
11 *	2 of the License, or (at your option) any later version.
12 */
13
14#include <linux/capability.h>
15#include <linux/kernel.h>
16#include <linux/if_bridge.h>
17#include <linux/netdevice.h>
18#include <linux/slab.h>
19#include <linux/times.h>
20#include <net/net_namespace.h>
21#include <asm/uaccess.h>
22#include "br_private.h"
23#ifdef HNDCTF
24#include <ctf/hndctf.h>
25#endif /* HNDCTF */
26
27/* Foxconn add start, Zz Shan@MutiSsidControl 03/13/2009*/
28#ifdef MULTIPLE_SSID
29
30/*foxconn modified start, water, 01/07/10*/
31//#include "../../../../router/multissidcontrol/MultiSsidControl.h"
32#include "../../../../../../ap/acos/multissidcontrol/MultiSsidControl.h"
33/*foxconn modified end, water, 01/07/10*/
34
35T_MSsidCtlProfile *gProfile = NULL;
36int gProfilenum = 0;
37/* Foxconn added start pling 10/06/2010 */
38T_MSsidCtlProfile *gProfile_5g = NULL;
39int gProfilenum_5g = 0;
40/* Foxconn added end pling 10/06/2010 */
41
42EXPORT_SYMBOL(gProfilenum);
43EXPORT_SYMBOL(gProfilenum_5g);
44EXPORT_SYMBOL(gProfile);
45EXPORT_SYMBOL(gProfile_5g);
46
47/*Fxcn added start by dennis,02/16/2012,for access control*/
48#ifdef INCLUDE_ACCESSCONTROL
49#include "../../../../../../ap/acos/access_control/AccessControl.h"
50/*Fxcn added end by dennis,02/16/2012,for access control*/
51/*Fxcn added start by dennis,02/16/2012,for access control*/
52T_AccessControlTable *gAccessTable = NULL;
53int gAccessControlMode = 0; /*0:access control disabled,1:access all new devices,2:block all new devices*/
54int gAccessTablenum = 0;
55/*Fxcn added end by dennis,02/16/2012,for access control*/
56/*Foxconn add start by Hank 08/29/2012*/
57EXPORT_SYMBOL(gAccessTable);
58EXPORT_SYMBOL(gAccessControlMode);
59EXPORT_SYMBOL(gAccessTablenum);
60/*Foxconn add end by Hank 08/29/2012*/
61#endif
62void profileprint(void)
63{
64    int i = 0;
65    for (i = 0;i < gProfilenum;i++)
66    {
67        printk("<0>Profile[%d]:name:%s,enable:%d\n",i,gProfile[i].IfName,gProfile[i].enable);
68    }
69    for (i = 0;i < gProfilenum_5g;i++)
70    {
71        printk("<0>Profile_5g[%d]:name:%s,enable:%d\n",i,gProfile_5g[i].IfName,gProfile_5g[i].enable);
72    }
73}
74#endif
75/* Foxconn add end, Zz Shan 03/13/2009*/
76
77/* called with RTNL */
78static int get_bridge_ifindices(struct net *net, int *indices, int num)
79{
80	struct net_device *dev;
81	int i = 0;
82
83	for_each_netdev(net, dev) {
84		if (i >= num)
85			break;
86		if (dev->priv_flags & IFF_EBRIDGE)
87			indices[i++] = dev->ifindex;
88	}
89
90	return i;
91}
92
93/* called with RTNL */
94static void get_port_ifindices(struct net_bridge *br, int *ifindices, int num)
95{
96	struct net_bridge_port *p;
97
98	list_for_each_entry(p, &br->port_list, list) {
99		if (p->port_no < num)
100			ifindices[p->port_no] = p->dev->ifindex;
101	}
102}
103
104/*
105 * Format up to a page worth of forwarding table entries
106 * userbuf -- where to copy result
107 * maxnum  -- maximum number of entries desired
108 *            (limited to a page for sanity)
109 * offset  -- number of records to skip
110 */
111static int get_fdb_entries(struct net_bridge *br, void __user *userbuf,
112			   unsigned long maxnum, unsigned long offset)
113{
114	int num;
115	void *buf;
116	size_t size;
117
118	/* Clamp size to PAGE_SIZE, test maxnum to avoid overflow */
119	if (maxnum > PAGE_SIZE/sizeof(struct __fdb_entry))
120		maxnum = PAGE_SIZE/sizeof(struct __fdb_entry);
121
122	size = maxnum * sizeof(struct __fdb_entry);
123
124	buf = kmalloc(size, GFP_USER);
125	if (!buf)
126		return -ENOMEM;
127
128	num = br_fdb_fillbuf(br, buf, maxnum, offset);
129	if (num > 0) {
130		if (copy_to_user(userbuf, buf, num*sizeof(struct __fdb_entry)))
131			num = -EFAULT;
132	}
133	kfree(buf);
134
135	return num;
136}
137
138/* called with RTNL */
139static int add_del_if(struct net_bridge *br, int ifindex, int isadd)
140{
141	struct net_device *dev;
142	int ret;
143
144	if (!capable(CAP_NET_ADMIN))
145		return -EPERM;
146
147	dev = __dev_get_by_index(dev_net(br->dev), ifindex);
148	if (dev == NULL)
149		return -EINVAL;
150
151	if (isadd)
152		ret = br_add_if(br, dev);
153	else
154		ret = br_del_if(br, dev);
155
156	return ret;
157}
158
159/*
160 * Legacy ioctl's through SIOCDEVPRIVATE
161 * This interface is deprecated because it was too difficult to
162 * to do the translation for 32/64bit ioctl compatability.
163 */
164static int old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
165{
166	struct net_bridge *br = netdev_priv(dev);
167	unsigned long args[4];
168
169	if (copy_from_user(args, rq->ifr_data, sizeof(args)))
170		return -EFAULT;
171
172	switch (args[0]) {
173	case BRCTL_ADD_IF:
174	case BRCTL_DEL_IF:
175		return add_del_if(br, args[1], args[0] == BRCTL_ADD_IF);
176
177	case BRCTL_GET_BRIDGE_INFO:
178	{
179		struct __bridge_info b;
180
181		memset(&b, 0, sizeof(struct __bridge_info));
182		rcu_read_lock();
183		memcpy(&b.designated_root, &br->designated_root, 8);
184		memcpy(&b.bridge_id, &br->bridge_id, 8);
185		b.root_path_cost = br->root_path_cost;
186		b.max_age = jiffies_to_clock_t(br->max_age);
187		b.hello_time = jiffies_to_clock_t(br->hello_time);
188		b.forward_delay = br->forward_delay;
189		b.bridge_max_age = br->bridge_max_age;
190		b.bridge_hello_time = br->bridge_hello_time;
191		b.bridge_forward_delay = jiffies_to_clock_t(br->bridge_forward_delay);
192		b.topology_change = br->topology_change;
193		b.topology_change_detected = br->topology_change_detected;
194		b.root_port = br->root_port;
195
196		b.stp_enabled = (br->stp_enabled != BR_NO_STP);
197		b.ageing_time = jiffies_to_clock_t(br->ageing_time);
198		b.hello_timer_value = br_timer_value(&br->hello_timer);
199		b.tcn_timer_value = br_timer_value(&br->tcn_timer);
200		b.topology_change_timer_value = br_timer_value(&br->topology_change_timer);
201		b.gc_timer_value = br_timer_value(&br->gc_timer);
202		rcu_read_unlock();
203
204		if (copy_to_user((void __user *)args[1], &b, sizeof(b)))
205			return -EFAULT;
206
207		return 0;
208	}
209
210	case BRCTL_GET_PORT_LIST:
211	{
212		int num, *indices;
213
214		num = args[2];
215		if (num < 0)
216			return -EINVAL;
217		if (num == 0)
218			num = 256;
219		if (num > BR_MAX_PORTS)
220			num = BR_MAX_PORTS;
221
222		indices = kcalloc(num, sizeof(int), GFP_KERNEL);
223		if (indices == NULL)
224			return -ENOMEM;
225
226		get_port_ifindices(br, indices, num);
227		if (copy_to_user((void __user *)args[1], indices, num*sizeof(int)))
228			num =  -EFAULT;
229		kfree(indices);
230		return num;
231	}
232
233	case BRCTL_SET_BRIDGE_FORWARD_DELAY:
234		if (!capable(CAP_NET_ADMIN))
235			return -EPERM;
236
237		spin_lock_bh(&br->lock);
238		br->bridge_forward_delay = clock_t_to_jiffies(args[1]);
239		if (br_is_root_bridge(br))
240			br->forward_delay = br->bridge_forward_delay;
241		spin_unlock_bh(&br->lock);
242		return 0;
243
244	case BRCTL_SET_BRIDGE_HELLO_TIME:
245	{
246		unsigned long t = clock_t_to_jiffies(args[1]);
247		if (!capable(CAP_NET_ADMIN))
248			return -EPERM;
249
250		if (t < HZ)
251			return -EINVAL;
252
253		spin_lock_bh(&br->lock);
254		br->bridge_hello_time = t;
255		if (br_is_root_bridge(br))
256			br->hello_time = br->bridge_hello_time;
257		spin_unlock_bh(&br->lock);
258		return 0;
259	}
260
261	case BRCTL_SET_BRIDGE_MAX_AGE:
262		if (!capable(CAP_NET_ADMIN))
263			return -EPERM;
264
265		spin_lock_bh(&br->lock);
266		br->bridge_max_age = clock_t_to_jiffies(args[1]);
267		if (br_is_root_bridge(br))
268			br->max_age = br->bridge_max_age;
269		spin_unlock_bh(&br->lock);
270		return 0;
271
272	case BRCTL_SET_AGEING_TIME:
273		if (!capable(CAP_NET_ADMIN))
274			return -EPERM;
275
276		br->ageing_time = clock_t_to_jiffies(args[1]);
277		return 0;
278
279	case BRCTL_GET_PORT_INFO:
280	{
281		struct __port_info p;
282		struct net_bridge_port *pt;
283
284		rcu_read_lock();
285		if ((pt = br_get_port(br, args[2])) == NULL) {
286			rcu_read_unlock();
287			return -EINVAL;
288		}
289
290		memset(&p, 0, sizeof(struct __port_info));
291		memcpy(&p.designated_root, &pt->designated_root, 8);
292		memcpy(&p.designated_bridge, &pt->designated_bridge, 8);
293		p.port_id = pt->port_id;
294		p.designated_port = pt->designated_port;
295		p.path_cost = pt->path_cost;
296		p.designated_cost = pt->designated_cost;
297		p.state = pt->state;
298		p.top_change_ack = pt->topology_change_ack;
299		p.config_pending = pt->config_pending;
300		p.message_age_timer_value = br_timer_value(&pt->message_age_timer);
301		p.forward_delay_timer_value = br_timer_value(&pt->forward_delay_timer);
302		p.hold_timer_value = br_timer_value(&pt->hold_timer);
303
304		rcu_read_unlock();
305
306		if (copy_to_user((void __user *)args[1], &p, sizeof(p)))
307			return -EFAULT;
308
309		return 0;
310	}
311
312	case BRCTL_SET_BRIDGE_STP_STATE:
313		if (!capable(CAP_NET_ADMIN))
314			return -EPERM;
315
316		br_stp_set_enabled(br, args[1]);
317		return 0;
318
319	case BRCTL_SET_BRIDGE_PRIORITY:
320		if (!capable(CAP_NET_ADMIN))
321			return -EPERM;
322
323		spin_lock_bh(&br->lock);
324		br_stp_set_bridge_priority(br, args[1]);
325		spin_unlock_bh(&br->lock);
326		return 0;
327
328	case BRCTL_SET_PORT_PRIORITY:
329	{
330		struct net_bridge_port *p;
331		int ret = 0;
332
333		if (!capable(CAP_NET_ADMIN))
334			return -EPERM;
335
336		if (args[2] >= (1<<(16-BR_PORT_BITS)))
337			return -ERANGE;
338
339		spin_lock_bh(&br->lock);
340		if ((p = br_get_port(br, args[1])) == NULL)
341			ret = -EINVAL;
342		else
343			br_stp_set_port_priority(p, args[2]);
344		spin_unlock_bh(&br->lock);
345		return ret;
346	}
347
348	case BRCTL_SET_PATH_COST:
349	{
350		struct net_bridge_port *p;
351		int ret = 0;
352
353		if (!capable(CAP_NET_ADMIN))
354			return -EPERM;
355
356		if ((p = br_get_port(br, args[1])) == NULL)
357			ret = -EINVAL;
358		else
359			br_stp_set_path_cost(p, args[2]);
360
361		return ret;
362	}
363
364	case BRCTL_GET_FDB_ENTRIES:
365		return get_fdb_entries(br, (void __user *)args[1],
366				       args[2], args[3]);
367	}
368
369	return -EOPNOTSUPP;
370}
371
372static int old_deviceless(struct net *net, void __user *uarg)
373{
374	unsigned long args[3];
375
376	if (copy_from_user(args, uarg, sizeof(args)))
377		return -EFAULT;
378
379	switch (args[0]) {
380	case BRCTL_GET_VERSION:
381		return BRCTL_VERSION;
382
383	case BRCTL_GET_BRIDGES:
384	{
385		int *indices;
386		int ret = 0;
387
388		if (args[2] >= 2048)
389			return -ENOMEM;
390		indices = kcalloc(args[2], sizeof(int), GFP_KERNEL);
391		if (indices == NULL)
392			return -ENOMEM;
393
394		args[2] = get_bridge_ifindices(net, indices, args[2]);
395
396		ret = copy_to_user((void __user *)args[1], indices, args[2]*sizeof(int))
397			? -EFAULT : args[2];
398
399		kfree(indices);
400		return ret;
401	}
402
403	case BRCTL_ADD_BRIDGE:
404	case BRCTL_DEL_BRIDGE:
405	{
406		char buf[IFNAMSIZ];
407
408		if (!capable(CAP_NET_ADMIN))
409			return -EPERM;
410
411		if (copy_from_user(buf, (void __user *)args[1], IFNAMSIZ))
412			return -EFAULT;
413
414		buf[IFNAMSIZ-1] = 0;
415
416		if (args[0] == BRCTL_ADD_BRIDGE)
417			return br_add_bridge(net, buf);
418
419		return br_del_bridge(net, buf);
420	}
421	/* Foxconn add start, Zz Shan@MutiSsidControl 03/13/2009*/
422#ifdef MULTIPLE_SSID
423	case BRCTL_SET_MSSIDPROFILE:
424	{
425		unsigned long num = 0;
426
427		gProfilenum = 0;
428		if (gProfile)
429		{
430			kfree(gProfile);
431			gProfile = NULL;
432		}
433
434		if (copy_from_user(&num, (void __user *)args[1], sizeof(int)))
435			return -EFAULT;
436
437		if (num == 0)
438			return 0;
439
440		gProfilenum = num;
441		gProfile = (T_MSsidCtlProfile *)kmalloc(num*sizeof(T_MSsidCtlProfile),GFP_ATOMIC);
442		if (!gProfile)
443		{
444			gProfilenum = 0;
445			return -EFAULT;
446		}
447		if (copy_from_user(gProfile, (void __user *)args[2], num*sizeof(T_MSsidCtlProfile)))
448		{
449			gProfilenum = 0;
450			kfree(gProfile);
451			gProfile = NULL;
452			return -EFAULT;
453		}
454
455		//profileprint();
456
457		return 0;
458	}
459    /* Foxconn added start pling 10/06/2010 */
460    /* For 5G guest network control */
461	case BRCTL_SET_5G_MSSIDPROFILE:
462	{
463		unsigned long num = 0;
464
465		gProfilenum_5g = 0;
466		if (gProfile_5g)
467		{
468			kfree(gProfile_5g);
469			gProfile_5g = NULL;
470		}
471
472		if (copy_from_user(&num, (void __user *)args[1], sizeof(int)))
473			return -EFAULT;
474
475		if (num == 0)
476			return 0;
477
478		gProfilenum_5g = num;
479		gProfile_5g = (T_MSsidCtlProfile *)kmalloc(num*sizeof(T_MSsidCtlProfile),GFP_ATOMIC);
480		if (!gProfile_5g)
481		{
482			gProfilenum_5g = 0;
483			return -EFAULT;
484		}
485		if (copy_from_user(gProfile_5g, (void __user *)args[2], num*sizeof(T_MSsidCtlProfile)))
486		{
487			gProfilenum_5g = 0;
488			kfree(gProfile_5g);
489			gProfile_5g = NULL;
490			return -EFAULT;
491		}
492
493		//profileprint();
494
495		return 0;
496	}
497    /* Foxconn added end pling 10/06/2010 */
498#endif
499	/* Foxconn add end, Zz Shan 03/13/2009*/
500	/* foxconn added start, zacker, 03/24/2011 */
501	case BRCTL_SET_BCMCTF_ENABLE:
502	{
503#ifdef HNDCTF
504		struct net_device *dev;
505		int ret = 0;
506		int ctf_is_enabled = 0;
507
508		/*Foxconn modify start by Hank 08/10/2012 */
509		/*kernel function be modified*/
510		dev = dev_get_by_index(net,args[1]);
511		/*Foxconn modify end by Hank 08/10/2012 */
512		if (dev == NULL)
513			return -EINVAL;
514
515		ctf_is_enabled = ctf_isenabled(kcih, dev);
516		if ((args[2] && !ctf_is_enabled)
517			|| (!args[2] && ctf_is_enabled))
518		{
519			printk("<0>SET_BCMCTF:ifname:%s,enable:%lu\n",
520					dev->name, args[2]);
521			ret = ctf_enable(kcih, dev, args[2],NULL);
522		}
523
524		dev_put(dev);
525		return ret;
526#else
527		break;
528#endif /* HNDCTF */
529	}
530	/* foxconn added end, zacker, 03/24/2011 */
531	 /*Fxcn added start dennis,02/18/2012,for access control*/
532#ifdef INCLUDE_ACCESSCONTROL
533    case BRCTL_SET_ACCESS_CONTROL:
534    {
535
536       unsigned long num = 0;
537       gAccessTablenum = 0;
538
539       if (gAccessTable)
540		{
541			kfree(gAccessTable);
542			gAccessTable = NULL;
543		}
544
545		if (copy_from_user(&num, (void __user *)args[1], sizeof(int)))
546			return -EFAULT;
547
548		if (num == 0)
549			return 0;
550
551		gAccessTablenum = num;
552		gAccessTable = (T_AccessControlTable *)kmalloc(num*sizeof(T_AccessControlTable),GFP_ATOMIC);
553		if (!gAccessTable)
554		{
555			gAccessTablenum = 0;
556			return -EFAULT;
557		}
558		if (copy_from_user(gAccessTable, (void __user *)args[2], num*sizeof(T_AccessControlTable)))
559		{
560			gAccessTablenum = 0;
561			kfree(gAccessTable);
562			gAccessTable = NULL;
563			return -EFAULT;
564		}
565
566       return 0;
567
568    }
569
570   case BRCTL_SET_ACCESS_CONTROL_MODE:
571   {
572        unsigned long mode = 0;
573        gAccessControlMode = 0;
574
575
576        if (copy_from_user(&mode, (void __user *)args[1], sizeof(int)))
577			return -EFAULT;
578	printk("BRCTL_SET_ACCESS_CONTROL_MODE>> mode %d\n",mode);
579        gAccessControlMode = mode;
580        printk("BRCTL_SET_ACCESS_CONTROL_MODE>> gAccessControlMode  %d\n",gAccessControlMode);
581
582        return 0;
583
584   }
585#endif
586
587    /*Fxcn added end dennis,02/18/2012,for access control*/
588	}
589
590	return -EOPNOTSUPP;
591}
592
593int br_ioctl_deviceless_stub(struct net *net, unsigned int cmd, void __user *uarg)
594{
595	switch (cmd) {
596	case SIOCGIFBR:
597	case SIOCSIFBR:
598		return old_deviceless(net, uarg);
599
600	case SIOCBRADDBR:
601	case SIOCBRDELBR:
602	{
603		char buf[IFNAMSIZ];
604
605		if (!capable(CAP_NET_ADMIN))
606			return -EPERM;
607
608		if (copy_from_user(buf, uarg, IFNAMSIZ))
609			return -EFAULT;
610
611		buf[IFNAMSIZ-1] = 0;
612		if (cmd == SIOCBRADDBR)
613			return br_add_bridge(net, buf);
614
615		return br_del_bridge(net, buf);
616	}
617	}
618	return -EOPNOTSUPP;
619}
620
621int br_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
622{
623	struct net_bridge *br = netdev_priv(dev);
624
625	switch(cmd) {
626	case SIOCDEVPRIVATE:
627		return old_dev_ioctl(dev, rq, cmd);
628
629	case SIOCBRADDIF:
630	case SIOCBRDELIF:
631		return add_del_if(br, rq->ifr_ifindex, cmd == SIOCBRADDIF);
632
633	}
634
635	br_debug(br, "Bridge does not support ioctl 0x%x\n", cmd);
636	return -EOPNOTSUPP;
637}
638