| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 | Experimental ethernet netdevice using ATM AAL5 as underlying carrier | 
 | 3 | (RFC1483 obsoleted by RFC2684) for Linux 2.4 | 
 | 4 | Author: Marcell GAL, 2000, XDSL Ltd, Hungary | 
 | 5 | */ | 
 | 6 |  | 
 | 7 | #include <linux/module.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/init.h> | 
 | 9 | #include <linux/kernel.h> | 
 | 10 | #include <linux/list.h> | 
 | 11 | #include <linux/netdevice.h> | 
 | 12 | #include <linux/skbuff.h> | 
 | 13 | #include <linux/etherdevice.h> | 
 | 14 | #include <linux/rtnetlink.h> | 
 | 15 | #include <linux/ip.h> | 
 | 16 | #include <asm/uaccess.h> | 
 | 17 | #include <net/arp.h> | 
 | 18 | #include <linux/atm.h> | 
 | 19 | #include <linux/atmdev.h> | 
| Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 20 | #include <linux/capability.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/seq_file.h> | 
 | 22 |  | 
 | 23 | #include <linux/atmbr2684.h> | 
 | 24 |  | 
 | 25 | #include "common.h" | 
 | 26 | #include "ipcommon.h" | 
 | 27 |  | 
 | 28 | /* | 
 | 29 |  * Define this to use a version of the code which interacts with the higher | 
 | 30 |  * layers in a more intellegent way, by always reserving enough space for | 
 | 31 |  * our header at the begining of the packet.  However, there may still be | 
 | 32 |  * some problems with programs like tcpdump.  In 2.5 we'll sort out what | 
 | 33 |  * we need to do to get this perfect.  For now we just will copy the packet | 
 | 34 |  * if we need space for the header | 
 | 35 |  */ | 
 | 36 | /* #define FASTER_VERSION */ | 
 | 37 |  | 
 | 38 | #ifdef DEBUG | 
 | 39 | #define DPRINTK(format, args...) printk(KERN_DEBUG "br2684: " format, ##args) | 
 | 40 | #else | 
 | 41 | #define DPRINTK(format, args...) | 
 | 42 | #endif | 
 | 43 |  | 
 | 44 | #ifdef SKB_DEBUG | 
 | 45 | static void skb_debug(const struct sk_buff *skb) | 
 | 46 | { | 
 | 47 | #define NUM2PRINT 50 | 
 | 48 | 	char buf[NUM2PRINT * 3 + 1];	/* 3 chars per byte */ | 
 | 49 | 	int i = 0; | 
 | 50 | 	for (i = 0; i < skb->len && i < NUM2PRINT; i++) { | 
 | 51 | 		sprintf(buf + i * 3, "%2.2x ", 0xff & skb->data[i]); | 
 | 52 | 	} | 
 | 53 | 	printk(KERN_DEBUG "br2684: skb: %s\n", buf); | 
 | 54 | } | 
 | 55 | #else | 
 | 56 | #define skb_debug(skb)	do {} while (0) | 
 | 57 | #endif | 
 | 58 |  | 
 | 59 | static unsigned char llc_oui_pid_pad[] = | 
 | 60 |     { 0xAA, 0xAA, 0x03, 0x00, 0x80, 0xC2, 0x00, 0x07, 0x00, 0x00 }; | 
 | 61 | #define PADLEN	(2) | 
 | 62 |  | 
 | 63 | enum br2684_encaps { | 
 | 64 | 	e_vc  = BR2684_ENCAPS_VC, | 
 | 65 | 	e_llc = BR2684_ENCAPS_LLC, | 
 | 66 | }; | 
 | 67 |  | 
 | 68 | struct br2684_vcc { | 
 | 69 | 	struct atm_vcc  *atmvcc; | 
 | 70 | 	struct net_device *device; | 
 | 71 | 	/* keep old push,pop functions for chaining */ | 
 | 72 | 	void (*old_push)(struct atm_vcc *vcc,struct sk_buff *skb); | 
 | 73 | 	/* void (*old_pop)(struct atm_vcc *vcc,struct sk_buff *skb); */ | 
 | 74 | 	enum br2684_encaps encaps; | 
 | 75 | 	struct list_head brvccs; | 
 | 76 | #ifdef CONFIG_ATM_BR2684_IPFILTER | 
 | 77 | 	struct br2684_filter filter; | 
 | 78 | #endif /* CONFIG_ATM_BR2684_IPFILTER */ | 
 | 79 | #ifndef FASTER_VERSION | 
 | 80 | 	unsigned copies_needed, copies_failed; | 
 | 81 | #endif /* FASTER_VERSION */ | 
 | 82 | }; | 
 | 83 |  | 
 | 84 | struct br2684_dev { | 
 | 85 | 	struct net_device *net_dev; | 
 | 86 | 	struct list_head br2684_devs; | 
 | 87 | 	int number; | 
 | 88 | 	struct list_head brvccs; /* one device <=> one vcc (before xmas) */ | 
 | 89 | 	struct net_device_stats stats; | 
 | 90 | 	int mac_was_set; | 
 | 91 | }; | 
 | 92 |  | 
 | 93 | /* | 
 | 94 |  * This lock should be held for writing any time the list of devices or | 
 | 95 |  * their attached vcc's could be altered.  It should be held for reading | 
 | 96 |  * any time these are being queried.  Note that we sometimes need to | 
 | 97 |  * do read-locking under interrupt context, so write locking must block | 
 | 98 |  * the current CPU's interrupts | 
 | 99 |  */ | 
 | 100 | static DEFINE_RWLOCK(devs_lock); | 
 | 101 |  | 
 | 102 | static LIST_HEAD(br2684_devs); | 
 | 103 |  | 
 | 104 | static inline struct br2684_dev *BRPRIV(const struct net_device *net_dev) | 
 | 105 | { | 
 | 106 | 	return (struct br2684_dev *) net_dev->priv; | 
 | 107 | } | 
 | 108 |  | 
 | 109 | static inline struct net_device *list_entry_brdev(const struct list_head *le) | 
 | 110 | { | 
 | 111 | 	return list_entry(le, struct br2684_dev, br2684_devs)->net_dev; | 
 | 112 | } | 
 | 113 |  | 
 | 114 | static inline struct br2684_vcc *BR2684_VCC(const struct atm_vcc *atmvcc) | 
 | 115 | { | 
 | 116 | 	return (struct br2684_vcc *) (atmvcc->user_back); | 
 | 117 | } | 
 | 118 |  | 
 | 119 | static inline struct br2684_vcc *list_entry_brvcc(const struct list_head *le) | 
 | 120 | { | 
 | 121 | 	return list_entry(le, struct br2684_vcc, brvccs); | 
 | 122 | } | 
 | 123 |  | 
 | 124 | /* Caller should hold read_lock(&devs_lock) */ | 
 | 125 | static struct net_device *br2684_find_dev(const struct br2684_if_spec *s) | 
 | 126 | { | 
 | 127 | 	struct list_head *lh; | 
 | 128 | 	struct net_device *net_dev; | 
 | 129 | 	switch (s->method) { | 
 | 130 | 	case BR2684_FIND_BYNUM: | 
 | 131 | 		list_for_each(lh, &br2684_devs) { | 
 | 132 | 			net_dev = list_entry_brdev(lh); | 
 | 133 | 			if (BRPRIV(net_dev)->number == s->spec.devnum) | 
 | 134 | 				return net_dev; | 
 | 135 | 		} | 
 | 136 | 		break; | 
 | 137 | 	case BR2684_FIND_BYIFNAME: | 
 | 138 | 		list_for_each(lh, &br2684_devs) { | 
 | 139 | 			net_dev = list_entry_brdev(lh); | 
 | 140 | 			if (!strncmp(net_dev->name, s->spec.ifname, IFNAMSIZ)) | 
 | 141 | 				return net_dev; | 
 | 142 | 		} | 
 | 143 | 		break; | 
 | 144 | 	} | 
 | 145 | 	return NULL; | 
 | 146 | } | 
 | 147 |  | 
 | 148 | /* | 
 | 149 |  * Send a packet out a particular vcc.  Not to useful right now, but paves | 
 | 150 |  * the way for multiple vcc's per itf.  Returns true if we can send, | 
 | 151 |  * otherwise false | 
 | 152 |  */ | 
 | 153 | static int br2684_xmit_vcc(struct sk_buff *skb, struct br2684_dev *brdev, | 
 | 154 | 	struct br2684_vcc *brvcc) | 
 | 155 | { | 
 | 156 | 	struct atm_vcc *atmvcc; | 
 | 157 | #ifdef FASTER_VERSION | 
 | 158 | 	if (brvcc->encaps == e_llc) | 
 | 159 | 		memcpy(skb_push(skb, 8), llc_oui_pid_pad, 8); | 
 | 160 | 	/* last 2 bytes of llc_oui_pid_pad are managed by header routines; | 
 | 161 | 	   yes, you got it: 8 + 2 = sizeof(llc_oui_pid_pad) | 
 | 162 | 	 */ | 
 | 163 | #else | 
 | 164 | 	int minheadroom = (brvcc->encaps == e_llc) ? 10 : 2; | 
 | 165 | 	if (skb_headroom(skb) < minheadroom) { | 
 | 166 | 		struct sk_buff *skb2 = skb_realloc_headroom(skb, minheadroom); | 
 | 167 | 		brvcc->copies_needed++; | 
 | 168 | 		dev_kfree_skb(skb); | 
 | 169 | 		if (skb2 == NULL) { | 
 | 170 | 			brvcc->copies_failed++; | 
 | 171 | 			return 0; | 
 | 172 | 		} | 
 | 173 | 		skb = skb2; | 
 | 174 | 	} | 
 | 175 | 	skb_push(skb, minheadroom); | 
 | 176 | 	if (brvcc->encaps == e_llc) | 
 | 177 | 		memcpy(skb->data, llc_oui_pid_pad, 10); | 
 | 178 | 	else | 
 | 179 | 		memset(skb->data, 0, 2); | 
 | 180 | #endif /* FASTER_VERSION */ | 
 | 181 | 	skb_debug(skb); | 
 | 182 |  | 
 | 183 | 	ATM_SKB(skb)->vcc = atmvcc = brvcc->atmvcc; | 
 | 184 | 	DPRINTK("atm_skb(%p)->vcc(%p)->dev(%p)\n", skb, atmvcc, atmvcc->dev); | 
 | 185 | 	if (!atm_may_send(atmvcc, skb->truesize)) { | 
 | 186 | 		/* we free this here for now, because we cannot know in a higher  | 
 | 187 | 			layer whether the skb point it supplied wasn't freed yet. | 
 | 188 | 			now, it always is. | 
 | 189 | 		*/ | 
 | 190 | 		dev_kfree_skb(skb); | 
 | 191 | 		return 0; | 
 | 192 | 		} | 
 | 193 | 	atomic_add(skb->truesize, &sk_atm(atmvcc)->sk_wmem_alloc); | 
 | 194 | 	ATM_SKB(skb)->atm_options = atmvcc->atm_options; | 
 | 195 | 	brdev->stats.tx_packets++; | 
 | 196 | 	brdev->stats.tx_bytes += skb->len; | 
 | 197 | 	atmvcc->send(atmvcc, skb); | 
 | 198 | 	return 1; | 
 | 199 | } | 
 | 200 |  | 
 | 201 | static inline struct br2684_vcc *pick_outgoing_vcc(struct sk_buff *skb, | 
 | 202 | 	struct br2684_dev *brdev) | 
 | 203 | { | 
 | 204 | 	return list_empty(&brdev->brvccs) ? NULL : | 
 | 205 | 	    list_entry_brvcc(brdev->brvccs.next); /* 1 vcc/dev right now */ | 
 | 206 | } | 
 | 207 |  | 
 | 208 | static int br2684_start_xmit(struct sk_buff *skb, struct net_device *dev) | 
 | 209 | { | 
 | 210 | 	struct br2684_dev *brdev = BRPRIV(dev); | 
 | 211 | 	struct br2684_vcc *brvcc; | 
 | 212 |  | 
 | 213 | 	DPRINTK("br2684_start_xmit, skb->dst=%p\n", skb->dst); | 
 | 214 | 	read_lock(&devs_lock); | 
 | 215 | 	brvcc = pick_outgoing_vcc(skb, brdev); | 
 | 216 | 	if (brvcc == NULL) { | 
 | 217 | 		DPRINTK("no vcc attached to dev %s\n", dev->name); | 
 | 218 | 		brdev->stats.tx_errors++; | 
 | 219 | 		brdev->stats.tx_carrier_errors++; | 
 | 220 | 		/* netif_stop_queue(dev); */ | 
 | 221 | 		dev_kfree_skb(skb); | 
 | 222 | 		read_unlock(&devs_lock); | 
| Jean-Denis Boyer | 4f55cd1 | 2005-10-07 13:44:35 -0700 | [diff] [blame] | 223 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | 	} | 
 | 225 | 	if (!br2684_xmit_vcc(skb, brdev, brvcc)) { | 
 | 226 | 		/* | 
 | 227 | 		 * We should probably use netif_*_queue() here, but that | 
 | 228 | 		 * involves added complication.  We need to walk before | 
 | 229 | 		 * we can run | 
 | 230 | 		 */ | 
 | 231 | 		/* don't free here! this pointer might be no longer valid! | 
 | 232 | 		dev_kfree_skb(skb); | 
 | 233 | 		*/ | 
 | 234 | 		brdev->stats.tx_errors++; | 
 | 235 | 		brdev->stats.tx_fifo_errors++; | 
 | 236 | 	} | 
 | 237 | 	read_unlock(&devs_lock); | 
 | 238 | 	return 0; | 
 | 239 | } | 
 | 240 |  | 
 | 241 | static struct net_device_stats *br2684_get_stats(struct net_device *dev) | 
 | 242 | { | 
 | 243 | 	DPRINTK("br2684_get_stats\n"); | 
 | 244 | 	return &BRPRIV(dev)->stats; | 
 | 245 | } | 
 | 246 |  | 
 | 247 | #ifdef FASTER_VERSION | 
 | 248 | /* | 
 | 249 |  * These mirror eth_header and eth_header_cache.  They are not usually | 
 | 250 |  * exported for use in modules, so we grab them from net_device | 
 | 251 |  * after ether_setup() is done with it.  Bit of a hack. | 
 | 252 |  */ | 
 | 253 | static int (*my_eth_header)(struct sk_buff *, struct net_device *, | 
 | 254 | 	unsigned short, void *, void *, unsigned); | 
 | 255 | static int (*my_eth_header_cache)(struct neighbour *, struct hh_cache *); | 
 | 256 |  | 
 | 257 | static int | 
 | 258 | br2684_header(struct sk_buff *skb, struct net_device *dev, | 
 | 259 | 	      unsigned short type, void *daddr, void *saddr, unsigned len) | 
 | 260 | { | 
 | 261 | 	u16 *pad_before_eth; | 
 | 262 | 	int t = my_eth_header(skb, dev, type, daddr, saddr, len); | 
 | 263 | 	if (t > 0) { | 
 | 264 | 		pad_before_eth = (u16 *) skb_push(skb, 2); | 
 | 265 | 		*pad_before_eth = 0; | 
 | 266 | 		return dev->hard_header_len;	/* or return 16; ? */ | 
 | 267 | 	} else | 
 | 268 | 		return t; | 
 | 269 | } | 
 | 270 |  | 
 | 271 | static int | 
 | 272 | br2684_header_cache(struct neighbour *neigh, struct hh_cache *hh) | 
 | 273 | { | 
 | 274 | /* hh_data is 16 bytes long. if encaps is ether-llc we need 24, so | 
 | 275 | xmit will add the additional header part in that case */ | 
 | 276 | 	u16 *pad_before_eth = (u16 *)(hh->hh_data); | 
 | 277 | 	int t = my_eth_header_cache(neigh, hh); | 
 | 278 | 	DPRINTK("br2684_header_cache, neigh=%p, hh_cache=%p\n", neigh, hh); | 
 | 279 | 	if (t < 0) | 
 | 280 | 		return t; | 
 | 281 | 	else { | 
 | 282 | 		*pad_before_eth = 0; | 
 | 283 | 		hh->hh_len = PADLEN + ETH_HLEN; | 
 | 284 | 	} | 
 | 285 | 	return 0; | 
 | 286 | } | 
 | 287 |  | 
 | 288 | /* | 
 | 289 |  * This is similar to eth_type_trans, which cannot be used because of | 
 | 290 |  * our dev->hard_header_len | 
 | 291 |  */ | 
