dn_dev.c 33 KB

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