Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame^] | 1 | /* net/atm/addr.c - Local ATM address registry */ |
| 2 | |
| 3 | /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */ |
| 4 | |
| 5 | #include <linux/atm.h> |
| 6 | #include <linux/atmdev.h> |
| 7 | #include <linux/sched.h> |
| 8 | #include <asm/uaccess.h> |
| 9 | |
| 10 | #include "signaling.h" |
| 11 | #include "addr.h" |
| 12 | |
| 13 | static int check_addr(struct sockaddr_atmsvc *addr) |
| 14 | { |
| 15 | int i; |
| 16 | |
| 17 | if (addr->sas_family != AF_ATMSVC) |
| 18 | return -EAFNOSUPPORT; |
| 19 | if (!*addr->sas_addr.pub) |
| 20 | return *addr->sas_addr.prv ? 0 : -EINVAL; |
| 21 | for (i = 1; i < ATM_E164_LEN + 1; i++) /* make sure it's \0-terminated */ |
| 22 | if (!addr->sas_addr.pub[i]) |
| 23 | return 0; |
| 24 | return -EINVAL; |
| 25 | } |
| 26 | |
| 27 | static int identical(struct sockaddr_atmsvc *a, struct sockaddr_atmsvc *b) |
| 28 | { |
| 29 | if (*a->sas_addr.prv) |
| 30 | if (memcmp(a->sas_addr.prv, b->sas_addr.prv, ATM_ESA_LEN)) |
| 31 | return 0; |
| 32 | if (!*a->sas_addr.pub) |
| 33 | return !*b->sas_addr.pub; |
| 34 | if (!*b->sas_addr.pub) |
| 35 | return 0; |
| 36 | return !strcmp(a->sas_addr.pub, b->sas_addr.pub); |
| 37 | } |
| 38 | |
| 39 | static void notify_sigd(struct atm_dev *dev) |
| 40 | { |
| 41 | struct sockaddr_atmpvc pvc; |
| 42 | |
| 43 | pvc.sap_addr.itf = dev->number; |
| 44 | sigd_enq(NULL, as_itf_notify, NULL, &pvc, NULL); |
| 45 | } |
| 46 | |
| 47 | void atm_reset_addr(struct atm_dev *dev) |
| 48 | { |
| 49 | unsigned long flags; |
| 50 | struct atm_dev_addr *this, *p; |
| 51 | |
| 52 | spin_lock_irqsave(&dev->lock, flags); |
| 53 | list_for_each_entry_safe(this, p, &dev->local, entry) |
| 54 | kfree(this); |
| 55 | spin_unlock_irqrestore(&dev->lock, flags); |
| 56 | notify_sigd(dev); |
| 57 | } |
| 58 | |
| 59 | int atm_add_addr(struct atm_dev *dev, struct sockaddr_atmsvc *addr) |
| 60 | { |
| 61 | unsigned long flags; |
| 62 | struct atm_dev_addr *this; |
| 63 | int error; |
| 64 | |
| 65 | error = check_addr(addr); |
| 66 | if (error) |
| 67 | return error; |
| 68 | spin_lock_irqsave(&dev->lock, flags); |
| 69 | list_for_each_entry(this, &dev->local, entry) { |
| 70 | if (identical(&this->addr, addr)) { |
| 71 | spin_unlock_irqrestore(&dev->lock, flags); |
| 72 | return -EEXIST; |
| 73 | } |
| 74 | } |
| 75 | this = kmalloc(sizeof(struct atm_dev_addr), GFP_ATOMIC); |
| 76 | if (!this) { |
| 77 | spin_unlock_irqrestore(&dev->lock, flags); |
| 78 | return -ENOMEM; |
| 79 | } |
| 80 | this->addr = *addr; |
| 81 | list_add(&this->entry, &dev->local); |
| 82 | spin_unlock_irqrestore(&dev->lock, flags); |
| 83 | notify_sigd(dev); |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | int atm_del_addr(struct atm_dev *dev, struct sockaddr_atmsvc *addr) |
| 88 | { |
| 89 | unsigned long flags; |
| 90 | struct atm_dev_addr *this; |
| 91 | int error; |
| 92 | |
| 93 | error = check_addr(addr); |
| 94 | if (error) |
| 95 | return error; |
| 96 | spin_lock_irqsave(&dev->lock, flags); |
| 97 | list_for_each_entry(this, &dev->local, entry) { |
| 98 | if (identical(&this->addr, addr)) { |
| 99 | list_del(&this->entry); |
| 100 | spin_unlock_irqrestore(&dev->lock, flags); |
| 101 | kfree(this); |
| 102 | notify_sigd(dev); |
| 103 | return 0; |
| 104 | } |
| 105 | } |
| 106 | spin_unlock_irqrestore(&dev->lock, flags); |
| 107 | return -ENOENT; |
| 108 | } |
| 109 | |
| 110 | int atm_get_addr(struct atm_dev *dev, struct sockaddr_atmsvc __user * buf, |
| 111 | size_t size) |
| 112 | { |
| 113 | unsigned long flags; |
| 114 | struct atm_dev_addr *this; |
| 115 | int total = 0, error; |
| 116 | struct sockaddr_atmsvc *tmp_buf, *tmp_bufp; |
| 117 | |
| 118 | spin_lock_irqsave(&dev->lock, flags); |
| 119 | list_for_each_entry(this, &dev->local, entry) |
| 120 | total += sizeof(struct sockaddr_atmsvc); |
| 121 | tmp_buf = tmp_bufp = kmalloc(total, GFP_ATOMIC); |
| 122 | if (!tmp_buf) { |
| 123 | spin_unlock_irqrestore(&dev->lock, flags); |
| 124 | return -ENOMEM; |
| 125 | } |
| 126 | list_for_each_entry(this, &dev->local, entry) |
| 127 | memcpy(tmp_bufp++, &this->addr, sizeof(struct sockaddr_atmsvc)); |
| 128 | spin_unlock_irqrestore(&dev->lock, flags); |
| 129 | error = total > size ? -E2BIG : total; |
| 130 | if (copy_to_user(buf, tmp_buf, total < size ? total : size)) |
| 131 | error = -EFAULT; |
| 132 | kfree(tmp_buf); |
| 133 | return error; |
| 134 | } |