blob: a30d0bf480638de8a44c18b221b007d65085047a [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>
7#include <linux/sched.h>
8#include <asm/uaccess.h>
9
10#include "signaling.h"
11#include "addr.h"
12
13static 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
27static 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
39static 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
47void 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);
Martin Whitaker735631a2005-09-28 16:35:22 -070053 list_for_each_entry_safe(this, p, &dev->local, entry) {
54 list_del(&this->entry);
55 kfree(this);
56 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 spin_unlock_irqrestore(&dev->lock, flags);
58 notify_sigd(dev);
59}
60
61int atm_add_addr(struct atm_dev *dev, struct sockaddr_atmsvc *addr)
62{
63 unsigned long flags;
64 struct atm_dev_addr *this;
65 int error;
66
67 error = check_addr(addr);
68 if (error)
69 return error;
70 spin_lock_irqsave(&dev->lock, flags);
71 list_for_each_entry(this, &dev->local, entry) {
72 if (identical(&this->addr, addr)) {
73 spin_unlock_irqrestore(&dev->lock, flags);
74 return -EEXIST;
75 }
76 }
77 this = kmalloc(sizeof(struct atm_dev_addr), GFP_ATOMIC);
78 if (!this) {
79 spin_unlock_irqrestore(&dev->lock, flags);
80 return -ENOMEM;
81 }
82 this->addr = *addr;
83 list_add(&this->entry, &dev->local);
84 spin_unlock_irqrestore(&dev->lock, flags);
85 notify_sigd(dev);
86 return 0;
87}
88
89int atm_del_addr(struct atm_dev *dev, struct sockaddr_atmsvc *addr)
90{
91 unsigned long flags;
92 struct atm_dev_addr *this;
93 int error;
94
95 error = check_addr(addr);
96 if (error)
97 return error;
98 spin_lock_irqsave(&dev->lock, flags);
99 list_for_each_entry(this, &dev->local, entry) {
100 if (identical(&this->addr, addr)) {
101 list_del(&this->entry);
102 spin_unlock_irqrestore(&dev->lock, flags);
103 kfree(this);
104 notify_sigd(dev);
105 return 0;
106 }
107 }
108 spin_unlock_irqrestore(&dev->lock, flags);
109 return -ENOENT;
110}
111
112int atm_get_addr(struct atm_dev *dev, struct sockaddr_atmsvc __user * buf,
113 size_t size)
114{
115 unsigned long flags;
116 struct atm_dev_addr *this;
117 int total = 0, error;
118 struct sockaddr_atmsvc *tmp_buf, *tmp_bufp;
119
120 spin_lock_irqsave(&dev->lock, flags);
121 list_for_each_entry(this, &dev->local, entry)
122 total += sizeof(struct sockaddr_atmsvc);
123 tmp_buf = tmp_bufp = kmalloc(total, GFP_ATOMIC);
124 if (!tmp_buf) {
125 spin_unlock_irqrestore(&dev->lock, flags);
126 return -ENOMEM;
127 }
128 list_for_each_entry(this, &dev->local, entry)
129 memcpy(tmp_bufp++, &this->addr, sizeof(struct sockaddr_atmsvc));
130 spin_unlock_irqrestore(&dev->lock, flags);
131 error = total > size ? -E2BIG : total;
132 if (copy_to_user(buf, tmp_buf, total < size ? total : size))
133 error = -EFAULT;
134 kfree(tmp_buf);
135 return error;
136}