Blame view

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