xref: /openbmc/linux/net/atm/addr.c (revision 5a0e3ad6)
11da177e4SLinus Torvalds /* net/atm/addr.c - Local ATM address registry */
21da177e4SLinus Torvalds 
31da177e4SLinus Torvalds /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #include <linux/atm.h>
61da177e4SLinus Torvalds #include <linux/atmdev.h>
75a0e3ad6STejun Heo #include <linux/slab.h>
8c39f01d7SJoe Perches #include <linux/uaccess.h>
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include "signaling.h"
111da177e4SLinus Torvalds #include "addr.h"
121da177e4SLinus Torvalds 
1361c33e01SMitchell Blank Jr static int check_addr(const struct sockaddr_atmsvc *addr)
141da177e4SLinus Torvalds {
151da177e4SLinus Torvalds 	int i;
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds 	if (addr->sas_family != AF_ATMSVC)
181da177e4SLinus Torvalds 		return -EAFNOSUPPORT;
191da177e4SLinus Torvalds 	if (!*addr->sas_addr.pub)
201da177e4SLinus Torvalds 		return *addr->sas_addr.prv ? 0 : -EINVAL;
211da177e4SLinus Torvalds 	for (i = 1; i < ATM_E164_LEN + 1; i++)	/* make sure it's \0-terminated */
221da177e4SLinus Torvalds 		if (!addr->sas_addr.pub[i])
231da177e4SLinus Torvalds 			return 0;
241da177e4SLinus Torvalds 	return -EINVAL;
251da177e4SLinus Torvalds }
261da177e4SLinus Torvalds 
2761c33e01SMitchell Blank Jr static int identical(const struct sockaddr_atmsvc *a, const struct sockaddr_atmsvc *b)
281da177e4SLinus Torvalds {
291da177e4SLinus Torvalds 	if (*a->sas_addr.prv)
301da177e4SLinus Torvalds 		if (memcmp(a->sas_addr.prv, b->sas_addr.prv, ATM_ESA_LEN))
311da177e4SLinus Torvalds 			return 0;
321da177e4SLinus Torvalds 	if (!*a->sas_addr.pub)
331da177e4SLinus Torvalds 		return !*b->sas_addr.pub;
341da177e4SLinus Torvalds 	if (!*b->sas_addr.pub)
351da177e4SLinus Torvalds 		return 0;
361da177e4SLinus Torvalds 	return !strcmp(a->sas_addr.pub, b->sas_addr.pub);
371da177e4SLinus Torvalds }
381da177e4SLinus Torvalds 
3961c33e01SMitchell Blank Jr static void notify_sigd(const struct atm_dev *dev)
401da177e4SLinus Torvalds {
411da177e4SLinus Torvalds 	struct sockaddr_atmpvc pvc;
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 	pvc.sap_addr.itf = dev->number;
441da177e4SLinus Torvalds 	sigd_enq(NULL, as_itf_notify, NULL, &pvc, NULL);
451da177e4SLinus Torvalds }
461da177e4SLinus Torvalds 
470f21ba7cSEric Kinzie void atm_reset_addr(struct atm_dev *dev, enum atm_addr_type_t atype)
481da177e4SLinus Torvalds {
491da177e4SLinus Torvalds 	unsigned long flags;
501da177e4SLinus Torvalds 	struct atm_dev_addr *this, *p;
510f21ba7cSEric Kinzie 	struct list_head *head;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds 	spin_lock_irqsave(&dev->lock, flags);
540f21ba7cSEric Kinzie 	if (atype == ATM_ADDR_LECS)
550f21ba7cSEric Kinzie 		head = &dev->lecs;
560f21ba7cSEric Kinzie 	else
570f21ba7cSEric Kinzie 		head = &dev->local;
580f21ba7cSEric Kinzie 	list_for_each_entry_safe(this, p, head, entry) {
59735631a9SMartin Whitaker 		list_del(&this->entry);
601da177e4SLinus Torvalds 		kfree(this);
61735631a9SMartin Whitaker 	}
621da177e4SLinus Torvalds 	spin_unlock_irqrestore(&dev->lock, flags);
630f21ba7cSEric Kinzie 	if (head == &dev->local)
641da177e4SLinus Torvalds 		notify_sigd(dev);
651da177e4SLinus Torvalds }
661da177e4SLinus Torvalds 
6761c33e01SMitchell Blank Jr int atm_add_addr(struct atm_dev *dev, const struct sockaddr_atmsvc *addr,
680f21ba7cSEric Kinzie 		 enum atm_addr_type_t atype)
691da177e4SLinus Torvalds {
701da177e4SLinus Torvalds 	unsigned long flags;
711da177e4SLinus Torvalds 	struct atm_dev_addr *this;
720f21ba7cSEric Kinzie 	struct list_head *head;
731da177e4SLinus Torvalds 	int error;
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	error = check_addr(addr);
761da177e4SLinus Torvalds 	if (error)
771da177e4SLinus Torvalds 		return error;
781da177e4SLinus Torvalds 	spin_lock_irqsave(&dev->lock, flags);
790f21ba7cSEric Kinzie 	if (atype == ATM_ADDR_LECS)
800f21ba7cSEric Kinzie 		head = &dev->lecs;
810f21ba7cSEric Kinzie 	else
820f21ba7cSEric Kinzie 		head = &dev->local;
830f21ba7cSEric Kinzie 	list_for_each_entry(this, head, entry) {
841da177e4SLinus Torvalds 		if (identical(&this->addr, addr)) {
851da177e4SLinus Torvalds 			spin_unlock_irqrestore(&dev->lock, flags);
861da177e4SLinus Torvalds 			return -EEXIST;
871da177e4SLinus Torvalds 		}
881da177e4SLinus Torvalds 	}
891da177e4SLinus Torvalds 	this = kmalloc(sizeof(struct atm_dev_addr), GFP_ATOMIC);
901da177e4SLinus Torvalds 	if (!this) {
911da177e4SLinus Torvalds 		spin_unlock_irqrestore(&dev->lock, flags);
921da177e4SLinus Torvalds 		return -ENOMEM;
931da177e4SLinus Torvalds 	}
941da177e4SLinus Torvalds 	this->addr = *addr;
950f21ba7cSEric Kinzie 	list_add(&this->entry, head);
961da177e4SLinus Torvalds 	spin_unlock_irqrestore(&dev->lock, flags);
970f21ba7cSEric Kinzie 	if (head == &dev->local)
981da177e4SLinus Torvalds 		notify_sigd(dev);
991da177e4SLinus Torvalds 	return 0;
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
10261c33e01SMitchell Blank Jr int atm_del_addr(struct atm_dev *dev, const struct sockaddr_atmsvc *addr,
1030f21ba7cSEric Kinzie 		 enum atm_addr_type_t atype)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	unsigned long flags;
1061da177e4SLinus Torvalds 	struct atm_dev_addr *this;
1070f21ba7cSEric Kinzie 	struct list_head *head;
1081da177e4SLinus Torvalds 	int error;
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	error = check_addr(addr);
1111da177e4SLinus Torvalds 	if (error)
1121da177e4SLinus Torvalds 		return error;
1131da177e4SLinus Torvalds 	spin_lock_irqsave(&dev->lock, flags);
1140f21ba7cSEric Kinzie 	if (atype == ATM_ADDR_LECS)
1150f21ba7cSEric Kinzie 		head = &dev->lecs;
1160f21ba7cSEric Kinzie 	else
1170f21ba7cSEric Kinzie 		head = &dev->local;
1180f21ba7cSEric Kinzie 	list_for_each_entry(this, head, entry) {
1191da177e4SLinus Torvalds 		if (identical(&this->addr, addr)) {
1201da177e4SLinus Torvalds 			list_del(&this->entry);
1211da177e4SLinus Torvalds 			spin_unlock_irqrestore(&dev->lock, flags);
1221da177e4SLinus Torvalds 			kfree(this);
1230f21ba7cSEric Kinzie 			if (head == &dev->local)
1241da177e4SLinus Torvalds 				notify_sigd(dev);
1251da177e4SLinus Torvalds 			return 0;
1261da177e4SLinus Torvalds 		}
1271da177e4SLinus Torvalds 	}
1281da177e4SLinus Torvalds 	spin_unlock_irqrestore(&dev->lock, flags);
1291da177e4SLinus Torvalds 	return -ENOENT;
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds int atm_get_addr(struct atm_dev *dev, struct sockaddr_atmsvc __user * buf,
1330f21ba7cSEric Kinzie 		 size_t size, enum atm_addr_type_t atype)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	unsigned long flags;
1361da177e4SLinus Torvalds 	struct atm_dev_addr *this;
1370f21ba7cSEric Kinzie 	struct list_head *head;
1381da177e4SLinus Torvalds 	int total = 0, error;
1391da177e4SLinus Torvalds 	struct sockaddr_atmsvc *tmp_buf, *tmp_bufp;
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	spin_lock_irqsave(&dev->lock, flags);
1420f21ba7cSEric Kinzie 	if (atype == ATM_ADDR_LECS)
1430f21ba7cSEric Kinzie 		head = &dev->lecs;
1440f21ba7cSEric Kinzie 	else
1450f21ba7cSEric Kinzie 		head = &dev->local;
1460f21ba7cSEric Kinzie 	list_for_each_entry(this, head, entry)
1471da177e4SLinus Torvalds 	    total += sizeof(struct sockaddr_atmsvc);
1481da177e4SLinus Torvalds 	tmp_buf = tmp_bufp = kmalloc(total, GFP_ATOMIC);
1491da177e4SLinus Torvalds 	if (!tmp_buf) {
1501da177e4SLinus Torvalds 		spin_unlock_irqrestore(&dev->lock, flags);
1511da177e4SLinus Torvalds 		return -ENOMEM;
1521da177e4SLinus Torvalds 	}
1530f21ba7cSEric Kinzie 	list_for_each_entry(this, head, entry)
1541da177e4SLinus Torvalds 	    memcpy(tmp_bufp++, &this->addr, sizeof(struct sockaddr_atmsvc));
1551da177e4SLinus Torvalds 	spin_unlock_irqrestore(&dev->lock, flags);
1561da177e4SLinus Torvalds 	error = total > size ? -E2BIG : total;
1571da177e4SLinus Torvalds 	if (copy_to_user(buf, tmp_buf, total < size ? total : size))
1581da177e4SLinus Torvalds 		error = -EFAULT;
1591da177e4SLinus Torvalds 	kfree(tmp_buf);
1601da177e4SLinus Torvalds 	return error;
1611da177e4SLinus Torvalds }
162