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> |
Joe Perches | c39f01d | 2010-01-26 11:40:01 +0000 | [diff] [blame] | 7 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | |
| 9 | #include "signaling.h" |
| 10 | #include "addr.h" |
| 11 | |
Mitchell Blank Jr | 61c33e0 | 2008-06-17 16:20:06 -0700 | [diff] [blame] | 12 | static int check_addr(const struct sockaddr_atmsvc *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | { |
| 14 | int i; |
| 15 | |
| 16 | if (addr->sas_family != AF_ATMSVC) |
| 17 | return -EAFNOSUPPORT; |
| 18 | if (!*addr->sas_addr.pub) |
| 19 | return *addr->sas_addr.prv ? 0 : -EINVAL; |
| 20 | for (i = 1; i < ATM_E164_LEN + 1; i++) /* make sure it's \0-terminated */ |
| 21 | if (!addr->sas_addr.pub[i]) |
| 22 | return 0; |
| 23 | return -EINVAL; |
| 24 | } |
| 25 | |
Mitchell Blank Jr | 61c33e0 | 2008-06-17 16:20:06 -0700 | [diff] [blame] | 26 | static int identical(const struct sockaddr_atmsvc *a, const struct sockaddr_atmsvc *b) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
| 28 | if (*a->sas_addr.prv) |
| 29 | if (memcmp(a->sas_addr.prv, b->sas_addr.prv, ATM_ESA_LEN)) |
| 30 | return 0; |
| 31 | if (!*a->sas_addr.pub) |
| 32 | return !*b->sas_addr.pub; |
| 33 | if (!*b->sas_addr.pub) |
| 34 | return 0; |
| 35 | return !strcmp(a->sas_addr.pub, b->sas_addr.pub); |
| 36 | } |
| 37 | |
Mitchell Blank Jr | 61c33e0 | 2008-06-17 16:20:06 -0700 | [diff] [blame] | 38 | static void notify_sigd(const struct atm_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | { |
| 40 | struct sockaddr_atmpvc pvc; |
| 41 | |
| 42 | pvc.sap_addr.itf = dev->number; |
| 43 | sigd_enq(NULL, as_itf_notify, NULL, &pvc, NULL); |
| 44 | } |
| 45 | |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 46 | void atm_reset_addr(struct atm_dev *dev, enum atm_addr_type_t atype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
| 48 | unsigned long flags; |
| 49 | struct atm_dev_addr *this, *p; |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 50 | struct list_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | spin_lock_irqsave(&dev->lock, flags); |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 53 | if (atype == ATM_ADDR_LECS) |
| 54 | head = &dev->lecs; |
| 55 | else |
| 56 | head = &dev->local; |
| 57 | list_for_each_entry_safe(this, p, head, entry) { |
Martin Whitaker | 735631a | 2005-09-28 16:35:22 -0700 | [diff] [blame] | 58 | list_del(&this->entry); |
| 59 | kfree(this); |
| 60 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | spin_unlock_irqrestore(&dev->lock, flags); |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 62 | if (head == &dev->local) |
| 63 | notify_sigd(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Mitchell Blank Jr | 61c33e0 | 2008-06-17 16:20:06 -0700 | [diff] [blame] | 66 | int atm_add_addr(struct atm_dev *dev, const struct sockaddr_atmsvc *addr, |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 67 | enum atm_addr_type_t atype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
| 69 | unsigned long flags; |
| 70 | struct atm_dev_addr *this; |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 71 | struct list_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | int error; |
| 73 | |
| 74 | error = check_addr(addr); |
| 75 | if (error) |
| 76 | return error; |
| 77 | spin_lock_irqsave(&dev->lock, flags); |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 78 | if (atype == ATM_ADDR_LECS) |
| 79 | head = &dev->lecs; |
| 80 | else |
| 81 | head = &dev->local; |
| 82 | list_for_each_entry(this, head, entry) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | if (identical(&this->addr, addr)) { |
| 84 | spin_unlock_irqrestore(&dev->lock, flags); |
| 85 | return -EEXIST; |
| 86 | } |
| 87 | } |
| 88 | this = kmalloc(sizeof(struct atm_dev_addr), GFP_ATOMIC); |
| 89 | if (!this) { |
| 90 | spin_unlock_irqrestore(&dev->lock, flags); |
| 91 | return -ENOMEM; |
| 92 | } |
| 93 | this->addr = *addr; |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 94 | list_add(&this->entry, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | spin_unlock_irqrestore(&dev->lock, flags); |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 96 | if (head == &dev->local) |
| 97 | notify_sigd(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
Mitchell Blank Jr | 61c33e0 | 2008-06-17 16:20:06 -0700 | [diff] [blame] | 101 | int atm_del_addr(struct atm_dev *dev, const struct sockaddr_atmsvc *addr, |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 102 | enum atm_addr_type_t atype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
| 104 | unsigned long flags; |
| 105 | struct atm_dev_addr *this; |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 106 | struct list_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | int error; |
| 108 | |
| 109 | error = check_addr(addr); |
| 110 | if (error) |
| 111 | return error; |
| 112 | spin_lock_irqsave(&dev->lock, flags); |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 113 | if (atype == ATM_ADDR_LECS) |
| 114 | head = &dev->lecs; |
| 115 | else |
| 116 | head = &dev->local; |
| 117 | list_for_each_entry(this, head, entry) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | if (identical(&this->addr, addr)) { |
| 119 | list_del(&this->entry); |
| 120 | spin_unlock_irqrestore(&dev->lock, flags); |
| 121 | kfree(this); |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 122 | if (head == &dev->local) |
| 123 | notify_sigd(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | } |
| 127 | spin_unlock_irqrestore(&dev->lock, flags); |
| 128 | return -ENOENT; |
| 129 | } |
| 130 | |
| 131 | int atm_get_addr(struct atm_dev *dev, struct sockaddr_atmsvc __user * buf, |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 132 | size_t size, enum atm_addr_type_t atype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
| 134 | unsigned long flags; |
| 135 | struct atm_dev_addr *this; |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 136 | struct list_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | int total = 0, error; |
| 138 | struct sockaddr_atmsvc *tmp_buf, *tmp_bufp; |
| 139 | |
| 140 | spin_lock_irqsave(&dev->lock, flags); |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 141 | if (atype == ATM_ADDR_LECS) |
| 142 | head = &dev->lecs; |
| 143 | else |
| 144 | head = &dev->local; |
| 145 | list_for_each_entry(this, head, entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | total += sizeof(struct sockaddr_atmsvc); |
| 147 | tmp_buf = tmp_bufp = kmalloc(total, GFP_ATOMIC); |
| 148 | if (!tmp_buf) { |
| 149 | spin_unlock_irqrestore(&dev->lock, flags); |
| 150 | return -ENOMEM; |
| 151 | } |
Eric Kinzie | 0f21ba7 | 2005-10-06 22:19:28 -0700 | [diff] [blame] | 152 | list_for_each_entry(this, head, entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | memcpy(tmp_bufp++, &this->addr, sizeof(struct sockaddr_atmsvc)); |
| 154 | spin_unlock_irqrestore(&dev->lock, flags); |
| 155 | error = total > size ? -E2BIG : total; |
| 156 | if (copy_to_user(buf, tmp_buf, total < size ? total : size)) |
| 157 | error = -EFAULT; |
| 158 | kfree(tmp_buf); |
| 159 | return error; |
| 160 | } |