blob: cf3ae8b475728df8668ce90ce14f5622e65c2388 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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 Perchesc39f01d2010-01-26 11:40:01 +00007#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
9#include "signaling.h"
10#include "addr.h"
11
Mitchell Blank Jr61c33e02008-06-17 16:20:06 -070012static int check_addr(const struct sockaddr_atmsvc *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013{
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 Jr61c33e02008-06-17 16:20:06 -070026static int identical(const struct sockaddr_atmsvc *a, const struct sockaddr_atmsvc *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
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 Jr61c33e02008-06-17 16:20:06 -070038static void notify_sigd(const struct atm_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
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 Kinzie0f21ba72005-10-06 22:19:28 -070046void atm_reset_addr(struct atm_dev *dev, enum atm_addr_type_t atype)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 unsigned long flags;
49 struct atm_dev_addr *this, *p;
Eric Kinzie0f21ba72005-10-06 22:19:28 -070050 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52 spin_lock_irqsave(&dev->lock, flags);
Eric Kinzie0f21ba72005-10-06 22:19:28 -070053 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 Whitaker735631a2005-09-28 16:35:22 -070058 list_del(&this->entry);
59 kfree(this);
60 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 spin_unlock_irqrestore(&dev->lock, flags);
Eric Kinzie0f21ba72005-10-06 22:19:28 -070062 if (head == &dev->local)
63 notify_sigd(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
Mitchell Blank Jr61c33e02008-06-17 16:20:06 -070066int atm_add_addr(struct atm_dev *dev, const struct sockaddr_atmsvc *addr,
Eric Kinzie0f21ba72005-10-06 22:19:28 -070067 enum atm_addr_type_t atype)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 unsigned long flags;
70 struct atm_dev_addr *this;
Eric Kinzie0f21ba72005-10-06 22:19:28 -070071 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 int error;
73
74 error = check_addr(addr);
75 if (error)
76 return error;
77 spin_lock_irqsave(&dev->lock, flags);
Eric Kinzie0f21ba72005-10-06 22:19:28 -070078 if (atype == ATM_ADDR_LECS)
79 head = &dev->lecs;
80 else
81 head = &dev->local;
82 list_for_each_entry(this, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 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 Kinzie0f21ba72005-10-06 22:19:28 -070094 list_add(&this->entry, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 spin_unlock_irqrestore(&dev->lock, flags);
Eric Kinzie0f21ba72005-10-06 22:19:28 -070096 if (head == &dev->local)
97 notify_sigd(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return 0;
99}
100
Mitchell Blank Jr61c33e02008-06-17 16:20:06 -0700101int atm_del_addr(struct atm_dev *dev, const struct sockaddr_atmsvc *addr,
Eric Kinzie0f21ba72005-10-06 22:19:28 -0700102 enum atm_addr_type_t atype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 unsigned long flags;
105 struct atm_dev_addr *this;
Eric Kinzie0f21ba72005-10-06 22:19:28 -0700106 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int error;
108
109 error = check_addr(addr);
110 if (error)
111 return error;
112 spin_lock_irqsave(&dev->lock, flags);
Eric Kinzie0f21ba72005-10-06 22:19:28 -0700113 if (atype == ATM_ADDR_LECS)
114 head = &dev->lecs;
115 else
116 head = &dev->local;
117 list_for_each_entry(this, head, entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (identical(&this->addr, addr)) {
119 list_del(&this->entry);
120 spin_unlock_irqrestore(&dev->lock, flags);
121 kfree(this);
Eric Kinzie0f21ba72005-10-06 22:19:28 -0700122 if (head == &dev->local)
123 notify_sigd(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return 0;
125 }
126 }
127 spin_unlock_irqrestore(&dev->lock, flags);
128 return -ENOENT;
129}
130
131int atm_get_addr(struct atm_dev *dev, struct sockaddr_atmsvc __user * buf,
Eric Kinzie0f21ba72005-10-06 22:19:28 -0700132 size_t size, enum atm_addr_type_t atype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 unsigned long flags;
135 struct atm_dev_addr *this;
Eric Kinzie0f21ba72005-10-06 22:19:28 -0700136 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int total = 0, error;
138 struct sockaddr_atmsvc *tmp_buf, *tmp_bufp;
139
140 spin_lock_irqsave(&dev->lock, flags);
Eric Kinzie0f21ba72005-10-06 22:19:28 -0700141 if (atype == ATM_ADDR_LECS)
142 head = &dev->lecs;
143 else
144 head = &dev->local;
145 list_for_each_entry(this, head, entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 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 Kinzie0f21ba72005-10-06 22:19:28 -0700152 list_for_each_entry(this, head, entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 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}