dn_dev.c 32 KB

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