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