dn_dev.c 32 KB

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