Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * DECnet An implementation of the DECnet protocol suite for the LINUX |
| 3 | * operating system. DECnet is implemented using the BSD Socket |
| 4 | * interface as the means of communication with the user level. |
| 5 | * |
| 6 | * DECnet Device Layer |
| 7 | * |
| 8 | * Authors: Steve Whitehouse <SteveW@ACM.org> |
| 9 | * Eduardo Marcelo Serrat <emserrat@geocities.com> |
| 10 | * |
| 11 | * Changes: |
| 12 | * Steve Whitehouse : Devices now see incoming frames so they |
| 13 | * can mark on who it came from. |
| 14 | * Steve Whitehouse : Fixed bug in creating neighbours. Each neighbour |
| 15 | * can now have a device specific setup func. |
| 16 | * Steve Whitehouse : Added /proc/sys/net/decnet/conf/<dev>/ |
| 17 | * Steve Whitehouse : Fixed bug which sometimes killed timer |
| 18 | * Steve Whitehouse : Multiple ifaddr support |
| 19 | * Steve Whitehouse : SIOCGIFCONF is now a compile time option |
| 20 | * Steve Whitehouse : /proc/sys/net/decnet/conf/<sys>/forwarding |
| 21 | * Steve Whitehouse : Removed timer1 - it's a user space issue now |
| 22 | * Patrick Caulfield : Fixed router hello message format |
| 23 | * Steve Whitehouse : Got rid of constant sizes for blksize for |
| 24 | * devices. All mtu based now. |
| 25 | */ |
| 26 | |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 27 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/moduleparam.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/net.h> |
| 32 | #include <linux/netdevice.h> |
| 33 | #include <linux/proc_fs.h> |
| 34 | #include <linux/seq_file.h> |
| 35 | #include <linux/timer.h> |
| 36 | #include <linux/string.h> |
Thomas Graf | 1823730 | 2006-08-04 23:04:54 -0700 | [diff] [blame] | 37 | #include <linux/if_addr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/if_arp.h> |
| 39 | #include <linux/if_ether.h> |
| 40 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/sysctl.h> |
| 42 | #include <linux/notifier.h> |
| 43 | #include <asm/uaccess.h> |
| 44 | #include <asm/system.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 45 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <net/neighbour.h> |
| 47 | #include <net/dst.h> |
| 48 | #include <net/flow.h> |
Steven Whitehouse | a8731cb | 2006-08-09 15:56:46 -0700 | [diff] [blame] | 49 | #include <net/fib_rules.h> |
Thomas Graf | a6f01ca | 2006-11-24 17:14:07 -0800 | [diff] [blame] | 50 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <net/dn.h> |
| 52 | #include <net/dn_dev.h> |
| 53 | #include <net/dn_route.h> |
| 54 | #include <net/dn_neigh.h> |
| 55 | #include <net/dn_fib.h> |
| 56 | |
| 57 | #define DN_IFREQ_SIZE (sizeof(struct ifreq) - sizeof(struct sockaddr) + sizeof(struct sockaddr_dn)) |
| 58 | |
| 59 | static char dn_rt_all_end_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x04,0x00,0x00}; |
| 60 | static char dn_rt_all_rt_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x03,0x00,0x00}; |
| 61 | static char dn_hiord[ETH_ALEN] = {0xAA,0x00,0x04,0x00,0x00,0x00}; |
| 62 | static unsigned char dn_eco_version[3] = {0x02,0x00,0x00}; |
| 63 | |
| 64 | extern struct neigh_table dn_neigh_table; |
| 65 | |
| 66 | /* |
| 67 | * decnet_address is kept in network order. |
| 68 | */ |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 69 | __le16 decnet_address = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | static DEFINE_RWLOCK(dndev_lock); |
| 72 | static struct net_device *decnet_default_device; |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 73 | static BLOCKING_NOTIFIER_HEAD(dnaddr_chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
| 75 | static struct dn_dev *dn_dev_create(struct net_device *dev, int *err); |
| 76 | static void dn_dev_delete(struct net_device *dev); |
Thomas Graf | b020b94 | 2006-11-24 17:14:31 -0800 | [diff] [blame] | 77 | static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | static int dn_eth_up(struct net_device *); |
| 80 | static void dn_eth_down(struct net_device *); |
| 81 | static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa); |
| 82 | static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa); |
| 83 | |
| 84 | static struct dn_dev_parms dn_dev_list[] = { |
| 85 | { |
| 86 | .type = ARPHRD_ETHER, /* Ethernet */ |
| 87 | .mode = DN_DEV_BCAST, |
| 88 | .state = DN_DEV_S_RU, |
| 89 | .t2 = 1, |
| 90 | .t3 = 10, |
| 91 | .name = "ethernet", |
| 92 | .ctl_name = NET_DECNET_CONF_ETHER, |
| 93 | .up = dn_eth_up, |
| 94 | .down = dn_eth_down, |
| 95 | .timer3 = dn_send_brd_hello, |
| 96 | }, |
| 97 | { |
| 98 | .type = ARPHRD_IPGRE, /* DECnet tunneled over GRE in IP */ |
| 99 | .mode = DN_DEV_BCAST, |
| 100 | .state = DN_DEV_S_RU, |
| 101 | .t2 = 1, |
| 102 | .t3 = 10, |
| 103 | .name = "ipgre", |
| 104 | .ctl_name = NET_DECNET_CONF_GRE, |
| 105 | .timer3 = dn_send_brd_hello, |
| 106 | }, |
| 107 | #if 0 |
| 108 | { |
| 109 | .type = ARPHRD_X25, /* Bog standard X.25 */ |
| 110 | .mode = DN_DEV_UCAST, |
| 111 | .state = DN_DEV_S_DS, |
| 112 | .t2 = 1, |
| 113 | .t3 = 120, |
| 114 | .name = "x25", |
| 115 | .ctl_name = NET_DECNET_CONF_X25, |
| 116 | .timer3 = dn_send_ptp_hello, |
| 117 | }, |
| 118 | #endif |
| 119 | #if 0 |
| 120 | { |
| 121 | .type = ARPHRD_PPP, /* DECnet over PPP */ |
| 122 | .mode = DN_DEV_BCAST, |
| 123 | .state = DN_DEV_S_RU, |
| 124 | .t2 = 1, |
| 125 | .t3 = 10, |
| 126 | .name = "ppp", |
| 127 | .ctl_name = NET_DECNET_CONF_PPP, |
| 128 | .timer3 = dn_send_brd_hello, |
| 129 | }, |
| 130 | #endif |
| 131 | { |
| 132 | .type = ARPHRD_DDCMP, /* DECnet over DDCMP */ |
| 133 | .mode = DN_DEV_UCAST, |
| 134 | .state = DN_DEV_S_DS, |
| 135 | .t2 = 1, |
| 136 | .t3 = 120, |
| 137 | .name = "ddcmp", |
| 138 | .ctl_name = NET_DECNET_CONF_DDCMP, |
| 139 | .timer3 = dn_send_ptp_hello, |
| 140 | }, |
| 141 | { |
| 142 | .type = ARPHRD_LOOPBACK, /* Loopback interface - always last */ |
| 143 | .mode = DN_DEV_BCAST, |
| 144 | .state = DN_DEV_S_RU, |
| 145 | .t2 = 1, |
| 146 | .t3 = 10, |
| 147 | .name = "loopback", |
| 148 | .ctl_name = NET_DECNET_CONF_LOOPBACK, |
| 149 | .timer3 = dn_send_brd_hello, |
| 150 | } |
| 151 | }; |
| 152 | |
Denis Cheng | 8b14a53 | 2007-09-16 16:41:29 -0700 | [diff] [blame] | 153 | #define DN_DEV_LIST_SIZE ARRAY_SIZE(dn_dev_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | #define DN_DEV_PARMS_OFFSET(x) ((int) ((char *) &((struct dn_dev_parms *)0)->x)) |
| 156 | |
| 157 | #ifdef CONFIG_SYSCTL |
| 158 | |
| 159 | static int min_t2[] = { 1 }; |
| 160 | static int max_t2[] = { 60 }; /* No max specified, but this seems sensible */ |
| 161 | static int min_t3[] = { 1 }; |
| 162 | static int max_t3[] = { 8191 }; /* Must fit in 16 bits when multiplied by BCT3MULT or T3MULT */ |
| 163 | |
| 164 | static int min_priority[1]; |
| 165 | static int max_priority[] = { 127 }; /* From DECnet spec */ |
| 166 | |
| 167 | static int dn_forwarding_proc(ctl_table *, int, struct file *, |
| 168 | void __user *, size_t *, loff_t *); |
| 169 | static int dn_forwarding_sysctl(ctl_table *table, int __user *name, int nlen, |
| 170 | void __user *oldval, size_t __user *oldlenp, |
Alexey Dobriyan | 1f29bcd | 2006-12-10 02:19:10 -0800 | [diff] [blame] | 171 | void __user *newval, size_t newlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | static struct dn_dev_sysctl_table { |
| 174 | struct ctl_table_header *sysctl_header; |
| 175 | ctl_table dn_dev_vars[5]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } dn_dev_sysctl = { |
| 177 | NULL, |
| 178 | { |
| 179 | { |
| 180 | .ctl_name = NET_DECNET_CONF_DEV_FORWARDING, |
| 181 | .procname = "forwarding", |
| 182 | .data = (void *)DN_DEV_PARMS_OFFSET(forwarding), |
| 183 | .maxlen = sizeof(int), |
| 184 | .mode = 0644, |
| 185 | .proc_handler = dn_forwarding_proc, |
| 186 | .strategy = dn_forwarding_sysctl, |
| 187 | }, |
| 188 | { |
| 189 | .ctl_name = NET_DECNET_CONF_DEV_PRIORITY, |
| 190 | .procname = "priority", |
| 191 | .data = (void *)DN_DEV_PARMS_OFFSET(priority), |
| 192 | .maxlen = sizeof(int), |
| 193 | .mode = 0644, |
| 194 | .proc_handler = proc_dointvec_minmax, |
| 195 | .strategy = sysctl_intvec, |
| 196 | .extra1 = &min_priority, |
| 197 | .extra2 = &max_priority |
| 198 | }, |
| 199 | { |
| 200 | .ctl_name = NET_DECNET_CONF_DEV_T2, |
| 201 | .procname = "t2", |
| 202 | .data = (void *)DN_DEV_PARMS_OFFSET(t2), |
| 203 | .maxlen = sizeof(int), |
| 204 | .mode = 0644, |
| 205 | .proc_handler = proc_dointvec_minmax, |
| 206 | .strategy = sysctl_intvec, |
| 207 | .extra1 = &min_t2, |
| 208 | .extra2 = &max_t2 |
| 209 | }, |
| 210 | { |
| 211 | .ctl_name = NET_DECNET_CONF_DEV_T3, |
| 212 | .procname = "t3", |
| 213 | .data = (void *)DN_DEV_PARMS_OFFSET(t3), |
| 214 | .maxlen = sizeof(int), |
| 215 | .mode = 0644, |
| 216 | .proc_handler = proc_dointvec_minmax, |
| 217 | .strategy = sysctl_intvec, |
| 218 | .extra1 = &min_t3, |
| 219 | .extra2 = &max_t3 |
| 220 | }, |
| 221 | {0} |
| 222 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms) |
| 226 | { |
| 227 | struct dn_dev_sysctl_table *t; |
| 228 | int i; |
| 229 | |
Pavel Emelyanov | 3151a9a | 2008-01-09 00:31:49 -0800 | [diff] [blame] | 230 | #define DN_CTL_PATH_DEV 3 |
| 231 | |
| 232 | struct ctl_path dn_ctl_path[] = { |
| 233 | { .procname = "net", .ctl_name = CTL_NET, }, |
| 234 | { .procname = "decnet", .ctl_name = NET_DECNET, }, |
| 235 | { .procname = "conf", .ctl_name = NET_DECNET_CONF, }, |
| 236 | { /* to be set */ }, |
| 237 | { }, |
| 238 | }; |
| 239 | |
Arnaldo Carvalho de Melo | c66b721 | 2006-11-17 12:29:21 -0200 | [diff] [blame] | 240 | t = kmemdup(&dn_dev_sysctl, sizeof(*t), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | if (t == NULL) |
| 242 | return; |
| 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | for(i = 0; i < ARRAY_SIZE(t->dn_dev_vars) - 1; i++) { |
| 245 | long offset = (long)t->dn_dev_vars[i].data; |
| 246 | t->dn_dev_vars[i].data = ((char *)parms) + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | if (dev) { |
Pavel Emelyanov | 3151a9a | 2008-01-09 00:31:49 -0800 | [diff] [blame] | 250 | dn_ctl_path[DN_CTL_PATH_DEV].procname = dev->name; |
| 251 | dn_ctl_path[DN_CTL_PATH_DEV].ctl_name = dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } else { |
Pavel Emelyanov | 3151a9a | 2008-01-09 00:31:49 -0800 | [diff] [blame] | 253 | dn_ctl_path[DN_CTL_PATH_DEV].procname = parms->name; |
| 254 | dn_ctl_path[DN_CTL_PATH_DEV].ctl_name = parms->ctl_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | t->dn_dev_vars[0].extra1 = (void *)dev; |
| 258 | |
Pavel Emelyanov | 3151a9a | 2008-01-09 00:31:49 -0800 | [diff] [blame] | 259 | t->sysctl_header = register_sysctl_paths(dn_ctl_path, t->dn_dev_vars); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | if (t->sysctl_header == NULL) |
| 261 | kfree(t); |
| 262 | else |
| 263 | parms->sysctl = t; |
| 264 | } |
| 265 | |
| 266 | static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms) |
| 267 | { |
| 268 | if (parms->sysctl) { |
| 269 | struct dn_dev_sysctl_table *t = parms->sysctl; |
| 270 | parms->sysctl = NULL; |
| 271 | unregister_sysctl_table(t->sysctl_header); |
| 272 | kfree(t); |
| 273 | } |
| 274 | } |
| 275 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 276 | static int dn_forwarding_proc(ctl_table *table, int write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | struct file *filep, |
| 278 | void __user *buffer, |
| 279 | size_t *lenp, loff_t *ppos) |
| 280 | { |
| 281 | #ifdef CONFIG_DECNET_ROUTER |
| 282 | struct net_device *dev = table->extra1; |
| 283 | struct dn_dev *dn_db; |
| 284 | int err; |
| 285 | int tmp, old; |
| 286 | |
| 287 | if (table->extra1 == NULL) |
| 288 | return -EINVAL; |
| 289 | |
| 290 | dn_db = dev->dn_ptr; |
| 291 | old = dn_db->parms.forwarding; |
| 292 | |
| 293 | err = proc_dointvec(table, write, filep, buffer, lenp, ppos); |
| 294 | |
| 295 | if ((err >= 0) && write) { |
| 296 | if (dn_db->parms.forwarding < 0) |
| 297 | dn_db->parms.forwarding = 0; |
| 298 | if (dn_db->parms.forwarding > 2) |
| 299 | dn_db->parms.forwarding = 2; |
| 300 | /* |
| 301 | * What an ugly hack this is... its works, just. It |
| 302 | * would be nice if sysctl/proc were just that little |
| 303 | * bit more flexible so I don't have to write a special |
| 304 | * routine, or suffer hacks like this - SJW |
| 305 | */ |
| 306 | tmp = dn_db->parms.forwarding; |
| 307 | dn_db->parms.forwarding = old; |
| 308 | if (dn_db->parms.down) |
| 309 | dn_db->parms.down(dev); |
| 310 | dn_db->parms.forwarding = tmp; |
| 311 | if (dn_db->parms.up) |
| 312 | dn_db->parms.up(dev); |
| 313 | } |
| 314 | |
| 315 | return err; |
| 316 | #else |
| 317 | return -EINVAL; |
| 318 | #endif |
| 319 | } |
| 320 | |
| 321 | static int dn_forwarding_sysctl(ctl_table *table, int __user *name, int nlen, |
| 322 | void __user *oldval, size_t __user *oldlenp, |
Alexey Dobriyan | 1f29bcd | 2006-12-10 02:19:10 -0800 | [diff] [blame] | 323 | void __user *newval, size_t newlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
| 325 | #ifdef CONFIG_DECNET_ROUTER |
| 326 | struct net_device *dev = table->extra1; |
| 327 | struct dn_dev *dn_db; |
| 328 | int value; |
| 329 | |
| 330 | if (table->extra1 == NULL) |
| 331 | return -EINVAL; |
| 332 | |
| 333 | dn_db = dev->dn_ptr; |
| 334 | |
| 335 | if (newval && newlen) { |
| 336 | if (newlen != sizeof(int)) |
| 337 | return -EINVAL; |
| 338 | |
| 339 | if (get_user(value, (int __user *)newval)) |
| 340 | return -EFAULT; |
| 341 | if (value < 0) |
| 342 | return -EINVAL; |
| 343 | if (value > 2) |
| 344 | return -EINVAL; |
| 345 | |
| 346 | if (dn_db->parms.down) |
| 347 | dn_db->parms.down(dev); |
| 348 | dn_db->parms.forwarding = value; |
| 349 | if (dn_db->parms.up) |
| 350 | dn_db->parms.up(dev); |
| 351 | } |
| 352 | |
| 353 | return 0; |
| 354 | #else |
| 355 | return -EINVAL; |
| 356 | #endif |
| 357 | } |
| 358 | |
| 359 | #else /* CONFIG_SYSCTL */ |
| 360 | static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms) |
| 361 | { |
| 362 | } |
| 363 | static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms) |
| 364 | { |
| 365 | } |
| 366 | |
| 367 | #endif /* CONFIG_SYSCTL */ |
| 368 | |
| 369 | static inline __u16 mtu2blksize(struct net_device *dev) |
| 370 | { |
| 371 | u32 blksize = dev->mtu; |
| 372 | if (blksize > 0xffff) |
| 373 | blksize = 0xffff; |
| 374 | |
| 375 | if (dev->type == ARPHRD_ETHER || |
| 376 | dev->type == ARPHRD_PPP || |
| 377 | dev->type == ARPHRD_IPGRE || |
| 378 | dev->type == ARPHRD_LOOPBACK) |
| 379 | blksize -= 2; |
| 380 | |
| 381 | return (__u16)blksize; |
| 382 | } |
| 383 | |
| 384 | static struct dn_ifaddr *dn_dev_alloc_ifa(void) |
| 385 | { |
| 386 | struct dn_ifaddr *ifa; |
| 387 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 388 | ifa = kzalloc(sizeof(*ifa), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
| 390 | return ifa; |
| 391 | } |
| 392 | |
| 393 | static __inline__ void dn_dev_free_ifa(struct dn_ifaddr *ifa) |
| 394 | { |
| 395 | kfree(ifa); |
| 396 | } |
| 397 | |
| 398 | static void dn_dev_del_ifa(struct dn_dev *dn_db, struct dn_ifaddr **ifap, int destroy) |
| 399 | { |
| 400 | struct dn_ifaddr *ifa1 = *ifap; |
| 401 | unsigned char mac_addr[6]; |
| 402 | struct net_device *dev = dn_db->dev; |
| 403 | |
| 404 | ASSERT_RTNL(); |
| 405 | |
| 406 | *ifap = ifa1->ifa_next; |
| 407 | |
| 408 | if (dn_db->dev->type == ARPHRD_ETHER) { |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 409 | if (ifa1->ifa_local != dn_eth2dn(dev->dev_addr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | dn_dn2eth(mac_addr, ifa1->ifa_local); |
| 411 | dev_mc_delete(dev, mac_addr, ETH_ALEN, 0); |
| 412 | } |
| 413 | } |
| 414 | |
Thomas Graf | b020b94 | 2006-11-24 17:14:31 -0800 | [diff] [blame] | 415 | dn_ifaddr_notify(RTM_DELADDR, ifa1); |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 416 | blocking_notifier_call_chain(&dnaddr_chain, NETDEV_DOWN, ifa1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | if (destroy) { |
| 418 | dn_dev_free_ifa(ifa1); |
| 419 | |
| 420 | if (dn_db->ifa_list == NULL) |
| 421 | dn_dev_delete(dn_db->dev); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | static int dn_dev_insert_ifa(struct dn_dev *dn_db, struct dn_ifaddr *ifa) |
| 426 | { |
| 427 | struct net_device *dev = dn_db->dev; |
| 428 | struct dn_ifaddr *ifa1; |
| 429 | unsigned char mac_addr[6]; |
| 430 | |
| 431 | ASSERT_RTNL(); |
| 432 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 433 | /* Check for duplicates */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | for(ifa1 = dn_db->ifa_list; ifa1; ifa1 = ifa1->ifa_next) { |
| 435 | if (ifa1->ifa_local == ifa->ifa_local) |
| 436 | return -EEXIST; |
| 437 | } |
| 438 | |
| 439 | if (dev->type == ARPHRD_ETHER) { |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 440 | if (ifa->ifa_local != dn_eth2dn(dev->dev_addr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | dn_dn2eth(mac_addr, ifa->ifa_local); |
| 442 | dev_mc_add(dev, mac_addr, ETH_ALEN, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
| 446 | ifa->ifa_next = dn_db->ifa_list; |
| 447 | dn_db->ifa_list = ifa; |
| 448 | |
Thomas Graf | b020b94 | 2006-11-24 17:14:31 -0800 | [diff] [blame] | 449 | dn_ifaddr_notify(RTM_NEWADDR, ifa); |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 450 | blocking_notifier_call_chain(&dnaddr_chain, NETDEV_UP, ifa); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static int dn_dev_set_ifa(struct net_device *dev, struct dn_ifaddr *ifa) |
| 456 | { |
| 457 | struct dn_dev *dn_db = dev->dn_ptr; |
| 458 | int rv; |
| 459 | |
| 460 | if (dn_db == NULL) { |
| 461 | int err; |
| 462 | dn_db = dn_dev_create(dev, &err); |
| 463 | if (dn_db == NULL) |
| 464 | return err; |
| 465 | } |
| 466 | |
| 467 | ifa->ifa_dev = dn_db; |
| 468 | |
| 469 | if (dev->flags & IFF_LOOPBACK) |
| 470 | ifa->ifa_scope = RT_SCOPE_HOST; |
| 471 | |
| 472 | rv = dn_dev_insert_ifa(dn_db, ifa); |
| 473 | if (rv) |
| 474 | dn_dev_free_ifa(ifa); |
| 475 | return rv; |
| 476 | } |
| 477 | |
| 478 | |
| 479 | int dn_dev_ioctl(unsigned int cmd, void __user *arg) |
| 480 | { |
| 481 | char buffer[DN_IFREQ_SIZE]; |
| 482 | struct ifreq *ifr = (struct ifreq *)buffer; |
| 483 | struct sockaddr_dn *sdn = (struct sockaddr_dn *)&ifr->ifr_addr; |
| 484 | struct dn_dev *dn_db; |
| 485 | struct net_device *dev; |
| 486 | struct dn_ifaddr *ifa = NULL, **ifap = NULL; |
| 487 | int ret = 0; |
| 488 | |
| 489 | if (copy_from_user(ifr, arg, DN_IFREQ_SIZE)) |
| 490 | return -EFAULT; |
| 491 | ifr->ifr_name[IFNAMSIZ-1] = 0; |
| 492 | |
| 493 | #ifdef CONFIG_KMOD |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 494 | dev_load(&init_net, ifr->ifr_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | #endif |
| 496 | |
| 497 | switch(cmd) { |
| 498 | case SIOCGIFADDR: |
| 499 | break; |
| 500 | case SIOCSIFADDR: |
| 501 | if (!capable(CAP_NET_ADMIN)) |
| 502 | return -EACCES; |
| 503 | if (sdn->sdn_family != AF_DECnet) |
| 504 | return -EINVAL; |
| 505 | break; |
| 506 | default: |
| 507 | return -EINVAL; |
| 508 | } |
| 509 | |
| 510 | rtnl_lock(); |
| 511 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 512 | if ((dev = __dev_get_by_name(&init_net, ifr->ifr_name)) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | ret = -ENODEV; |
| 514 | goto done; |
| 515 | } |
| 516 | |
| 517 | if ((dn_db = dev->dn_ptr) != NULL) { |
| 518 | for (ifap = &dn_db->ifa_list; (ifa=*ifap) != NULL; ifap = &ifa->ifa_next) |
| 519 | if (strcmp(ifr->ifr_name, ifa->ifa_label) == 0) |
| 520 | break; |
| 521 | } |
| 522 | |
| 523 | if (ifa == NULL && cmd != SIOCSIFADDR) { |
| 524 | ret = -EADDRNOTAVAIL; |
| 525 | goto done; |
| 526 | } |
| 527 | |
| 528 | switch(cmd) { |
| 529 | case SIOCGIFADDR: |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 530 | *((__le16 *)sdn->sdn_nodeaddr) = ifa->ifa_local; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | goto rarok; |
| 532 | |
| 533 | case SIOCSIFADDR: |
| 534 | if (!ifa) { |
| 535 | if ((ifa = dn_dev_alloc_ifa()) == NULL) { |
| 536 | ret = -ENOBUFS; |
| 537 | break; |
| 538 | } |
| 539 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); |
| 540 | } else { |
| 541 | if (ifa->ifa_local == dn_saddr2dn(sdn)) |
| 542 | break; |
| 543 | dn_dev_del_ifa(dn_db, ifap, 0); |
| 544 | } |
| 545 | |
| 546 | ifa->ifa_local = ifa->ifa_address = dn_saddr2dn(sdn); |
| 547 | |
| 548 | ret = dn_dev_set_ifa(dev, ifa); |
| 549 | } |
| 550 | done: |
| 551 | rtnl_unlock(); |
| 552 | |
| 553 | return ret; |
| 554 | rarok: |
| 555 | if (copy_to_user(arg, ifr, DN_IFREQ_SIZE)) |
| 556 | ret = -EFAULT; |
| 557 | goto done; |
| 558 | } |
| 559 | |
| 560 | struct net_device *dn_dev_get_default(void) |
| 561 | { |
| 562 | struct net_device *dev; |
| 563 | read_lock(&dndev_lock); |
| 564 | dev = decnet_default_device; |
| 565 | if (dev) { |
| 566 | if (dev->dn_ptr) |
| 567 | dev_hold(dev); |
| 568 | else |
| 569 | dev = NULL; |
| 570 | } |
| 571 | read_unlock(&dndev_lock); |
| 572 | return dev; |
| 573 | } |
| 574 | |
| 575 | int dn_dev_set_default(struct net_device *dev, int force) |
| 576 | { |
| 577 | struct net_device *old = NULL; |
| 578 | int rv = -EBUSY; |
| 579 | if (!dev->dn_ptr) |
| 580 | return -ENODEV; |
| 581 | write_lock(&dndev_lock); |
| 582 | if (force || decnet_default_device == NULL) { |
| 583 | old = decnet_default_device; |
| 584 | decnet_default_device = dev; |
| 585 | rv = 0; |
| 586 | } |
| 587 | write_unlock(&dndev_lock); |
| 588 | if (old) |
Patrick Caulfield | 6a57b2e | 2006-03-29 13:57:31 -0800 | [diff] [blame] | 589 | dev_put(old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | return rv; |
| 591 | } |
| 592 | |
| 593 | static void dn_dev_check_default(struct net_device *dev) |
| 594 | { |
| 595 | write_lock(&dndev_lock); |
| 596 | if (dev == decnet_default_device) { |
| 597 | decnet_default_device = NULL; |
| 598 | } else { |
| 599 | dev = NULL; |
| 600 | } |
| 601 | write_unlock(&dndev_lock); |
| 602 | if (dev) |
| 603 | dev_put(dev); |
| 604 | } |
| 605 | |
| 606 | static struct dn_dev *dn_dev_by_index(int ifindex) |
| 607 | { |
| 608 | struct net_device *dev; |
| 609 | struct dn_dev *dn_dev = NULL; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 610 | dev = dev_get_by_index(&init_net, ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | if (dev) { |
| 612 | dn_dev = dev->dn_ptr; |
| 613 | dev_put(dev); |
| 614 | } |
| 615 | |
| 616 | return dn_dev; |
| 617 | } |
| 618 | |
Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 619 | static const struct nla_policy dn_ifa_policy[IFA_MAX+1] = { |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 620 | [IFA_ADDRESS] = { .type = NLA_U16 }, |
| 621 | [IFA_LOCAL] = { .type = NLA_U16 }, |
| 622 | [IFA_LABEL] = { .type = NLA_STRING, |
| 623 | .len = IFNAMSIZ - 1 }, |
| 624 | }; |
| 625 | |
| 626 | static int dn_nl_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | { |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 628 | struct net *net = skb->sk->sk_net; |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 629 | struct nlattr *tb[IFA_MAX+1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | struct dn_dev *dn_db; |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 631 | struct ifaddrmsg *ifm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | struct dn_ifaddr *ifa, **ifap; |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 633 | int err = -EINVAL; |
| 634 | |
| 635 | if (net != &init_net) |
| 636 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 638 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, dn_ifa_policy); |
| 639 | if (err < 0) |
| 640 | goto errout; |
| 641 | |
Pavel Emelyanov | 3ccd862 | 2007-11-30 23:43:31 +1100 | [diff] [blame] | 642 | err = -ENODEV; |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 643 | ifm = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | if ((dn_db = dn_dev_by_index(ifm->ifa_index)) == NULL) |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 645 | goto errout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
Pavel Emelyanov | 3ccd862 | 2007-11-30 23:43:31 +1100 | [diff] [blame] | 647 | err = -EADDRNOTAVAIL; |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 648 | for (ifap = &dn_db->ifa_list; (ifa = *ifap); ifap = &ifa->ifa_next) { |
| 649 | if (tb[IFA_LOCAL] && |
| 650 | nla_memcmp(tb[IFA_LOCAL], &ifa->ifa_local, 2)) |
| 651 | continue; |
| 652 | |
| 653 | if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | continue; |
| 655 | |
| 656 | dn_dev_del_ifa(dn_db, ifap, 1); |
| 657 | return 0; |
| 658 | } |
| 659 | |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 660 | errout: |
| 661 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | } |
| 663 | |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 664 | static int dn_nl_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | { |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 666 | struct net *net = skb->sk->sk_net; |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 667 | struct nlattr *tb[IFA_MAX+1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | struct net_device *dev; |
| 669 | struct dn_dev *dn_db; |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 670 | struct ifaddrmsg *ifm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | struct dn_ifaddr *ifa; |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 672 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 674 | if (net != &init_net) |
| 675 | return -EINVAL; |
| 676 | |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 677 | err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, dn_ifa_policy); |
| 678 | if (err < 0) |
| 679 | return err; |
| 680 | |
| 681 | if (tb[IFA_LOCAL] == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | return -EINVAL; |
| 683 | |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 684 | ifm = nlmsg_data(nlh); |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 685 | if ((dev = __dev_get_by_index(&init_net, ifm->ifa_index)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | return -ENODEV; |
| 687 | |
| 688 | if ((dn_db = dev->dn_ptr) == NULL) { |
| 689 | int err; |
| 690 | dn_db = dn_dev_create(dev, &err); |
| 691 | if (!dn_db) |
| 692 | return err; |
| 693 | } |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | if ((ifa = dn_dev_alloc_ifa()) == NULL) |
| 696 | return -ENOBUFS; |
| 697 | |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 698 | if (tb[IFA_ADDRESS] == NULL) |
| 699 | tb[IFA_ADDRESS] = tb[IFA_LOCAL]; |
| 700 | |
| 701 | ifa->ifa_local = nla_get_le16(tb[IFA_LOCAL]); |
| 702 | ifa->ifa_address = nla_get_le16(tb[IFA_ADDRESS]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | ifa->ifa_flags = ifm->ifa_flags; |
| 704 | ifa->ifa_scope = ifm->ifa_scope; |
| 705 | ifa->ifa_dev = dn_db; |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 706 | |
| 707 | if (tb[IFA_LABEL]) |
| 708 | nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | else |
| 710 | memcpy(ifa->ifa_label, dev->name, IFNAMSIZ); |
| 711 | |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 712 | err = dn_dev_insert_ifa(dn_db, ifa); |
| 713 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | dn_dev_free_ifa(ifa); |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 715 | |
| 716 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } |
| 718 | |
Thomas Graf | a6f01ca | 2006-11-24 17:14:07 -0800 | [diff] [blame] | 719 | static inline size_t dn_ifaddr_nlmsg_size(void) |
| 720 | { |
| 721 | return NLMSG_ALIGN(sizeof(struct ifaddrmsg)) |
| 722 | + nla_total_size(IFNAMSIZ) /* IFA_LABEL */ |
| 723 | + nla_total_size(2) /* IFA_ADDRESS */ |
| 724 | + nla_total_size(2); /* IFA_LOCAL */ |
| 725 | } |
| 726 | |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 727 | static int dn_nl_fill_ifaddr(struct sk_buff *skb, struct dn_ifaddr *ifa, |
| 728 | u32 pid, u32 seq, int event, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | { |
| 730 | struct ifaddrmsg *ifm; |
| 731 | struct nlmsghdr *nlh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 733 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags); |
| 734 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 735 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 737 | ifm = nlmsg_data(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | ifm->ifa_family = AF_DECnet; |
| 739 | ifm->ifa_prefixlen = 16; |
| 740 | ifm->ifa_flags = ifa->ifa_flags | IFA_F_PERMANENT; |
| 741 | ifm->ifa_scope = ifa->ifa_scope; |
| 742 | ifm->ifa_index = ifa->ifa_dev->dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 744 | if (ifa->ifa_address) |
| 745 | NLA_PUT_LE16(skb, IFA_ADDRESS, ifa->ifa_address); |
| 746 | if (ifa->ifa_local) |
| 747 | NLA_PUT_LE16(skb, IFA_LOCAL, ifa->ifa_local); |
| 748 | if (ifa->ifa_label[0]) |
| 749 | NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label); |
| 750 | |
| 751 | return nlmsg_end(skb, nlh); |
| 752 | |
| 753 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 754 | nlmsg_cancel(skb, nlh); |
| 755 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Thomas Graf | b020b94 | 2006-11-24 17:14:31 -0800 | [diff] [blame] | 758 | static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | { |
| 760 | struct sk_buff *skb; |
Thomas Graf | dc738dd | 2006-08-15 00:33:35 -0700 | [diff] [blame] | 761 | int err = -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | |
Thomas Graf | a6f01ca | 2006-11-24 17:14:07 -0800 | [diff] [blame] | 763 | skb = alloc_skb(dn_ifaddr_nlmsg_size(), GFP_KERNEL); |
Thomas Graf | dc738dd | 2006-08-15 00:33:35 -0700 | [diff] [blame] | 764 | if (skb == NULL) |
| 765 | goto errout; |
| 766 | |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 767 | err = dn_nl_fill_ifaddr(skb, ifa, 0, 0, event, 0); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 768 | if (err < 0) { |
| 769 | /* -EMSGSIZE implies BUG in dn_ifaddr_nlmsg_size() */ |
| 770 | WARN_ON(err == -EMSGSIZE); |
| 771 | kfree_skb(skb); |
| 772 | goto errout; |
| 773 | } |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 774 | err = rtnl_notify(skb, &init_net, 0, RTNLGRP_DECnet_IFADDR, NULL, GFP_KERNEL); |
Thomas Graf | dc738dd | 2006-08-15 00:33:35 -0700 | [diff] [blame] | 775 | errout: |
| 776 | if (err < 0) |
Denis V. Lunev | 97c53ca | 2007-11-19 22:26:51 -0800 | [diff] [blame] | 777 | rtnl_set_sk_err(&init_net, RTNLGRP_DECnet_IFADDR, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 780 | static int dn_nl_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | { |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 782 | struct net *net = skb->sk->sk_net; |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 783 | int idx, dn_idx = 0, skip_ndevs, skip_naddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | struct net_device *dev; |
| 785 | struct dn_dev *dn_db; |
| 786 | struct dn_ifaddr *ifa; |
| 787 | |
Denis V. Lunev | b854272 | 2007-12-01 00:21:31 +1100 | [diff] [blame] | 788 | if (net != &init_net) |
| 789 | return 0; |
| 790 | |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 791 | skip_ndevs = cb->args[0]; |
| 792 | skip_naddr = cb->args[1]; |
| 793 | |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 794 | idx = 0; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 795 | for_each_netdev(&init_net, dev) { |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 796 | if (idx < skip_ndevs) |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 797 | goto cont; |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 798 | else if (idx > skip_ndevs) { |
| 799 | /* Only skip over addresses for first dev dumped |
| 800 | * in this iteration (idx == skip_ndevs) */ |
| 801 | skip_naddr = 0; |
| 802 | } |
| 803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | if ((dn_db = dev->dn_ptr) == NULL) |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 805 | goto cont; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 807 | for (ifa = dn_db->ifa_list, dn_idx = 0; ifa; |
| 808 | ifa = ifa->ifa_next, dn_idx++) { |
| 809 | if (dn_idx < skip_naddr) |
Patrick McHardy | a2221f3 | 2007-09-11 10:45:15 +0200 | [diff] [blame] | 810 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | |
Thomas Graf | 4a89c25 | 2006-11-24 17:14:51 -0800 | [diff] [blame] | 812 | if (dn_nl_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid, |
| 813 | cb->nlh->nlmsg_seq, RTM_NEWADDR, |
| 814 | NLM_F_MULTI) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | goto done; |
| 816 | } |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 817 | cont: |
| 818 | idx++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | } |
| 820 | done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | cb->args[0] = idx; |
| 822 | cb->args[1] = dn_idx; |
| 823 | |
| 824 | return skb->len; |
| 825 | } |
| 826 | |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 827 | static int dn_dev_get_first(struct net_device *dev, __le16 *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | { |
| 829 | struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr; |
| 830 | struct dn_ifaddr *ifa; |
| 831 | int rv = -ENODEV; |
| 832 | if (dn_db == NULL) |
| 833 | goto out; |
| 834 | ifa = dn_db->ifa_list; |
| 835 | if (ifa != NULL) { |
| 836 | *addr = ifa->ifa_local; |
| 837 | rv = 0; |
| 838 | } |
| 839 | out: |
| 840 | return rv; |
| 841 | } |
| 842 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 843 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | * Find a default address to bind to. |
| 845 | * |
| 846 | * This is one of those areas where the initial VMS concepts don't really |
| 847 | * map onto the Linux concepts, and since we introduced multiple addresses |
| 848 | * per interface we have to cope with slightly odd ways of finding out what |
| 849 | * "our address" really is. Mostly it's not a problem; for this we just guess |
| 850 | * a sensible default. Eventually the routing code will take care of all the |
| 851 | * nasties for us I hope. |
| 852 | */ |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 853 | int dn_dev_bind_default(__le16 *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | { |
| 855 | struct net_device *dev; |
| 856 | int rv; |
| 857 | dev = dn_dev_get_default(); |
| 858 | last_chance: |
| 859 | if (dev) { |
| 860 | read_lock(&dev_base_lock); |
| 861 | rv = dn_dev_get_first(dev, addr); |
| 862 | read_unlock(&dev_base_lock); |
| 863 | dev_put(dev); |
Eric W. Biederman | 2774c7a | 2007-09-26 22:10:56 -0700 | [diff] [blame] | 864 | if (rv == 0 || dev == init_net.loopback_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | return rv; |
| 866 | } |
Eric W. Biederman | 2774c7a | 2007-09-26 22:10:56 -0700 | [diff] [blame] | 867 | dev = init_net.loopback_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | dev_hold(dev); |
| 869 | goto last_chance; |
| 870 | } |
| 871 | |
| 872 | static void dn_send_endnode_hello(struct net_device *dev, struct dn_ifaddr *ifa) |
| 873 | { |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 874 | struct endnode_hello_message *msg; |
| 875 | struct sk_buff *skb = NULL; |
| 876 | __le16 *pktlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr; |
| 878 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 879 | if ((skb = dn_alloc_skb(NULL, sizeof(*msg), GFP_ATOMIC)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | return; |
| 881 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 882 | skb->dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 884 | msg = (struct endnode_hello_message *)skb_put(skb,sizeof(*msg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 886 | msg->msgflg = 0x0D; |
| 887 | memcpy(msg->tiver, dn_eco_version, 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | dn_dn2eth(msg->id, ifa->ifa_local); |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 889 | msg->iinfo = DN_RT_INFO_ENDN; |
| 890 | msg->blksize = dn_htons(mtu2blksize(dev)); |
| 891 | msg->area = 0x00; |
| 892 | memset(msg->seed, 0, 8); |
| 893 | memcpy(msg->neighbor, dn_hiord, ETH_ALEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | |
| 895 | if (dn_db->router) { |
| 896 | struct dn_neigh *dn = (struct dn_neigh *)dn_db->router; |
| 897 | dn_dn2eth(msg->neighbor, dn->addr); |
| 898 | } |
| 899 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 900 | msg->timer = dn_htons((unsigned short)dn_db->parms.t3); |
| 901 | msg->mpd = 0x00; |
| 902 | msg->datalen = 0x02; |
| 903 | memset(msg->data, 0xAA, 2); |
| 904 | |
| 905 | pktlen = (__le16 *)skb_push(skb,2); |
| 906 | *pktlen = dn_htons(skb->len - 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 908 | skb_reset_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | |
| 910 | dn_rt_finish_output(skb, dn_rt_all_rt_mcast, msg->id); |
| 911 | } |
| 912 | |
| 913 | |
| 914 | #define DRDELAY (5 * HZ) |
| 915 | |
| 916 | static int dn_am_i_a_router(struct dn_neigh *dn, struct dn_dev *dn_db, struct dn_ifaddr *ifa) |
| 917 | { |
| 918 | /* First check time since device went up */ |
| 919 | if ((jiffies - dn_db->uptime) < DRDELAY) |
| 920 | return 0; |
| 921 | |
| 922 | /* If there is no router, then yes... */ |
| 923 | if (!dn_db->router) |
| 924 | return 1; |
| 925 | |
| 926 | /* otherwise only if we have a higher priority or.. */ |
| 927 | if (dn->priority < dn_db->parms.priority) |
| 928 | return 1; |
| 929 | |
| 930 | /* if we have equal priority and a higher node number */ |
| 931 | if (dn->priority != dn_db->parms.priority) |
| 932 | return 0; |
| 933 | |
| 934 | if (dn_ntohs(dn->addr) < dn_ntohs(ifa->ifa_local)) |
| 935 | return 1; |
| 936 | |
| 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | static void dn_send_router_hello(struct net_device *dev, struct dn_ifaddr *ifa) |
| 941 | { |
| 942 | int n; |
| 943 | struct dn_dev *dn_db = dev->dn_ptr; |
| 944 | struct dn_neigh *dn = (struct dn_neigh *)dn_db->router; |
| 945 | struct sk_buff *skb; |
| 946 | size_t size; |
| 947 | unsigned char *ptr; |
| 948 | unsigned char *i1, *i2; |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 949 | __le16 *pktlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | char *src; |
| 951 | |
| 952 | if (mtu2blksize(dev) < (26 + 7)) |
| 953 | return; |
| 954 | |
| 955 | n = mtu2blksize(dev) - 26; |
| 956 | n /= 7; |
| 957 | |
| 958 | if (n > 32) |
| 959 | n = 32; |
| 960 | |
| 961 | size = 2 + 26 + 7 * n; |
| 962 | |
| 963 | if ((skb = dn_alloc_skb(NULL, size, GFP_ATOMIC)) == NULL) |
| 964 | return; |
| 965 | |
| 966 | skb->dev = dev; |
| 967 | ptr = skb_put(skb, size); |
| 968 | |
| 969 | *ptr++ = DN_RT_PKT_CNTL | DN_RT_PKT_ERTH; |
| 970 | *ptr++ = 2; /* ECO */ |
| 971 | *ptr++ = 0; |
| 972 | *ptr++ = 0; |
| 973 | dn_dn2eth(ptr, ifa->ifa_local); |
| 974 | src = ptr; |
| 975 | ptr += ETH_ALEN; |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 976 | *ptr++ = dn_db->parms.forwarding == 1 ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | DN_RT_INFO_L1RT : DN_RT_INFO_L2RT; |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 978 | *((__le16 *)ptr) = dn_htons(mtu2blksize(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | ptr += 2; |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 980 | *ptr++ = dn_db->parms.priority; /* Priority */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | *ptr++ = 0; /* Area: Reserved */ |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 982 | *((__le16 *)ptr) = dn_htons((unsigned short)dn_db->parms.t3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | ptr += 2; |
| 984 | *ptr++ = 0; /* MPD: Reserved */ |
| 985 | i1 = ptr++; |
| 986 | memset(ptr, 0, 7); /* Name: Reserved */ |
| 987 | ptr += 7; |
| 988 | i2 = ptr++; |
| 989 | |
| 990 | n = dn_neigh_elist(dev, ptr, n); |
| 991 | |
| 992 | *i2 = 7 * n; |
| 993 | *i1 = 8 + *i2; |
| 994 | |
| 995 | skb_trim(skb, (27 + *i2)); |
| 996 | |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 997 | pktlen = (__le16 *)skb_push(skb, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | *pktlen = dn_htons(skb->len - 2); |
| 999 | |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 1000 | skb_reset_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | |
| 1002 | if (dn_am_i_a_router(dn, dn_db, ifa)) { |
| 1003 | struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC); |
| 1004 | if (skb2) { |
| 1005 | dn_rt_finish_output(skb2, dn_rt_all_end_mcast, src); |
| 1006 | } |
| 1007 | } |
| 1008 | |
| 1009 | dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src); |
| 1010 | } |
| 1011 | |
| 1012 | static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa) |
| 1013 | { |
| 1014 | struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr; |
| 1015 | |
| 1016 | if (dn_db->parms.forwarding == 0) |
| 1017 | dn_send_endnode_hello(dev, ifa); |
| 1018 | else |
| 1019 | dn_send_router_hello(dev, ifa); |
| 1020 | } |
| 1021 | |
| 1022 | static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa) |
| 1023 | { |
| 1024 | int tdlen = 16; |
| 1025 | int size = dev->hard_header_len + 2 + 4 + tdlen; |
| 1026 | struct sk_buff *skb = dn_alloc_skb(NULL, size, GFP_ATOMIC); |
| 1027 | int i; |
| 1028 | unsigned char *ptr; |
| 1029 | char src[ETH_ALEN]; |
| 1030 | |
| 1031 | if (skb == NULL) |
| 1032 | return ; |
| 1033 | |
| 1034 | skb->dev = dev; |
| 1035 | skb_push(skb, dev->hard_header_len); |
| 1036 | ptr = skb_put(skb, 2 + 4 + tdlen); |
| 1037 | |
| 1038 | *ptr++ = DN_RT_PKT_HELO; |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 1039 | *((__le16 *)ptr) = ifa->ifa_local; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | ptr += 2; |
| 1041 | *ptr++ = tdlen; |
| 1042 | |
| 1043 | for(i = 0; i < tdlen; i++) |
| 1044 | *ptr++ = 0252; |
| 1045 | |
| 1046 | dn_dn2eth(src, ifa->ifa_local); |
| 1047 | dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src); |
| 1048 | } |
| 1049 | |
| 1050 | static int dn_eth_up(struct net_device *dev) |
| 1051 | { |
| 1052 | struct dn_dev *dn_db = dev->dn_ptr; |
| 1053 | |
| 1054 | if (dn_db->parms.forwarding == 0) |
| 1055 | dev_mc_add(dev, dn_rt_all_end_mcast, ETH_ALEN, 0); |
| 1056 | else |
| 1057 | dev_mc_add(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0); |
| 1058 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | dn_db->use_long = 1; |
| 1060 | |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | static void dn_eth_down(struct net_device *dev) |
| 1065 | { |
| 1066 | struct dn_dev *dn_db = dev->dn_ptr; |
| 1067 | |
| 1068 | if (dn_db->parms.forwarding == 0) |
| 1069 | dev_mc_delete(dev, dn_rt_all_end_mcast, ETH_ALEN, 0); |
| 1070 | else |
| 1071 | dev_mc_delete(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0); |
| 1072 | } |
| 1073 | |
| 1074 | static void dn_dev_set_timer(struct net_device *dev); |
| 1075 | |
| 1076 | static void dn_dev_timer_func(unsigned long arg) |
| 1077 | { |
| 1078 | struct net_device *dev = (struct net_device *)arg; |
| 1079 | struct dn_dev *dn_db = dev->dn_ptr; |
| 1080 | struct dn_ifaddr *ifa; |
| 1081 | |
| 1082 | if (dn_db->t3 <= dn_db->parms.t2) { |
| 1083 | if (dn_db->parms.timer3) { |
| 1084 | for(ifa = dn_db->ifa_list; ifa; ifa = ifa->ifa_next) { |
| 1085 | if (!(ifa->ifa_flags & IFA_F_SECONDARY)) |
| 1086 | dn_db->parms.timer3(dev, ifa); |
| 1087 | } |
| 1088 | } |
| 1089 | dn_db->t3 = dn_db->parms.t3; |
| 1090 | } else { |
| 1091 | dn_db->t3 -= dn_db->parms.t2; |
| 1092 | } |
| 1093 | |
| 1094 | dn_dev_set_timer(dev); |
| 1095 | } |
| 1096 | |
| 1097 | static void dn_dev_set_timer(struct net_device *dev) |
| 1098 | { |
| 1099 | struct dn_dev *dn_db = dev->dn_ptr; |
| 1100 | |
| 1101 | if (dn_db->parms.t2 > dn_db->parms.t3) |
| 1102 | dn_db->parms.t2 = dn_db->parms.t3; |
| 1103 | |
| 1104 | dn_db->timer.data = (unsigned long)dev; |
| 1105 | dn_db->timer.function = dn_dev_timer_func; |
| 1106 | dn_db->timer.expires = jiffies + (dn_db->parms.t2 * HZ); |
| 1107 | |
| 1108 | add_timer(&dn_db->timer); |
| 1109 | } |
| 1110 | |
| 1111 | struct dn_dev *dn_dev_create(struct net_device *dev, int *err) |
| 1112 | { |
| 1113 | int i; |
| 1114 | struct dn_dev_parms *p = dn_dev_list; |
| 1115 | struct dn_dev *dn_db; |
| 1116 | |
| 1117 | for(i = 0; i < DN_DEV_LIST_SIZE; i++, p++) { |
| 1118 | if (p->type == dev->type) |
| 1119 | break; |
| 1120 | } |
| 1121 | |
| 1122 | *err = -ENODEV; |
| 1123 | if (i == DN_DEV_LIST_SIZE) |
| 1124 | return NULL; |
| 1125 | |
| 1126 | *err = -ENOBUFS; |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 1127 | if ((dn_db = kzalloc(sizeof(struct dn_dev), GFP_ATOMIC)) == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | return NULL; |
| 1129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | memcpy(&dn_db->parms, p, sizeof(struct dn_dev_parms)); |
| 1131 | smp_wmb(); |
| 1132 | dev->dn_ptr = dn_db; |
| 1133 | dn_db->dev = dev; |
| 1134 | init_timer(&dn_db->timer); |
| 1135 | |
| 1136 | dn_db->uptime = jiffies; |
Eric W. Biederman | 95743de | 2007-01-25 15:51:51 -0800 | [diff] [blame] | 1137 | |
| 1138 | dn_db->neigh_parms = neigh_parms_alloc(dev, &dn_neigh_table); |
| 1139 | if (!dn_db->neigh_parms) { |
| 1140 | dev->dn_ptr = NULL; |
| 1141 | kfree(dn_db); |
| 1142 | return NULL; |
| 1143 | } |
| 1144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | if (dn_db->parms.up) { |
| 1146 | if (dn_db->parms.up(dev) < 0) { |
Eric W. Biederman | 95743de | 2007-01-25 15:51:51 -0800 | [diff] [blame] | 1147 | neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | dev->dn_ptr = NULL; |
| 1149 | kfree(dn_db); |
| 1150 | return NULL; |
| 1151 | } |
| 1152 | } |
| 1153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | dn_dev_sysctl_register(dev, &dn_db->parms); |
| 1155 | |
| 1156 | dn_dev_set_timer(dev); |
| 1157 | |
| 1158 | *err = 0; |
| 1159 | return dn_db; |
| 1160 | } |
| 1161 | |
| 1162 | |
| 1163 | /* |
| 1164 | * This processes a device up event. We only start up |
| 1165 | * the loopback device & ethernet devices with correct |
| 1166 | * MAC addreses automatically. Others must be started |
| 1167 | * specifically. |
| 1168 | * |
| 1169 | * FIXME: How should we configure the loopback address ? If we could dispense |
| 1170 | * with using decnet_address here and for autobind, it will be one less thing |
| 1171 | * for users to worry about setting up. |
| 1172 | */ |
| 1173 | |
| 1174 | void dn_dev_up(struct net_device *dev) |
| 1175 | { |
| 1176 | struct dn_ifaddr *ifa; |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 1177 | __le16 addr = decnet_address; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | int maybe_default = 0; |
| 1179 | struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr; |
| 1180 | |
| 1181 | if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK)) |
| 1182 | return; |
| 1183 | |
| 1184 | /* |
| 1185 | * Need to ensure that loopback device has a dn_db attached to it |
| 1186 | * to allow creation of neighbours against it, even though it might |
| 1187 | * not have a local address of its own. Might as well do the same for |
| 1188 | * all autoconfigured interfaces. |
| 1189 | */ |
| 1190 | if (dn_db == NULL) { |
| 1191 | int err; |
| 1192 | dn_db = dn_dev_create(dev, &err); |
| 1193 | if (dn_db == NULL) |
| 1194 | return; |
| 1195 | } |
| 1196 | |
| 1197 | if (dev->type == ARPHRD_ETHER) { |
| 1198 | if (memcmp(dev->dev_addr, dn_hiord, 4) != 0) |
| 1199 | return; |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 1200 | addr = dn_eth2dn(dev->dev_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | maybe_default = 1; |
| 1202 | } |
| 1203 | |
| 1204 | if (addr == 0) |
| 1205 | return; |
| 1206 | |
| 1207 | if ((ifa = dn_dev_alloc_ifa()) == NULL) |
| 1208 | return; |
| 1209 | |
| 1210 | ifa->ifa_local = ifa->ifa_address = addr; |
| 1211 | ifa->ifa_flags = 0; |
| 1212 | ifa->ifa_scope = RT_SCOPE_UNIVERSE; |
| 1213 | strcpy(ifa->ifa_label, dev->name); |
| 1214 | |
| 1215 | dn_dev_set_ifa(dev, ifa); |
| 1216 | |
| 1217 | /* |
| 1218 | * Automagically set the default device to the first automatically |
| 1219 | * configured ethernet card in the system. |
| 1220 | */ |
| 1221 | if (maybe_default) { |
| 1222 | dev_hold(dev); |
| 1223 | if (dn_dev_set_default(dev, 0)) |
| 1224 | dev_put(dev); |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | static void dn_dev_delete(struct net_device *dev) |
| 1229 | { |
| 1230 | struct dn_dev *dn_db = dev->dn_ptr; |
| 1231 | |
| 1232 | if (dn_db == NULL) |
| 1233 | return; |
| 1234 | |
| 1235 | del_timer_sync(&dn_db->timer); |
| 1236 | dn_dev_sysctl_unregister(&dn_db->parms); |
| 1237 | dn_dev_check_default(dev); |
| 1238 | neigh_ifdown(&dn_neigh_table, dev); |
| 1239 | |
| 1240 | if (dn_db->parms.down) |
| 1241 | dn_db->parms.down(dev); |
| 1242 | |
| 1243 | dev->dn_ptr = NULL; |
| 1244 | |
| 1245 | neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms); |
| 1246 | neigh_ifdown(&dn_neigh_table, dev); |
| 1247 | |
| 1248 | if (dn_db->router) |
| 1249 | neigh_release(dn_db->router); |
| 1250 | if (dn_db->peer) |
| 1251 | neigh_release(dn_db->peer); |
| 1252 | |
| 1253 | kfree(dn_db); |
| 1254 | } |
| 1255 | |
| 1256 | void dn_dev_down(struct net_device *dev) |
| 1257 | { |
| 1258 | struct dn_dev *dn_db = dev->dn_ptr; |
| 1259 | struct dn_ifaddr *ifa; |
| 1260 | |
| 1261 | if (dn_db == NULL) |
| 1262 | return; |
| 1263 | |
| 1264 | while((ifa = dn_db->ifa_list) != NULL) { |
| 1265 | dn_dev_del_ifa(dn_db, &dn_db->ifa_list, 0); |
| 1266 | dn_dev_free_ifa(ifa); |
| 1267 | } |
| 1268 | |
| 1269 | dn_dev_delete(dev); |
| 1270 | } |
| 1271 | |
| 1272 | void dn_dev_init_pkt(struct sk_buff *skb) |
| 1273 | { |
| 1274 | return; |
| 1275 | } |
| 1276 | |
| 1277 | void dn_dev_veri_pkt(struct sk_buff *skb) |
| 1278 | { |
| 1279 | return; |
| 1280 | } |
| 1281 | |
| 1282 | void dn_dev_hello(struct sk_buff *skb) |
| 1283 | { |
| 1284 | return; |
| 1285 | } |
| 1286 | |
| 1287 | void dn_dev_devices_off(void) |
| 1288 | { |
| 1289 | struct net_device *dev; |
| 1290 | |
| 1291 | rtnl_lock(); |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1292 | for_each_netdev(&init_net, dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | dn_dev_down(dev); |
| 1294 | rtnl_unlock(); |
| 1295 | |
| 1296 | } |
| 1297 | |
| 1298 | void dn_dev_devices_on(void) |
| 1299 | { |
| 1300 | struct net_device *dev; |
| 1301 | |
| 1302 | rtnl_lock(); |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1303 | for_each_netdev(&init_net, dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | if (dev->flags & IFF_UP) |
| 1305 | dn_dev_up(dev); |
| 1306 | } |
| 1307 | rtnl_unlock(); |
| 1308 | } |
| 1309 | |
| 1310 | int register_dnaddr_notifier(struct notifier_block *nb) |
| 1311 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1312 | return blocking_notifier_chain_register(&dnaddr_chain, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | int unregister_dnaddr_notifier(struct notifier_block *nb) |
| 1316 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1317 | return blocking_notifier_chain_unregister(&dnaddr_chain, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | #ifdef CONFIG_PROC_FS |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 1321 | static inline int is_dn_dev(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | { |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 1323 | return dev->dn_ptr != NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | } |
| 1325 | |
| 1326 | static void *dn_dev_seq_start(struct seq_file *seq, loff_t *pos) |
| 1327 | { |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 1328 | int i; |
| 1329 | struct net_device *dev; |
| 1330 | |
| 1331 | read_lock(&dev_base_lock); |
| 1332 | |
| 1333 | if (*pos == 0) |
| 1334 | return SEQ_START_TOKEN; |
| 1335 | |
| 1336 | i = 1; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1337 | for_each_netdev(&init_net, dev) { |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 1338 | if (!is_dn_dev(dev)) |
| 1339 | continue; |
| 1340 | |
| 1341 | if (i++ == *pos) |
| 1342 | return dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | } |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 1344 | |
| 1345 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | static void *dn_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 1349 | { |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 1350 | struct net_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | ++*pos; |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 1353 | |
| 1354 | dev = (struct net_device *)v; |
| 1355 | if (v == SEQ_START_TOKEN) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1356 | dev = net_device_entry(&init_net.dev_base_head); |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 1357 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1358 | for_each_netdev_continue(&init_net, dev) { |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 1359 | if (!is_dn_dev(dev)) |
| 1360 | continue; |
| 1361 | |
| 1362 | return dev; |
| 1363 | } |
| 1364 | |
| 1365 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | static void dn_dev_seq_stop(struct seq_file *seq, void *v) |
| 1369 | { |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 1370 | read_unlock(&dev_base_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | static char *dn_type2asc(char type) |
| 1374 | { |
| 1375 | switch(type) { |
| 1376 | case DN_DEV_BCAST: |
| 1377 | return "B"; |
| 1378 | case DN_DEV_UCAST: |
| 1379 | return "U"; |
| 1380 | case DN_DEV_MPOINT: |
| 1381 | return "M"; |
| 1382 | } |
| 1383 | |
| 1384 | return "?"; |
| 1385 | } |
| 1386 | |
| 1387 | static int dn_dev_seq_show(struct seq_file *seq, void *v) |
| 1388 | { |
| 1389 | if (v == SEQ_START_TOKEN) |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1390 | seq_puts(seq, "Name Flags T1 Timer1 T3 Timer3 BlkSize Pri State DevType Router Peer\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | else { |
| 1392 | struct net_device *dev = v; |
| 1393 | char peer_buf[DN_ASCBUF_LEN]; |
| 1394 | char router_buf[DN_ASCBUF_LEN]; |
| 1395 | struct dn_dev *dn_db = dev->dn_ptr; |
| 1396 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1397 | seq_printf(seq, "%-8s %1s %04u %04u %04lu %04lu" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | " %04hu %03d %02x %-10s %-7s %-7s\n", |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1399 | dev->name ? dev->name : "???", |
| 1400 | dn_type2asc(dn_db->parms.mode), |
| 1401 | 0, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | dn_db->t3, dn_db->parms.t3, |
| 1403 | mtu2blksize(dev), |
| 1404 | dn_db->parms.priority, |
| 1405 | dn_db->parms.state, dn_db->parms.name, |
Steven Whitehouse | c4ea94a | 2006-03-20 22:42:39 -0800 | [diff] [blame] | 1406 | dn_db->router ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->router->primary_key), router_buf) : "", |
| 1407 | dn_db->peer ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->peer->primary_key), peer_buf) : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | } |
| 1409 | return 0; |
| 1410 | } |
| 1411 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 1412 | static const struct seq_operations dn_dev_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | .start = dn_dev_seq_start, |
| 1414 | .next = dn_dev_seq_next, |
| 1415 | .stop = dn_dev_seq_stop, |
| 1416 | .show = dn_dev_seq_show, |
| 1417 | }; |
| 1418 | |
| 1419 | static int dn_dev_seq_open(struct inode *inode, struct file *file) |
| 1420 | { |
| 1421 | return seq_open(file, &dn_dev_seq_ops); |
| 1422 | } |
| 1423 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 1424 | static const struct file_operations dn_dev_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | .owner = THIS_MODULE, |
| 1426 | .open = dn_dev_seq_open, |
| 1427 | .read = seq_read, |
| 1428 | .llseek = seq_lseek, |
| 1429 | .release = seq_release, |
| 1430 | }; |
| 1431 | |
| 1432 | #endif /* CONFIG_PROC_FS */ |
| 1433 | |
Alexey Dobriyan | 4e05806 | 2007-11-05 21:30:11 -0800 | [diff] [blame] | 1434 | static int addr[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | module_param_array(addr, int, NULL, 0444); |
| 1436 | MODULE_PARM_DESC(addr, "The DECnet address of this machine: area,node"); |
| 1437 | |
| 1438 | void __init dn_dev_init(void) |
| 1439 | { |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1440 | if (addr[0] > 63 || addr[0] < 0) { |
| 1441 | printk(KERN_ERR "DECnet: Area must be between 0 and 63"); |
| 1442 | return; |
| 1443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1445 | if (addr[1] > 1023 || addr[1] < 0) { |
| 1446 | printk(KERN_ERR "DECnet: Node must be between 0 and 1023"); |
| 1447 | return; |
| 1448 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | |
YOSHIFUJI Hideaki | 429eb0f | 2007-02-09 23:24:40 +0900 | [diff] [blame] | 1450 | decnet_address = dn_htons((addr[0] << 10) | addr[1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | |
| 1452 | dn_dev_devices_on(); |
| 1453 | |
Thomas Graf | fa34ddd | 2007-03-22 11:57:46 -0700 | [diff] [blame] | 1454 | rtnl_register(PF_DECnet, RTM_NEWADDR, dn_nl_newaddr, NULL); |
| 1455 | rtnl_register(PF_DECnet, RTM_DELADDR, dn_nl_deladdr, NULL); |
| 1456 | rtnl_register(PF_DECnet, RTM_GETADDR, NULL, dn_nl_dump_ifaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 1458 | proc_net_fops_create(&init_net, "decnet_dev", S_IRUGO, &dn_dev_seq_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | |
| 1460 | #ifdef CONFIG_SYSCTL |
| 1461 | { |
| 1462 | int i; |
| 1463 | for(i = 0; i < DN_DEV_LIST_SIZE; i++) |
| 1464 | dn_dev_sysctl_register(NULL, &dn_dev_list[i]); |
| 1465 | } |
| 1466 | #endif /* CONFIG_SYSCTL */ |
| 1467 | } |
| 1468 | |
| 1469 | void __exit dn_dev_cleanup(void) |
| 1470 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | #ifdef CONFIG_SYSCTL |
| 1472 | { |
| 1473 | int i; |
| 1474 | for(i = 0; i < DN_DEV_LIST_SIZE; i++) |
| 1475 | dn_dev_sysctl_unregister(&dn_dev_list[i]); |
| 1476 | } |
| 1477 | #endif /* CONFIG_SYSCTL */ |
| 1478 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 1479 | proc_net_remove(&init_net, "decnet_dev"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | |
| 1481 | dn_dev_devices_off(); |
| 1482 | } |