dn_dev.c 34 KB

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