| Alexey Dobriyan | ab61148 | 2005-07-12 12:08:43 -0700 | [diff] [blame] | 292 | static inline __be16 br_type_trans(struct sk_buff *skb, struct net_device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { | 
 | 294 | 	struct ethhdr *eth; | 
 | 295 | 	unsigned char *rawp; | 
 | 296 | 	eth = eth_hdr(skb); | 
 | 297 |  | 
| Kris Katterjohn | dbbc098 | 2006-01-06 13:05:58 -0800 | [diff] [blame] | 298 | 	if (is_multicast_ether_addr(eth->h_dest)) { | 
| Kris Katterjohn | d3f4a68 | 2006-01-09 16:01:43 -0800 | [diff] [blame] | 299 | 		if (!compare_ether_addr(eth->h_dest, dev->broadcast)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | 			skb->pkt_type = PACKET_BROADCAST; | 
 | 301 | 		else | 
 | 302 | 			skb->pkt_type = PACKET_MULTICAST; | 
 | 303 | 	} | 
 | 304 |  | 
| Kris Katterjohn | d3f4a68 | 2006-01-09 16:01:43 -0800 | [diff] [blame] | 305 | 	else if (compare_ether_addr(eth->h_dest, dev->dev_addr)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | 		skb->pkt_type = PACKET_OTHERHOST; | 
 | 307 |  | 
 | 308 | 	if (ntohs(eth->h_proto) >= 1536) | 
 | 309 | 		return eth->h_proto; | 
 | 310 |  | 
 | 311 | 	rawp = skb->data; | 
 | 312 |  | 
 | 313 | 	/* | 
 | 314 | 	 * This is a magic hack to spot IPX packets. Older Novell breaks | 
 | 315 | 	 * the protocol design and runs IPX over 802.3 without an 802.2 LLC | 
 | 316 | 	 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This | 
 | 317 | 	 * won't work for fault tolerant netware but does for the rest. | 
 | 318 | 	 */ | 
 | 319 | 	if (*(unsigned short *) rawp == 0xFFFF) | 
 | 320 | 		return htons(ETH_P_802_3); | 
 | 321 |  | 
 | 322 | 	/* | 
 | 323 | 	 * Real 802.2 LLC | 
 | 324 | 	 */ | 
 | 325 | 	return htons(ETH_P_802_2); | 
 | 326 | } | 
 | 327 | #endif /* FASTER_VERSION */ | 
 | 328 |  | 
 | 329 | /* | 
 | 330 |  * We remember when the MAC gets set, so we don't override it later with | 
 | 331 |  * the ESI of the ATM card of the first VC | 
 | 332 |  */ | 
 | 333 | static int (*my_eth_mac_addr)(struct net_device *, void *); | 
 | 334 | static int br2684_mac_addr(struct net_device *dev, void *p) | 
 | 335 | { | 
 | 336 | 	int err = my_eth_mac_addr(dev, p); | 
 | 337 | 	if (!err) | 
 | 338 | 		BRPRIV(dev)->mac_was_set = 1; | 
 | 339 | 	return err; | 
 | 340 | } | 
 | 341 |  | 
 | 342 | #ifdef CONFIG_ATM_BR2684_IPFILTER | 
 | 343 | /* this IOCTL is experimental. */ | 
 | 344 | static int br2684_setfilt(struct atm_vcc *atmvcc, void __user *arg) | 
 | 345 | { | 
 | 346 | 	struct br2684_vcc *brvcc; | 
 | 347 | 	struct br2684_filter_set fs; | 
 | 348 |  | 
 | 349 | 	if (copy_from_user(&fs, arg, sizeof fs)) | 
 | 350 | 		return -EFAULT; | 
 | 351 | 	if (fs.ifspec.method != BR2684_FIND_BYNOTHING) { | 
 | 352 | 		/* | 
 | 353 | 		 * This is really a per-vcc thing, but we can also search | 
 | 354 | 		 * by device | 
 | 355 | 		 */ | 
 | 356 | 		struct br2684_dev *brdev; | 
 | 357 | 		read_lock(&devs_lock); | 
 | 358 | 		brdev = BRPRIV(br2684_find_dev(&fs.ifspec)); | 
 | 359 | 		if (brdev == NULL || list_empty(&brdev->brvccs) || | 
 | 360 | 		    brdev->brvccs.next != brdev->brvccs.prev)  /* >1 VCC */ | 
 | 361 | 			brvcc = NULL; | 
 | 362 | 		else | 
 | 363 | 			brvcc = list_entry_brvcc(brdev->brvccs.next); | 
 | 364 | 		read_unlock(&devs_lock); | 
 | 365 | 		if (brvcc == NULL) | 
 | 366 | 			return -ESRCH; | 
 | 367 | 	} else | 
 | 368 | 		brvcc = BR2684_VCC(atmvcc); | 
 | 369 | 	memcpy(&brvcc->filter, &fs.filter, sizeof(brvcc->filter)); | 
 | 370 | 	return 0; | 
 | 371 | } | 
 | 372 |  | 
 | 373 | /* Returns 1 if packet should be dropped */ | 
 | 374 | static inline int | 
 | 375 | packet_fails_filter(u16 type, struct br2684_vcc *brvcc, struct sk_buff *skb) | 
 | 376 | { | 
 | 377 | 	if (brvcc->filter.netmask == 0) | 
 | 378 | 		return 0;			/* no filter in place */ | 
 | 379 | 	if (type == __constant_htons(ETH_P_IP) && | 
 | 380 | 	    (((struct iphdr *) (skb->data))->daddr & brvcc->filter. | 
 | 381 | 	     netmask) == brvcc->filter.prefix) | 
 | 382 | 		return 0; | 
 | 383 | 	if (type == __constant_htons(ETH_P_ARP)) | 
 | 384 | 		return 0; | 
 | 385 | 	/* TODO: we should probably filter ARPs too.. don't want to have | 
 | 386 | 	 *   them returning values that don't make sense, or is that ok? | 
 | 387 | 	 */ | 
 | 388 | 	return 1;		/* drop */ | 
 | 389 | } | 
 | 390 | #endif /* CONFIG_ATM_BR2684_IPFILTER */ | 
 | 391 |  | 
 | 392 | static void br2684_close_vcc(struct br2684_vcc *brvcc) | 
 | 393 | { | 
 | 394 | 	DPRINTK("removing VCC %p from dev %p\n", brvcc, brvcc->device); | 
 | 395 | 	write_lock_irq(&devs_lock); | 
 | 396 | 	list_del(&brvcc->brvccs); | 
 | 397 | 	write_unlock_irq(&devs_lock); | 
 | 398 | 	brvcc->atmvcc->user_back = NULL;	/* what about vcc->recvq ??? */ | 
 | 399 | 	brvcc->old_push(brvcc->atmvcc, NULL);	/* pass on the bad news */ | 
 | 400 | 	kfree(brvcc); | 
 | 401 | 	module_put(THIS_MODULE); | 
 | 402 | } | 
 | 403 |  | 
 | 404 | /* when AAL5 PDU comes in: */ | 
 | 405 | static void br2684_push(struct atm_vcc *atmvcc, struct sk_buff *skb) | 
 | 406 | { | 
 | 407 | 	struct br2684_vcc *brvcc = BR2684_VCC(atmvcc); | 
 | 408 | 	struct net_device *net_dev = brvcc->device; | 
 | 409 | 	struct br2684_dev *brdev = BRPRIV(net_dev); | 
 | 410 | 	int plen = sizeof(llc_oui_pid_pad) + ETH_HLEN; | 
 | 411 |  | 
 | 412 | 	DPRINTK("br2684_push\n"); | 
 | 413 |  | 
 | 414 | 	if (unlikely(skb == NULL)) { | 
 | 415 | 		/* skb==NULL means VCC is being destroyed */ | 
 | 416 | 		br2684_close_vcc(brvcc); | 
 | 417 | 		if (list_empty(&brdev->brvccs)) { | 
 | 418 | 			read_lock(&devs_lock); | 
 | 419 | 			list_del(&brdev->br2684_devs); | 
 | 420 | 			read_unlock(&devs_lock); | 
 | 421 | 			unregister_netdev(net_dev); | 
 | 422 | 			free_netdev(net_dev); | 
 | 423 | 		} | 
 | 424 | 		return; | 
 | 425 | 	} | 
 | 426 |  | 
 | 427 | 	skb_debug(skb); | 
 | 428 | 	atm_return(atmvcc, skb->truesize); | 
 | 429 | 	DPRINTK("skb from brdev %p\n", brdev); | 
 | 430 | 	if (brvcc->encaps == e_llc) { | 
 | 431 | 		/* let us waste some time for checking the encapsulation. | 
 | 432 | 		   Note, that only 7 char is checked so frames with a valid FCS | 
 | 433 | 		   are also accepted (but FCS is not checked of course) */ | 
 | 434 | 		if (memcmp(skb->data, llc_oui_pid_pad, 7)) { | 
 | 435 | 			brdev->stats.rx_errors++; | 
 | 436 | 			dev_kfree_skb(skb); | 
 | 437 | 			return; | 
 | 438 | 		} | 
 | 439 |  | 
 | 440 | 		/* Strip FCS if present */ | 
 | 441 | 		if (skb->len > 7 && skb->data[7] == 0x01) | 
 | 442 | 			__skb_trim(skb, skb->len - 4); | 
 | 443 | 	} else { | 
 | 444 | 		plen = PADLEN + ETH_HLEN;	/* pad, dstmac,srcmac, ethtype */ | 
 | 445 | 		/* first 2 chars should be 0 */ | 
 | 446 | 		if (*((u16 *) (skb->data)) != 0) { | 
 | 447 | 			brdev->stats.rx_errors++; | 
 | 448 | 			dev_kfree_skb(skb); | 
 | 449 | 			return; | 
 | 450 | 		} | 
 | 451 | 	} | 
 | 452 | 	if (skb->len < plen) { | 
 | 453 | 		brdev->stats.rx_errors++; | 
 | 454 | 		dev_kfree_skb(skb);	/* dev_ not needed? */ | 
 | 455 | 		return; | 
 | 456 | 	} | 
 | 457 |  | 
 | 458 | #ifdef FASTER_VERSION | 
 | 459 | 	/* FIXME: tcpdump shows that pointer to mac header is 2 bytes earlier, | 
 | 460 | 	   than should be. What else should I set? */ | 
 | 461 | 	skb_pull(skb, plen); | 
 | 462 | 	skb->mac.raw = ((char *) (skb->data)) - ETH_HLEN; | 
 | 463 | 	skb->pkt_type = PACKET_HOST; | 
 | 464 | #ifdef CONFIG_BR2684_FAST_TRANS | 
 | 465 | 	skb->protocol = ((u16 *) skb->data)[-1]; | 
 | 466 | #else				/* some protocols might require this: */ | 
 | 467 | 	skb->protocol = br_type_trans(skb, net_dev); | 
 | 468 | #endif /* CONFIG_BR2684_FAST_TRANS */ | 
 | 469 | #else | 
 | 470 | 	skb_pull(skb, plen - ETH_HLEN); | 
 | 471 | 	skb->protocol = eth_type_trans(skb, net_dev); | 
 | 472 | #endif /* FASTER_VERSION */ | 
 | 473 | #ifdef CONFIG_ATM_BR2684_IPFILTER | 
 | 474 | 	if (unlikely(packet_fails_filter(skb->protocol, brvcc, skb))) { | 
 | 475 | 		brdev->stats.rx_dropped++; | 
 | 476 | 		dev_kfree_skb(skb); | 
 | 477 | 		return; | 
 | 478 | 	} | 
 | 479 | #endif /* CONFIG_ATM_BR2684_IPFILTER */ | 
 | 480 | 	skb->dev = net_dev; | 
 | 481 | 	ATM_SKB(skb)->vcc = atmvcc;	/* needed ? */ | 
 | 482 | 	DPRINTK("received packet's protocol: %x\n", ntohs(skb->protocol)); | 
 | 483 | 	skb_debug(skb); | 
 | 484 | 	if (unlikely(!(net_dev->flags & IFF_UP))) { | 
 | 485 | 		/* sigh, interface is down */ | 
 | 486 | 		brdev->stats.rx_dropped++; | 
 | 487 | 		dev_kfree_skb(skb); | 
 | 488 | 		return; | 
 | 489 | 	} | 
 | 490 | 	brdev->stats.rx_packets++; | 
 | 491 | 	brdev->stats.rx_bytes += skb->len; | 
 | 492 | 	memset(ATM_SKB(skb), 0, sizeof(struct atm_skb_data)); | 
 | 493 | 	netif_rx(skb); | 
 | 494 | } | 
 | 495 |  | 
 | 496 | static int br2684_regvcc(struct atm_vcc *atmvcc, void __user *arg) | 
 | 497 | { | 
 | 498 | /* assign a vcc to a dev | 
 | 499 | Note: we do not have explicit unassign, but look at _push() | 
 | 500 | */ | 
 | 501 | 	int err; | 
 | 502 | 	struct br2684_vcc *brvcc; | 
 | 503 | 	struct sk_buff_head copy; | 
 | 504 | 	struct sk_buff *skb; | 
 | 505 | 	struct br2684_dev *brdev; | 
 | 506 | 	struct net_device *net_dev; | 
 | 507 | 	struct atm_backend_br2684 be; | 
 | 508 |  | 
 | 509 | 	if (copy_from_user(&be, arg, sizeof be)) | 
 | 510 | 		return -EFAULT; | 
| Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 511 | 	brvcc = kzalloc(sizeof(struct br2684_vcc), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | 	if (!brvcc) | 
 | 513 | 		return -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | 	write_lock_irq(&devs_lock); | 
 | 515 | 	net_dev = br2684_find_dev(&be.ifspec); | 
 | 516 | 	if (net_dev == NULL) { | 
 | 517 | 		printk(KERN_ERR | 
 | 518 | 		    "br2684: tried to attach to non-existant device\n"); | 
 | 519 | 		err = -ENXIO; | 
 | 520 | 		goto error; | 
 | 521 | 	} | 
 | 522 | 	brdev = BRPRIV(net_dev); | 
 | 523 | 	if (atmvcc->push == NULL) { | 
 | 524 | 		err = -EBADFD; | 
 | 525 | 		goto error; | 
 | 526 | 	} | 
 | 527 | 	if (!list_empty(&brdev->brvccs)) { | 
 | 528 | 		/* Only 1 VCC/dev right now */ | 
 | 529 | 		err = -EEXIST; | 
 | 530 | 		goto error; | 
 | 531 | 	} | 
 | 532 | 	if (be.fcs_in != BR2684_FCSIN_NO || be.fcs_out != BR2684_FCSOUT_NO || | 
 | 533 | 	    be.fcs_auto || be.has_vpiid || be.send_padding || (be.encaps != | 
 | 534 | 	    BR2684_ENCAPS_VC && be.encaps != BR2684_ENCAPS_LLC) || | 
 | 535 | 	    be.min_size != 0) { | 
 | 536 | 		err = -EINVAL; | 
 | 537 | 		goto error; | 
 | 538 | 	} | 
 | 539 | 	DPRINTK("br2684_regvcc vcc=%p, encaps=%d, brvcc=%p\n", atmvcc, be.encaps, | 
 | 540 | 		brvcc); | 
 | 541 | 	if (list_empty(&brdev->brvccs) && !brdev->mac_was_set) { | 
 | 542 | 		unsigned char *esi = atmvcc->dev->esi; | 
 | 543 | 		if (esi[0] | esi[1] | esi[2] | esi[3] | esi[4] | esi[5]) | 
 | 544 | 			memcpy(net_dev->dev_addr, esi, net_dev->addr_len); | 
 | 545 | 		else | 
 | 546 | 			net_dev->dev_addr[2] = 1; | 
 | 547 | 	} | 
 | 548 | 	list_add(&brvcc->brvccs, &brdev->brvccs); | 
 | 549 | 	write_unlock_irq(&devs_lock); | 
 | 550 | 	brvcc->device = net_dev; | 
 | 551 | 	brvcc->atmvcc = atmvcc; | 
 | 552 | 	atmvcc->user_back = brvcc; | 
 | 553 | 	brvcc->encaps = (enum br2684_encaps) be.encaps; | 
 | 554 | 	brvcc->old_push = atmvcc->push; | 
 | 555 | 	barrier(); | 
 | 556 | 	atmvcc->push = br2684_push; | 
 | 557 | 	skb_queue_head_init(©); | 
 | 558 | 	skb_migrate(&sk_atm(atmvcc)->sk_receive_queue, ©); | 
 | 559 | 	while ((skb = skb_dequeue(©)) != NULL) { | 
 | 560 | 		BRPRIV(skb->dev)->stats.rx_bytes -= skb->len; | 
 | 561 | 		BRPRIV(skb->dev)->stats.rx_packets--; | 
 | 562 | 		br2684_push(atmvcc, skb); | 
 | 563 | 	} | 
 | 564 | 	__module_get(THIS_MODULE); | 
 | 565 | 	return 0; | 
 | 566 |     error: | 
 | 567 | 	write_unlock_irq(&devs_lock); | 
 | 568 | 	kfree(brvcc); | 
 | 569 | 	return err; | 
 | 570 | } | 
 | 571 |  | 
 | 572 | static void br2684_setup(struct net_device *netdev) | 
 | 573 | { | 
 | 574 | 	struct br2684_dev *brdev = BRPRIV(netdev); | 
 | 575 |  | 
 | 576 | 	ether_setup(netdev); | 
 | 577 | 	brdev->net_dev = netdev; | 
 | 578 |  | 
 | 579 | #ifdef FASTER_VERSION | 
 | 580 | 	my_eth_header = netdev->hard_header; | 
 | 581 | 	netdev->hard_header = br2684_header; | 
 | 582 | 	my_eth_header_cache = netdev->hard_header_cache; | 
 | 583 | 	netdev->hard_header_cache = br2684_header_cache; | 
 | 584 | 	netdev->hard_header_len = sizeof(llc_oui_pid_pad) + ETH_HLEN;	/* 10 + 14 */ | 
 | 585 | #endif | 
 | 586 | 	my_eth_mac_addr = netdev->set_mac_address; | 
 | 587 | 	netdev->set_mac_address = br2684_mac_addr; | 
 | 588 | 	netdev->hard_start_xmit = br2684_start_xmit; | 
 | 589 | 	netdev->get_stats = br2684_get_stats; | 
 | 590 |  | 
 | 591 | 	INIT_LIST_HEAD(&brdev->brvccs); | 
 | 592 | } | 
 | 593 |  | 
 | 594 | static int br2684_create(void __user *arg) | 
 | 595 | { | 
 | 596 | 	int err; | 
 | 597 | 	struct net_device *netdev; | 
 | 598 | 	struct br2684_dev *brdev; | 
 | 599 | 	struct atm_newif_br2684 ni; | 
 | 600 |  | 
 | 601 | 	DPRINTK("br2684_create\n"); | 
 | 602 |  | 
 | 603 | 	if (copy_from_user(&ni, arg, sizeof ni)) { | 
 | 604 | 		return -EFAULT; | 
 | 605 | 	} | 
 | 606 | 	if (ni.media != BR2684_MEDIA_ETHERNET || ni.mtu != 1500) { | 
 | 607 | 		return -EINVAL; | 
 | 608 | 	} | 
 | 609 |  | 
 | 610 | 	netdev = alloc_netdev(sizeof(struct br2684_dev), | 
 | 611 | 			      ni.ifname[0] ? ni.ifname : "nas%d", | 
 | 612 | 			      br2684_setup); | 
 | 613 | 	if (!netdev) | 
 | 614 | 		return -ENOMEM; | 
 | 615 |  | 
 | 616 | 	brdev = BRPRIV(netdev); | 
 | 617 |  | 
 | 618 | 	DPRINTK("registered netdev %s\n", netdev->name); | 
 | 619 | 	/* open, stop, do_ioctl ? */ | 
 | 620 | 	err = register_netdev(netdev); | 
 | 621 | 	if (err < 0) { | 
 | 622 | 		printk(KERN_ERR "br2684_create: register_netdev failed\n"); | 
 | 623 | 		free_netdev(netdev); | 
 | 624 | 		return err; | 
 | 625 | 	} | 
 | 626 |  | 
 | 627 | 	write_lock_irq(&devs_lock); | 
 | 628 | 	brdev->number = list_empty(&br2684_devs) ? 1 : | 
 | 629 | 	    BRPRIV(list_entry_brdev(br2684_devs.prev))->number + 1; | 
 | 630 | 	list_add_tail(&brdev->br2684_devs, &br2684_devs); | 
 | 631 | 	write_unlock_irq(&devs_lock); | 
 | 632 | 	return 0; | 
 | 633 | } | 
 | 634 |  | 
 | 635 | /* | 
 | 636 |  * This handles ioctls actually performed on our vcc - we must return | 
 | 637 |  * -ENOIOCTLCMD for any unrecognized ioctl | 
 | 638 |  */ | 
 | 639 | static int br2684_ioctl(struct socket *sock, unsigned int cmd, | 
 | 640 | 	unsigned long arg) | 
 | 641 | { | 
 | 642 | 	struct atm_vcc *atmvcc = ATM_SD(sock); | 
 | 643 | 	void __user *argp = (void __user *)arg; | 
 | 644 |  | 
 | 645 | 	int err; | 
 | 646 | 	switch(cmd) { | 
 | 647 | 	case ATM_SETBACKEND: | 
 | 648 | 	case ATM_NEWBACKENDIF: { | 
 | 649 | 		atm_backend_t b; | 
 | 650 | 		err = get_user(b, (atm_backend_t __user *) argp); | 
 | 651 | 		if (err) | 
 | 652 | 			return -EFAULT; | 
 | 653 | 		if (b != ATM_BACKEND_BR2684) | 
 | 654 | 			return -ENOIOCTLCMD; | 
 | 655 | 		if (!capable(CAP_NET_ADMIN)) | 
 | 656 | 			return -EPERM; | 
 | 657 | 		if (cmd == ATM_SETBACKEND) | 
 | 658 | 			return br2684_regvcc(atmvcc, argp); | 
 | 659 | 		else | 
 | 660 | 			return br2684_create(argp); | 
 | 661 | 		} | 
 | 662 | #ifdef CONFIG_ATM_BR2684_IPFILTER | 
 | 663 | 	case BR2684_SETFILT: | 
 | 664 | 		if (atmvcc->push != br2684_push) | 
 | 665 | 			return -ENOIOCTLCMD; | 
 | 666 | 		if (!capable(CAP_NET_ADMIN)) | 
 | 667 | 			return -EPERM; | 
 | 668 | 		err = br2684_setfilt(atmvcc, argp); | 
 | 669 | 		return err; | 
 | 670 | #endif /* CONFIG_ATM_BR2684_IPFILTER */ | 
 | 671 | 	} | 
 | 672 | 	return -ENOIOCTLCMD; | 
 | 673 | } | 
 | 674 |  | 
 | 675 | static struct atm_ioctl br2684_ioctl_ops = { | 
 | 676 | 	.owner	= THIS_MODULE, | 
 | 677 | 	.ioctl	= br2684_ioctl, | 
 | 678 | }; | 
 | 679 |  | 
 | 680 |  | 
 | 681 | #ifdef CONFIG_PROC_FS | 
 | 682 | static void *br2684_seq_start(struct seq_file *seq, loff_t *pos) | 
 | 683 | { | 
 | 684 | 	loff_t offs = 0; | 
 | 685 | 	struct br2684_dev *brd; | 
 | 686 |  | 
 | 687 | 	read_lock(&devs_lock); | 
 | 688 |  | 
 | 689 | 	list_for_each_entry(brd, &br2684_devs, br2684_devs) { | 
 | 690 | 		if (offs == *pos) | 
 | 691 | 			return brd; | 
 | 692 | 		++offs; | 
 | 693 | 	} | 
 | 694 | 	return NULL; | 
 | 695 | } | 
 | 696 |  | 
 | 697 | static void *br2684_seq_next(struct seq_file *seq, void *v, loff_t *pos) | 
 | 698 | { | 
 | 699 | 	struct br2684_dev *brd = v; | 
 | 700 |  | 
 | 701 | 	++*pos; | 
 | 702 |  | 
 | 703 | 	brd = list_entry(brd->br2684_devs.next,  | 
 | 704 | 			 struct br2684_dev, br2684_devs); | 
 | 705 | 	return (&brd->br2684_devs != &br2684_devs) ? brd : NULL; | 
 | 706 | } | 
 | 707 |  | 
 | 708 | static void br2684_seq_stop(struct seq_file *seq, void *v) | 
 | 709 | { | 
 | 710 | 	read_unlock(&devs_lock); | 
 | 711 | } | 
 | 712 |  | 
 | 713 | static int br2684_seq_show(struct seq_file *seq, void *v) | 
 | 714 | { | 
 | 715 | 	const struct br2684_dev *brdev = v; | 
 | 716 | 	const struct net_device *net_dev = brdev->net_dev; | 
 | 717 | 	const struct br2684_vcc *brvcc; | 
 | 718 |  | 
 | 719 | 	seq_printf(seq, "dev %.16s: num=%d, mac=%02X:%02X:" | 
 | 720 | 		       "%02X:%02X:%02X:%02X (%s)\n", net_dev->name, | 
 | 721 | 		       brdev->number, | 
 | 722 | 		       net_dev->dev_addr[0], | 
 | 723 | 		       net_dev->dev_addr[1], | 
 | 724 | 		       net_dev->dev_addr[2], | 
 | 725 | 		       net_dev->dev_addr[3], | 
 | 726 | 		       net_dev->dev_addr[4], | 
 | 727 | 		       net_dev->dev_addr[5], | 
 | 728 | 		       brdev->mac_was_set ? "set" : "auto"); | 
 | 729 |  | 
 | 730 | 	list_for_each_entry(brvcc, &brdev->brvccs, brvccs) { | 
 | 731 | 		seq_printf(seq, "  vcc %d.%d.%d: encaps=%s" | 
 | 732 | #ifndef FASTER_VERSION | 
 | 733 | 				    ", failed copies %u/%u" | 
 | 734 | #endif /* FASTER_VERSION */ | 
 | 735 | 				    "\n", brvcc->atmvcc->dev->number, | 
 | 736 | 				    brvcc->atmvcc->vpi, brvcc->atmvcc->vci, | 
 | 737 | 				    (brvcc->encaps == e_llc) ? "LLC" : "VC" | 
 | 738 | #ifndef FASTER_VERSION | 
 | 739 | 				    , brvcc->copies_failed | 
 | 740 | 				    , brvcc->copies_needed | 
 | 741 | #endif /* FASTER_VERSION */ | 
 | 742 | 				    ); | 
 | 743 | #ifdef CONFIG_ATM_BR2684_IPFILTER | 
 | 744 | #define b1(var, byte)	((u8 *) &brvcc->filter.var)[byte] | 
 | 745 | #define bs(var)		b1(var, 0), b1(var, 1), b1(var, 2), b1(var, 3) | 
 | 746 | 			if (brvcc->filter.netmask != 0) | 
 | 747 | 				seq_printf(seq, "    filter=%d.%d.%d.%d/" | 
 | 748 | 						"%d.%d.%d.%d\n", | 
 | 749 | 						bs(prefix), bs(netmask)); | 
 | 750 | #undef bs | 
 | 751 | #undef b1 | 
 | 752 | #endif /* CONFIG_ATM_BR2684_IPFILTER */ | 
 | 753 | 	} | 
 | 754 | 	return 0; | 
 | 755 | } | 
 | 756 |  | 
 | 757 | static struct seq_operations br2684_seq_ops = { | 
 | 758 | 	.start = br2684_seq_start, | 
 | 759 | 	.next  = br2684_seq_next, | 
 | 760 | 	.stop  = br2684_seq_stop, | 
 | 761 | 	.show  = br2684_seq_show, | 
 | 762 | }; | 
 | 763 |  | 
 | 764 | static int br2684_proc_open(struct inode *inode, struct file *file) | 
 | 765 | { | 
 | 766 | 	return seq_open(file, &br2684_seq_ops); | 
 | 767 | } | 
 | 768 |  | 
 | 769 | static struct file_operations br2684_proc_ops = { | 
 | 770 | 	.owner   = THIS_MODULE, | 
 | 771 | 	.open    = br2684_proc_open, | 
 | 772 | 	.read    = seq_read, | 
 | 773 | 	.llseek  = seq_lseek, | 
 | 774 | 	.release = seq_release, | 
 | 775 | }; | 
 | 776 |  | 
 | 777 | extern struct proc_dir_entry *atm_proc_root;	/* from proc.c */ | 
 | 778 | #endif | 
 | 779 |  | 
 | 780 | static int __init br2684_init(void) | 
 | 781 | { | 
 | 782 | #ifdef CONFIG_PROC_FS | 
 | 783 | 	struct proc_dir_entry *p; | 
 | 784 | 	if ((p = create_proc_entry("br2684", 0, atm_proc_root)) == NULL) | 
 | 785 | 		return -ENOMEM; | 
 | 786 | 	p->proc_fops = &br2684_proc_ops; | 
 | 787 | #endif | 
 | 788 | 	register_atm_ioctl(&br2684_ioctl_ops); | 
 | 789 | 	return 0; | 
 | 790 | } | 
 | 791 |  | 
 | 792 | static void __exit br2684_exit(void) | 
 | 793 | { | 
 | 794 | 	struct net_device *net_dev; | 
 | 795 | 	struct br2684_dev *brdev; | 
 | 796 | 	struct br2684_vcc *brvcc; | 
 | 797 | 	deregister_atm_ioctl(&br2684_ioctl_ops); | 
 | 798 |  | 
 | 799 | #ifdef CONFIG_PROC_FS | 
 | 800 | 	remove_proc_entry("br2684", atm_proc_root); | 
 | 801 | #endif | 
 | 802 |  | 
 | 803 | 	while (!list_empty(&br2684_devs)) { | 
 | 804 | 		net_dev = list_entry_brdev(br2684_devs.next); | 
 | 805 | 		brdev = BRPRIV(net_dev); | 
 | 806 | 		while (!list_empty(&brdev->brvccs)) { | 
 | 807 | 			brvcc = list_entry_brvcc(brdev->brvccs.next); | 
 | 808 | 			br2684_close_vcc(brvcc); | 
 | 809 | 		} | 
 | 810 |  | 
 | 811 | 		list_del(&brdev->br2684_devs); | 
 | 812 | 		unregister_netdev(net_dev); | 
 | 813 | 		free_netdev(net_dev); | 
 | 814 | 	} | 
 | 815 | } | 
 | 816 |  | 
 | 817 | module_init(br2684_init); | 
 | 818 | module_exit(br2684_exit); | 
 | 819 |  | 
 | 820 | MODULE_AUTHOR("Marcell GAL"); | 
 | 821 | MODULE_DESCRIPTION("RFC2684 bridged protocols over ATM/AAL5"); | 
 | 822 | MODULE_LICENSE("GPL"); |