bpqether.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * G8BPQ compatible "AX.25 via ethernet" driver release 004
  4. *
  5. * This code REQUIRES 2.0.0 or higher/ NET3.029
  6. *
  7. * This is a "pseudo" network driver to allow AX.25 over Ethernet
  8. * using G8BPQ encapsulation. It has been extracted from the protocol
  9. * implementation because
  10. *
  11. * - things got unreadable within the protocol stack
  12. * - to cure the protocol stack from "feature-ism"
  13. * - a protocol implementation shouldn't need to know on
  14. * which hardware it is running
  15. * - user-level programs like the AX.25 utilities shouldn't
  16. * need to know about the hardware.
  17. * - IP over ethernet encapsulated AX.25 was impossible
  18. * - rxecho.c did not work
  19. * - to have room for extensions
  20. * - it just deserves to "live" as an own driver
  21. *
  22. * This driver can use any ethernet destination address, and can be
  23. * limited to accept frames from one dedicated ethernet card only.
  24. *
  25. * Note that the driver sets up the BPQ devices automagically on
  26. * startup or (if started before the "insmod" of an ethernet device)
  27. * on "ifconfig up". It hopefully will remove the BPQ on "rmmod"ing
  28. * the ethernet device (in fact: as soon as another ethernet or bpq
  29. * device gets "ifconfig"ured).
  30. *
  31. * I have heard that several people are thinking of experiments
  32. * with highspeed packet radio using existing ethernet cards.
  33. * Well, this driver is prepared for this purpose, just add
  34. * your tx key control and a txdelay / tailtime algorithm,
  35. * probably some buffering, and /voila/...
  36. *
  37. * History
  38. * BPQ 001 Joerg(DL1BKE) Extracted BPQ code from AX.25
  39. * protocol stack and added my own
  40. * yet existing patches
  41. * BPQ 002 Joerg(DL1BKE) Scan network device list on
  42. * startup.
  43. * BPQ 003 Joerg(DL1BKE) Ethernet destination address
  44. * and accepted source address
  45. * can be configured by an ioctl()
  46. * call.
  47. * Fixed to match Linux networking
  48. * changes - 2.1.15.
  49. * BPQ 004 Joerg(DL1BKE) Fixed to not lock up on ifconfig.
  50. */
  51. #include <linux/errno.h>
  52. #include <linux/types.h>
  53. #include <linux/socket.h>
  54. #include <linux/in.h>
  55. #include <linux/kernel.h>
  56. #include <linux/string.h>
  57. #include <linux/net.h>
  58. #include <linux/slab.h>
  59. #include <net/ax25.h>
  60. #include <linux/inet.h>
  61. #include <linux/netdevice.h>
  62. #include <linux/etherdevice.h>
  63. #include <linux/if_arp.h>
  64. #include <linux/skbuff.h>
  65. #include <net/sock.h>
  66. #include <linux/uaccess.h>
  67. #include <linux/mm.h>
  68. #include <linux/interrupt.h>
  69. #include <linux/notifier.h>
  70. #include <linux/proc_fs.h>
  71. #include <linux/seq_file.h>
  72. #include <linux/stat.h>
  73. #include <linux/module.h>
  74. #include <linux/init.h>
  75. #include <linux/rtnetlink.h>
  76. #include <net/ip.h>
  77. #include <net/arp.h>
  78. #include <net/net_namespace.h>
  79. #include <linux/bpqether.h>
  80. static const char banner[] __initconst = KERN_INFO \
  81. "AX.25: bpqether driver version 004\n";
  82. static int bpq_rcv(struct sk_buff *, struct net_device *, struct packet_type *, struct net_device *);
  83. static int bpq_device_event(struct notifier_block *, unsigned long, void *);
  84. static struct packet_type bpq_packet_type __read_mostly = {
  85. .type = cpu_to_be16(ETH_P_BPQ),
  86. .func = bpq_rcv,
  87. };
  88. static struct notifier_block bpq_dev_notifier = {
  89. .notifier_call = bpq_device_event,
  90. };
  91. struct bpqdev {
  92. struct list_head bpq_list; /* list of bpq devices chain */
  93. struct net_device *ethdev; /* link to ethernet device */
  94. struct net_device *axdev; /* bpq device (bpq#) */
  95. char dest_addr[6]; /* ether destination address */
  96. char acpt_addr[6]; /* accept ether frames from this address only */
  97. };
  98. static LIST_HEAD(bpq_devices);
  99. /*
  100. * bpqether network devices are paired with ethernet devices below them, so
  101. * form a special "super class" of normal ethernet devices; split their locks
  102. * off into a separate class since they always nest.
  103. */
  104. static struct lock_class_key bpq_netdev_xmit_lock_key;
  105. static struct lock_class_key bpq_netdev_addr_lock_key;
  106. static void bpq_set_lockdep_class_one(struct net_device *dev,
  107. struct netdev_queue *txq,
  108. void *_unused)
  109. {
  110. lockdep_set_class(&txq->_xmit_lock, &bpq_netdev_xmit_lock_key);
  111. }
  112. static void bpq_set_lockdep_class(struct net_device *dev)
  113. {
  114. lockdep_set_class(&dev->addr_list_lock, &bpq_netdev_addr_lock_key);
  115. netdev_for_each_tx_queue(dev, bpq_set_lockdep_class_one, NULL);
  116. }
  117. /* ------------------------------------------------------------------------ */
  118. /*
  119. * Get the ethernet device for a BPQ device
  120. */
  121. static inline struct net_device *bpq_get_ether_dev(struct net_device *dev)
  122. {
  123. struct bpqdev *bpq = netdev_priv(dev);
  124. return bpq ? bpq->ethdev : NULL;
  125. }
  126. /*
  127. * Get the BPQ device for the ethernet device
  128. */
  129. static inline struct net_device *bpq_get_ax25_dev(struct net_device *dev)
  130. {
  131. struct bpqdev *bpq;
  132. list_for_each_entry_rcu(bpq, &bpq_devices, bpq_list,
  133. lockdep_rtnl_is_held()) {
  134. if (bpq->ethdev == dev)
  135. return bpq->axdev;
  136. }
  137. return NULL;
  138. }
  139. static inline int dev_is_ethdev(struct net_device *dev)
  140. {
  141. return dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5);
  142. }
  143. /* ------------------------------------------------------------------------ */
  144. /*
  145. * Receive an AX.25 frame via an ethernet interface.
  146. */
  147. static int bpq_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *ptype, struct net_device *orig_dev)
  148. {
  149. int len;
  150. char * ptr;
  151. struct ethhdr *eth;
  152. struct bpqdev *bpq;
  153. if (!net_eq(dev_net(dev), &init_net))
  154. goto drop;
  155. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  156. return NET_RX_DROP;
  157. if (!pskb_may_pull(skb, sizeof(struct ethhdr)))
  158. goto drop;
  159. rcu_read_lock();
  160. dev = bpq_get_ax25_dev(dev);
  161. if (dev == NULL || !netif_running(dev))
  162. goto drop_unlock;
  163. /*
  164. * if we want to accept frames from just one ethernet device
  165. * we check the source address of the sender.
  166. */
  167. bpq = netdev_priv(dev);
  168. eth = eth_hdr(skb);
  169. if (!(bpq->acpt_addr[0] & 0x01) &&
  170. !ether_addr_equal(eth->h_source, bpq->acpt_addr))
  171. goto drop_unlock;
  172. if (skb_cow(skb, sizeof(struct ethhdr)))
  173. goto drop_unlock;
  174. len = skb->data[0] + skb->data[1] * 256 - 5;
  175. skb_pull(skb, 2); /* Remove the length bytes */
  176. skb_trim(skb, len); /* Set the length of the data */
  177. dev->stats.rx_packets++;
  178. dev->stats.rx_bytes += len;
  179. ptr = skb_push(skb, 1);
  180. *ptr = 0;
  181. skb->protocol = ax25_type_trans(skb, dev);
  182. netif_rx(skb);
  183. unlock:
  184. rcu_read_unlock();
  185. return 0;
  186. drop_unlock:
  187. kfree_skb(skb);
  188. goto unlock;
  189. drop:
  190. kfree_skb(skb);
  191. return 0;
  192. }
  193. /*
  194. * Send an AX.25 frame via an ethernet interface
  195. */
  196. static netdev_tx_t bpq_xmit(struct sk_buff *skb, struct net_device *dev)
  197. {
  198. unsigned char *ptr;
  199. struct bpqdev *bpq;
  200. struct net_device *orig_dev;
  201. int size;
  202. if (skb->protocol == htons(ETH_P_IP))
  203. return ax25_ip_xmit(skb);
  204. /*
  205. * Just to be *really* sure not to send anything if the interface
  206. * is down, the ethernet device may have gone.
  207. */
  208. if (!netif_running(dev)) {
  209. kfree_skb(skb);
  210. return NETDEV_TX_OK;
  211. }
  212. skb_pull(skb, 1); /* Drop KISS byte */
  213. size = skb->len;
  214. /*
  215. * We're about to mess with the skb which may still shared with the
  216. * generic networking code so unshare and ensure it's got enough
  217. * space for the BPQ headers.
  218. */
  219. if (skb_cow(skb, AX25_BPQ_HEADER_LEN)) {
  220. if (net_ratelimit())
  221. pr_err("bpqether: out of memory\n");
  222. kfree_skb(skb);
  223. return NETDEV_TX_OK;
  224. }
  225. ptr = skb_push(skb, 2); /* Make space for length */
  226. *ptr++ = (size + 5) % 256;
  227. *ptr++ = (size + 5) / 256;
  228. bpq = netdev_priv(dev);
  229. orig_dev = dev;
  230. if ((dev = bpq_get_ether_dev(dev)) == NULL) {
  231. orig_dev->stats.tx_dropped++;
  232. kfree_skb(skb);
  233. return NETDEV_TX_OK;
  234. }
  235. skb->protocol = ax25_type_trans(skb, dev);
  236. skb_reset_network_header(skb);
  237. dev_hard_header(skb, dev, ETH_P_BPQ, bpq->dest_addr, NULL, 0);
  238. dev->stats.tx_packets++;
  239. dev->stats.tx_bytes+=skb->len;
  240. dev_queue_xmit(skb);
  241. netif_wake_queue(dev);
  242. return NETDEV_TX_OK;
  243. }
  244. /*
  245. * Set AX.25 callsign
  246. */
  247. static int bpq_set_mac_address(struct net_device *dev, void *addr)
  248. {
  249. struct sockaddr *sa = (struct sockaddr *)addr;
  250. dev_addr_set(dev, sa->sa_data);
  251. return 0;
  252. }
  253. /* Ioctl commands
  254. *
  255. * SIOCSBPQETHOPT reserved for enhancements
  256. * SIOCSBPQETHADDR set the destination and accepted
  257. * source ethernet address (broadcast
  258. * or multicast: accept all)
  259. */
  260. static int bpq_siocdevprivate(struct net_device *dev, struct ifreq *ifr,
  261. void __user *data, int cmd)
  262. {
  263. struct bpq_ethaddr __user *ethaddr = data;
  264. struct bpqdev *bpq = netdev_priv(dev);
  265. struct bpq_req req;
  266. if (!capable(CAP_NET_ADMIN))
  267. return -EPERM;
  268. switch (cmd) {
  269. case SIOCSBPQETHOPT:
  270. if (copy_from_user(&req, data, sizeof(struct bpq_req)))
  271. return -EFAULT;
  272. switch (req.cmd) {
  273. case SIOCGBPQETHPARAM:
  274. case SIOCSBPQETHPARAM:
  275. default:
  276. return -EINVAL;
  277. }
  278. break;
  279. case SIOCSBPQETHADDR:
  280. if (copy_from_user(bpq->dest_addr, ethaddr->destination, ETH_ALEN))
  281. return -EFAULT;
  282. if (copy_from_user(bpq->acpt_addr, ethaddr->accept, ETH_ALEN))
  283. return -EFAULT;
  284. break;
  285. default:
  286. return -EINVAL;
  287. }
  288. return 0;
  289. }
  290. /*
  291. * open/close a device
  292. */
  293. static int bpq_open(struct net_device *dev)
  294. {
  295. netif_start_queue(dev);
  296. return 0;
  297. }
  298. static int bpq_close(struct net_device *dev)
  299. {
  300. netif_stop_queue(dev);
  301. return 0;
  302. }
  303. /* ------------------------------------------------------------------------ */
  304. #ifdef CONFIG_PROC_FS
  305. /*
  306. * Proc filesystem
  307. */
  308. static void *bpq_seq_start(struct seq_file *seq, loff_t *pos)
  309. __acquires(RCU)
  310. {
  311. int i = 1;
  312. struct bpqdev *bpqdev;
  313. rcu_read_lock();
  314. if (*pos == 0)
  315. return SEQ_START_TOKEN;
  316. list_for_each_entry_rcu(bpqdev, &bpq_devices, bpq_list) {
  317. if (i == *pos)
  318. return bpqdev;
  319. }
  320. return NULL;
  321. }
  322. static void *bpq_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  323. {
  324. struct list_head *p;
  325. struct bpqdev *bpqdev = v;
  326. ++*pos;
  327. if (v == SEQ_START_TOKEN)
  328. p = rcu_dereference(list_next_rcu(&bpq_devices));
  329. else
  330. p = rcu_dereference(list_next_rcu(&bpqdev->bpq_list));
  331. return (p == &bpq_devices) ? NULL
  332. : list_entry(p, struct bpqdev, bpq_list);
  333. }
  334. static void bpq_seq_stop(struct seq_file *seq, void *v)
  335. __releases(RCU)
  336. {
  337. rcu_read_unlock();
  338. }
  339. static int bpq_seq_show(struct seq_file *seq, void *v)
  340. {
  341. if (v == SEQ_START_TOKEN)
  342. seq_puts(seq,
  343. "dev ether destination accept from\n");
  344. else {
  345. const struct bpqdev *bpqdev = v;
  346. seq_printf(seq, "%-5s %-10s %pM ",
  347. bpqdev->axdev->name, bpqdev->ethdev->name,
  348. bpqdev->dest_addr);
  349. if (is_multicast_ether_addr(bpqdev->acpt_addr))
  350. seq_printf(seq, "*\n");
  351. else
  352. seq_printf(seq, "%pM\n", bpqdev->acpt_addr);
  353. }
  354. return 0;
  355. }
  356. static const struct seq_operations bpq_seqops = {
  357. .start = bpq_seq_start,
  358. .next = bpq_seq_next,
  359. .stop = bpq_seq_stop,
  360. .show = bpq_seq_show,
  361. };
  362. #endif
  363. /* ------------------------------------------------------------------------ */
  364. static const struct net_device_ops bpq_netdev_ops = {
  365. .ndo_open = bpq_open,
  366. .ndo_stop = bpq_close,
  367. .ndo_start_xmit = bpq_xmit,
  368. .ndo_set_mac_address = bpq_set_mac_address,
  369. .ndo_siocdevprivate = bpq_siocdevprivate,
  370. };
  371. static void bpq_setup(struct net_device *dev)
  372. {
  373. dev->netdev_ops = &bpq_netdev_ops;
  374. dev->needs_free_netdev = true;
  375. dev->flags = 0;
  376. dev->features = NETIF_F_LLTX; /* Allow recursion */
  377. #if IS_ENABLED(CONFIG_AX25)
  378. dev->header_ops = &ax25_header_ops;
  379. #endif
  380. dev->type = ARPHRD_AX25;
  381. dev->hard_header_len = AX25_MAX_HEADER_LEN + AX25_BPQ_HEADER_LEN;
  382. dev->mtu = AX25_DEF_PACLEN;
  383. dev->addr_len = AX25_ADDR_LEN;
  384. memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
  385. dev_addr_set(dev, (u8 *)&ax25_defaddr);
  386. }
  387. /*
  388. * Setup a new device.
  389. */
  390. static int bpq_new_device(struct net_device *edev)
  391. {
  392. int err;
  393. struct net_device *ndev;
  394. struct bpqdev *bpq;
  395. ndev = alloc_netdev(sizeof(struct bpqdev), "bpq%d", NET_NAME_UNKNOWN,
  396. bpq_setup);
  397. if (!ndev)
  398. return -ENOMEM;
  399. bpq = netdev_priv(ndev);
  400. dev_hold(edev);
  401. bpq->ethdev = edev;
  402. bpq->axdev = ndev;
  403. eth_broadcast_addr(bpq->dest_addr);
  404. eth_broadcast_addr(bpq->acpt_addr);
  405. err = register_netdevice(ndev);
  406. if (err)
  407. goto error;
  408. bpq_set_lockdep_class(ndev);
  409. /* List protected by RTNL */
  410. list_add_rcu(&bpq->bpq_list, &bpq_devices);
  411. return 0;
  412. error:
  413. dev_put(edev);
  414. free_netdev(ndev);
  415. return err;
  416. }
  417. static void bpq_free_device(struct net_device *ndev)
  418. {
  419. struct bpqdev *bpq = netdev_priv(ndev);
  420. dev_put(bpq->ethdev);
  421. list_del_rcu(&bpq->bpq_list);
  422. unregister_netdevice(ndev);
  423. }
  424. /*
  425. * Handle device status changes.
  426. */
  427. static int bpq_device_event(struct notifier_block *this,
  428. unsigned long event, void *ptr)
  429. {
  430. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  431. if (!net_eq(dev_net(dev), &init_net))
  432. return NOTIFY_DONE;
  433. if (!dev_is_ethdev(dev) && !bpq_get_ax25_dev(dev))
  434. return NOTIFY_DONE;
  435. switch (event) {
  436. case NETDEV_UP: /* new ethernet device -> new BPQ interface */
  437. if (bpq_get_ax25_dev(dev) == NULL)
  438. bpq_new_device(dev);
  439. break;
  440. case NETDEV_DOWN: /* ethernet device closed -> close BPQ interface */
  441. if ((dev = bpq_get_ax25_dev(dev)) != NULL)
  442. dev_close(dev);
  443. break;
  444. case NETDEV_UNREGISTER: /* ethernet device removed -> free BPQ interface */
  445. if ((dev = bpq_get_ax25_dev(dev)) != NULL)
  446. bpq_free_device(dev);
  447. break;
  448. default:
  449. break;
  450. }
  451. return NOTIFY_DONE;
  452. }
  453. /* ------------------------------------------------------------------------ */
  454. /*
  455. * Initialize driver. To be called from af_ax25 if not compiled as a
  456. * module
  457. */
  458. static int __init bpq_init_driver(void)
  459. {
  460. #ifdef CONFIG_PROC_FS
  461. if (!proc_create_seq("bpqether", 0444, init_net.proc_net, &bpq_seqops)) {
  462. printk(KERN_ERR
  463. "bpq: cannot create /proc/net/bpqether entry.\n");
  464. return -ENOENT;
  465. }
  466. #endif /* CONFIG_PROC_FS */
  467. dev_add_pack(&bpq_packet_type);
  468. register_netdevice_notifier(&bpq_dev_notifier);
  469. printk(banner);
  470. return 0;
  471. }
  472. static void __exit bpq_cleanup_driver(void)
  473. {
  474. struct bpqdev *bpq;
  475. dev_remove_pack(&bpq_packet_type);
  476. unregister_netdevice_notifier(&bpq_dev_notifier);
  477. remove_proc_entry("bpqether", init_net.proc_net);
  478. rtnl_lock();
  479. while (!list_empty(&bpq_devices)) {
  480. bpq = list_entry(bpq_devices.next, struct bpqdev, bpq_list);
  481. bpq_free_device(bpq->axdev);
  482. }
  483. rtnl_unlock();
  484. }
  485. MODULE_AUTHOR("Joerg Reuter DL1BKE <[email protected]>");
  486. MODULE_DESCRIPTION("Transmit and receive AX.25 packets over Ethernet");
  487. MODULE_LICENSE("GPL");
  488. module_init(bpq_init_driver);
  489. module_exit(bpq_cleanup_driver);