| 1 |
/* |
| 2 |
* Dynamic Span Interface for DAHDI (Ethernet Interface) |
| 3 |
* |
| 4 |
* Written by Mark Spencer <[email protected]> |
| 5 |
* |
| 6 |
* Copyright (C) 2001-2012, Digium, Inc. |
| 7 |
* |
| 8 |
* All rights reserved. |
| 9 |
* |
| 10 |
*/ |
| 11 |
|
| 12 |
/* |
| 13 |
* See http://www.asterisk.org for more information about |
| 14 |
* the Asterisk project. Please do not directly contact |
| 15 |
* any of the maintainers of this project for assistance; |
| 16 |
* the project provides a web site, mailing lists and IRC |
| 17 |
* channels for your use. |
| 18 |
* |
| 19 |
* This program is free software, distributed under the terms of |
| 20 |
* the GNU General Public License Version 2 as published by the |
| 21 |
* Free Software Foundation. See the LICENSE file included with |
| 22 |
* this program for more details. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <linux/kernel.h> |
| 26 |
#include <linux/errno.h> |
| 27 |
#include <linux/module.h> |
| 28 |
#include <linux/init.h> |
| 29 |
#include <linux/spinlock.h> |
| 30 |
#include <linux/slab.h> |
| 31 |
#include <linux/kmod.h> |
| 32 |
#include <linux/netdevice.h> |
| 33 |
#include <linux/notifier.h> |
| 34 |
|
| 35 |
#include <dahdi/kernel.h> |
| 36 |
|
| 37 |
#define ETH_P_DAHDI_DETH 0xd00d |
| 38 |
|
| 39 |
struct ztdeth_header { |
| 40 |
unsigned short subaddr; |
| 41 |
}; |
| 42 |
|
| 43 |
/* We take the raw message, put it in an ethernet frame, and add a |
| 44 |
two byte addressing header at the top for future use */ |
| 45 |
static DEFINE_SPINLOCK(zlock); |
| 46 |
|
| 47 |
static struct sk_buff_head skbs; |
| 48 |
|
| 49 |
static struct ztdeth { |
| 50 |
unsigned char addr[ETH_ALEN]; |
| 51 |
unsigned short subaddr; /* Network byte order */ |
| 52 |
struct dahdi_span *span; |
| 53 |
char ethdev[IFNAMSIZ]; |
| 54 |
struct net_device *dev; |
| 55 |
struct ztdeth *next; |
| 56 |
} *zdevs = NULL; |
| 57 |
|
| 58 |
static struct dahdi_span *ztdeth_getspan(unsigned char *addr, unsigned short subaddr) |
| 59 |
{ |
| 60 |
unsigned long flags; |
| 61 |
struct ztdeth *z; |
| 62 |
struct dahdi_span *span = NULL; |
| 63 |
spin_lock_irqsave(&zlock, flags); |
| 64 |
z = zdevs; |
| 65 |
while(z) { |
| 66 |
if (!memcmp(addr, z->addr, ETH_ALEN) && |
| 67 |
z->subaddr == subaddr) |
| 68 |
break; |
| 69 |
z = z->next; |
| 70 |
} |
| 71 |
if (z) |
| 72 |
span = z->span; |
| 73 |
spin_unlock_irqrestore(&zlock, flags); |
| 74 |
if (!span || !test_bit(DAHDI_FLAGBIT_REGISTERED, &span->flags)) |
| 75 |
return NULL; |
| 76 |
return span; |
| 77 |
} |
| 78 |
|
| 79 |
static int ztdeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev) |
| 80 |
{ |
| 81 |
struct dahdi_span *span; |
| 82 |
struct ztdeth_header *zh; |
| 83 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22) |
| 84 |
zh = (struct ztdeth_header *)skb_network_header(skb); |
| 85 |
#else |
| 86 |
zh = (struct ztdeth_header *)skb->nh.raw; |
| 87 |
#endif |
| 88 |
span = ztdeth_getspan(eth_hdr(skb)->h_source, zh->subaddr); |
| 89 |
if (span) { |
| 90 |
skb_pull(skb, sizeof(struct ztdeth_header)); |
| 91 |
#ifdef NEW_SKB_LINEARIZE |
| 92 |
if (skb_is_nonlinear(skb)) |
| 93 |
skb_linearize(skb); |
| 94 |
#else |
| 95 |
if (skb_is_nonlinear(skb)) |
| 96 |
skb_linearize(skb, GFP_KERNEL); |
| 97 |
#endif |
| 98 |
dahdi_dynamic_receive(span, (unsigned char *)skb->data, skb->len); |
| 99 |
} |
| 100 |
kfree_skb(skb); |
| 101 |
return 0; |
| 102 |
} |
| 103 |
|
| 104 |
static int ztdeth_notifier(struct notifier_block *block, unsigned long event, void *ptr) |
| 105 |
{ |
| 106 |
struct net_device *dev = ptr; |
| 107 |
struct ztdeth *z; |
| 108 |
unsigned long flags; |
| 109 |
switch(event) { |
| 110 |
case NETDEV_GOING_DOWN: |
| 111 |
case NETDEV_DOWN: |
| 112 |
spin_lock_irqsave(&zlock, flags); |
| 113 |
z = zdevs; |
| 114 |
while(z) { |
| 115 |
/* Note that the device no longer exists */ |
| 116 |
if (z->dev == dev) |
| 117 |
z->dev = NULL; |
| 118 |
z = z->next; |
| 119 |
} |
| 120 |
spin_unlock_irqrestore(&zlock, flags); |
| 121 |
break; |
| 122 |
case NETDEV_UP: |
| 123 |
spin_lock_irqsave(&zlock, flags); |
| 124 |
z = zdevs; |
| 125 |
while(z) { |
| 126 |
/* Now that the device exists again, use it */ |
| 127 |
if (!strcmp(z->ethdev, dev->name)) |
| 128 |
z->dev = dev; |
| 129 |
z = z->next; |
| 130 |
} |
| 131 |
spin_unlock_irqrestore(&zlock, flags); |
| 132 |
break; |
| 133 |
} |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
static void ztdeth_transmit(struct dahdi_dynamic *dyn, u8 *msg, size_t msglen) |
| 138 |
{ |
| 139 |
struct ztdeth *z; |
| 140 |
struct sk_buff *skb; |
| 141 |
struct ztdeth_header *zh; |
| 142 |
unsigned long flags; |
| 143 |
struct net_device *dev; |
| 144 |
unsigned char addr[ETH_ALEN]; |
| 145 |
unsigned short subaddr; /* Network byte order */ |
| 146 |
|
| 147 |
spin_lock_irqsave(&zlock, flags); |
| 148 |
z = dyn->pvt; |
| 149 |
if (z && z->dev) { |
| 150 |
/* Copy fields to local variables to remove spinlock ASAP */ |
| 151 |
dev = z->dev; |
| 152 |
memcpy(addr, z->addr, sizeof(z->addr)); |
| 153 |
subaddr = z->subaddr; |
| 154 |
spin_unlock_irqrestore(&zlock, flags); |
| 155 |
skb = dev_alloc_skb(msglen + dev->hard_header_len + sizeof(struct ztdeth_header) + 32); |
| 156 |
if (skb) { |
| 157 |
/* Reserve header space */ |
| 158 |
skb_reserve(skb, dev->hard_header_len + sizeof(struct ztdeth_header)); |
| 159 |
|
| 160 |
/* Copy message body */ |
| 161 |
memcpy(skb_put(skb, msglen), msg, msglen); |
| 162 |
|
| 163 |
/* Throw on header */ |
| 164 |
zh = (struct ztdeth_header *)skb_push(skb, sizeof(struct ztdeth_header)); |
| 165 |
zh->subaddr = subaddr; |
| 166 |
|
| 167 |
/* Setup protocol and such */ |
| 168 |
skb->protocol = __constant_htons(ETH_P_DAHDI_DETH); |
| 169 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22) |
| 170 |
skb_set_network_header(skb, 0); |
| 171 |
#else |
| 172 |
skb->nh.raw = skb->data; |
| 173 |
#endif |
| 174 |
skb->dev = dev; |
| 175 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24) |
| 176 |
dev_hard_header(skb, dev, ETH_P_DAHDI_DETH, addr, dev->dev_addr, skb->len); |
| 177 |
#else |
| 178 |
if (dev->hard_header) |
| 179 |
dev->hard_header(skb, dev, ETH_P_DAHDI_DETH, addr, dev->dev_addr, skb->len); |
| 180 |
#endif |
| 181 |
skb_queue_tail(&skbs, skb); |
| 182 |
} |
| 183 |
} |
| 184 |
else |
| 185 |
spin_unlock_irqrestore(&zlock, flags); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* ztdeth_flush - Flush all pending transactions. |
| 190 |
* |
| 191 |
* This function is always called in softirq context. |
| 192 |
*/ |
| 193 |
static int ztdeth_flush(void) |
| 194 |
{ |
| 195 |
struct sk_buff *skb; |
| 196 |
while ((skb = skb_dequeue(&skbs))) |
| 197 |
dev_queue_xmit(skb); |
| 198 |
return 0; |
| 199 |
} |
| 200 |
|
| 201 |
static struct packet_type ztdeth_ptype = { |
| 202 |
.type = __constant_htons(ETH_P_DAHDI_DETH), /* Protocol */ |
| 203 |
.dev = NULL, /* Device (NULL = wildcard) */ |
| 204 |
.func = ztdeth_rcv, /* Receiver */ |
| 205 |
}; |
| 206 |
|
| 207 |
static int digit2int(char d) |
| 208 |
{ |
| 209 |
switch(d) { |
| 210 |
case 'F': |
| 211 |
case 'E': |
| 212 |
case 'D': |
| 213 |
case 'C': |
| 214 |
case 'B': |
| 215 |
case 'A': |
| 216 |
return d - 'A' + 10; |
| 217 |
case 'f': |
| 218 |
case 'e': |
| 219 |
case 'd': |
| 220 |
case 'c': |
| 221 |
case 'b': |
| 222 |
case 'a': |
| 223 |
return d - 'a' + 10; |
| 224 |
case '9': |
| 225 |
case '8': |
| 226 |
case '7': |
| 227 |
case '6': |
| 228 |
case '5': |
| 229 |
case '4': |
| 230 |
case '3': |
| 231 |
case '2': |
| 232 |
case '1': |
| 233 |
case '0': |
| 234 |
return d - '0'; |
| 235 |
} |
| 236 |
return -1; |
| 237 |
} |
| 238 |
|
| 239 |
static int hex2int(char *s) |
| 240 |
{ |
| 241 |
int res; |
| 242 |
int tmp; |
| 243 |
/* Gotta be at least one digit */ |
| 244 |
if (strlen(s) < 1) |
| 245 |
return -1; |
| 246 |
/* Can't be more than two */ |
| 247 |
if (strlen(s) > 2) |
| 248 |
return -1; |
| 249 |
/* Grab the first digit */ |
| 250 |
res = digit2int(s[0]); |
| 251 |
if (res < 0) |
| 252 |
return -1; |
| 253 |
tmp = res; |
| 254 |
/* Grab the next */ |
| 255 |
if (strlen(s) > 1) { |
| 256 |
res = digit2int(s[1]); |
| 257 |
if (res < 0) |
| 258 |
return -1; |
| 259 |
tmp = tmp * 16 + res; |
| 260 |
} |
| 261 |
return tmp; |
| 262 |
} |
| 263 |
|
| 264 |
static void ztdeth_destroy(struct dahdi_dynamic *dyn) |
| 265 |
{ |
| 266 |
struct ztdeth *z = dyn->pvt; |
| 267 |
unsigned long flags; |
| 268 |
struct ztdeth *prev=NULL, *cur; |
| 269 |
|
| 270 |
spin_lock_irqsave(&zlock, flags); |
| 271 |
cur = zdevs; |
| 272 |
while(cur) { |
| 273 |
if (cur == z) { |
| 274 |
if (prev) |
| 275 |
prev->next = cur->next; |
| 276 |
else |
| 277 |
zdevs = cur->next; |
| 278 |
break; |
| 279 |
} |
| 280 |
prev = cur; |
| 281 |
cur = cur->next; |
| 282 |
} |
| 283 |
spin_unlock_irqrestore(&zlock, flags); |
| 284 |
|
| 285 |
if (cur == z) { /* Successfully removed */ |
| 286 |
dyn->pvt = NULL; |
| 287 |
dev_put(z->dev); |
| 288 |
printk(KERN_INFO "TDMoE: Removed interface for %s\n", z->span->name); |
| 289 |
kfree(z); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
static int ztdeth_create(struct dahdi_dynamic *dyn, const char *addr) |
| 294 |
{ |
| 295 |
struct ztdeth *z; |
| 296 |
char src[256]; |
| 297 |
char tmp[256], *tmp2, *tmp3, *tmp4 = NULL; |
| 298 |
int res,x; |
| 299 |
unsigned long flags; |
| 300 |
struct dahdi_span *const span = &dyn->span; |
| 301 |
|
| 302 |
z = kmalloc(sizeof(struct ztdeth), GFP_KERNEL); |
| 303 |
if (z) { |
| 304 |
/* Zero it out */ |
| 305 |
memset(z, 0, sizeof(struct ztdeth)); |
| 306 |
|
| 307 |
/* Address should be <dev>/<macaddr>[/subaddr] */ |
| 308 |
strlcpy(tmp, addr, sizeof(tmp)); |
| 309 |
tmp2 = strchr(tmp, '/'); |
| 310 |
if (tmp2) { |
| 311 |
*tmp2 = '\0'; |
| 312 |
tmp2++; |
| 313 |
strlcpy(z->ethdev, tmp, sizeof(z->ethdev)); |
| 314 |
} else { |
| 315 |
printk(KERN_NOTICE "Invalid TDMoE address (no device) '%s'\n", addr); |
| 316 |
kfree(z); |
| 317 |
return -EINVAL; |
| 318 |
} |
| 319 |
if (tmp2) { |
| 320 |
tmp4 = strchr(tmp2+1, '/'); |
| 321 |
if (tmp4) { |
| 322 |
*tmp4 = '\0'; |
| 323 |
tmp4++; |
| 324 |
} |
| 325 |
/* We don't have SSCANF :( Gotta do this the hard way */ |
| 326 |
tmp3 = strchr(tmp2, ':'); |
| 327 |
for (x=0;x<6;x++) { |
| 328 |
if (tmp2) { |
| 329 |
if (tmp3) { |
| 330 |
*tmp3 = '\0'; |
| 331 |
tmp3++; |
| 332 |
} |
| 333 |
res = hex2int(tmp2); |
| 334 |
if (res < 0) |
| 335 |
break; |
| 336 |
z->addr[x] = res & 0xff; |
| 337 |
} else |
| 338 |
break; |
| 339 |
if ((tmp2 = tmp3)) |
| 340 |
tmp3 = strchr(tmp2, ':'); |
| 341 |
} |
| 342 |
if (x != 6) { |
| 343 |
printk(KERN_NOTICE "TDMoE: Invalid MAC address in: %s\n", addr); |
| 344 |
kfree(z); |
| 345 |
return -EINVAL; |
| 346 |
} |
| 347 |
} else { |
| 348 |
printk(KERN_NOTICE "TDMoE: Missing MAC address\n"); |
| 349 |
kfree(z); |
| 350 |
return -EINVAL; |
| 351 |
} |
| 352 |
if (tmp4) { |
| 353 |
int sub = 0; |
| 354 |
int mul = 1; |
| 355 |
|
| 356 |
/* We have a subaddr */ |
| 357 |
tmp3 = tmp4 + strlen (tmp4) - 1; |
| 358 |
while (tmp3 >= tmp4) { |
| 359 |
if (*tmp3 >= '0' && *tmp3 <= '9') { |
| 360 |
sub += (*tmp3 - '0') * mul; |
| 361 |
} else { |
| 362 |
printk(KERN_NOTICE "TDMoE: Invalid subaddress\n"); |
| 363 |
kfree(z); |
| 364 |
return -EINVAL; |
| 365 |
} |
| 366 |
mul *= 10; |
| 367 |
tmp3--; |
| 368 |
} |
| 369 |
z->subaddr = htons(sub); |
| 370 |
} |
| 371 |
z->dev = dev_get_by_name( |
| 372 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24) |
| 373 |
&init_net, |
| 374 |
#endif |
| 375 |
z->ethdev); |
| 376 |
if (!z->dev) { |
| 377 |
printk(KERN_NOTICE "TDMoE: Invalid device '%s'\n", z->ethdev); |
| 378 |
kfree(z); |
| 379 |
return -EINVAL; |
| 380 |
} |
| 381 |
z->span = span; |
| 382 |
src[0] ='\0'; |
| 383 |
for (x=0;x<5;x++) |
| 384 |
sprintf(src + strlen(src), "%02x:", z->dev->dev_addr[x]); |
| 385 |
sprintf(src + strlen(src), "%02x", z->dev->dev_addr[5]); |
| 386 |
printk(KERN_INFO "TDMoE: Added new interface for %s at %s (addr=%s, src=%s, subaddr=%d)\n", span->name, z->dev->name, addr, src, ntohs(z->subaddr)); |
| 387 |
|
| 388 |
spin_lock_irqsave(&zlock, flags); |
| 389 |
z->next = zdevs; |
| 390 |
zdevs = z; |
| 391 |
dyn->pvt = z; |
| 392 |
spin_unlock_irqrestore(&zlock, flags); |
| 393 |
} |
| 394 |
return (z) ? 0 : -ENOMEM; |
| 395 |
} |
| 396 |
|
| 397 |
static struct dahdi_dynamic_driver ztd_eth = { |
| 398 |
.owner = THIS_MODULE, |
| 399 |
.name = "eth", |
| 400 |
.desc = "Ethernet", |
| 401 |
.create = ztdeth_create, |
| 402 |
.destroy = ztdeth_destroy, |
| 403 |
.transmit = ztdeth_transmit, |
| 404 |
.flush = ztdeth_flush, |
| 405 |
}; |
| 406 |
|
| 407 |
static struct notifier_block ztdeth_nblock = { |
| 408 |
.notifier_call = ztdeth_notifier, |
| 409 |
}; |
| 410 |
|
| 411 |
static int __init ztdeth_init(void) |
| 412 |
{ |
| 413 |
skb_queue_head_init(&skbs); |
| 414 |
|
| 415 |
dev_add_pack(&ztdeth_ptype); |
| 416 |
register_netdevice_notifier(&ztdeth_nblock); |
| 417 |
dahdi_dynamic_register_driver(&ztd_eth); |
| 418 |
|
| 419 |
return 0; |
| 420 |
} |
| 421 |
|
| 422 |
static void __exit ztdeth_exit(void) |
| 423 |
{ |
| 424 |
dahdi_dynamic_unregister_driver(&ztd_eth); |
| 425 |
unregister_netdevice_notifier(&ztdeth_nblock); |
| 426 |
dev_remove_pack(&ztdeth_ptype); |
| 427 |
|
| 428 |
skb_queue_purge(&skbs); |
| 429 |
} |
| 430 |
|
| 431 |
MODULE_DESCRIPTION("DAHDI Dynamic TDMoE Support"); |
| 432 |
MODULE_AUTHOR("Mark Spencer <[email protected]>"); |
| 433 |
MODULE_LICENSE("GPL v2"); |
| 434 |
|
| 435 |
module_init(ztdeth_init); |
| 436 |
module_exit(ztdeth_exit); |