dn_dev.c 32 KB

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