macvtap.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. #include <linux/etherdevice.h>
  2. #include <linux/if_macvlan.h>
  3. #include <linux/if_vlan.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/nsproxy.h>
  6. #include <linux/compat.h>
  7. #include <linux/if_tun.h>
  8. #include <linux/module.h>
  9. #include <linux/skbuff.h>
  10. #include <linux/cache.h>
  11. #include <linux/sched.h>
  12. #include <linux/types.h>
  13. #include <linux/slab.h>
  14. #include <linux/wait.h>
  15. #include <linux/cdev.h>
  16. #include <linux/idr.h>
  17. #include <linux/fs.h>
  18. #include <linux/uio.h>
  19. #include <net/net_namespace.h>
  20. #include <net/rtnetlink.h>
  21. #include <net/sock.h>
  22. #include <linux/virtio_net.h>
  23. /*
  24. * A macvtap queue is the central object of this driver, it connects
  25. * an open character device to a macvlan interface. There can be
  26. * multiple queues on one interface, which map back to queues
  27. * implemented in hardware on the underlying device.
  28. *
  29. * macvtap_proto is used to allocate queues through the sock allocation
  30. * mechanism.
  31. *
  32. */
  33. struct macvtap_queue {
  34. struct sock sk;
  35. struct socket sock;
  36. struct socket_wq wq;
  37. int vnet_hdr_sz;
  38. struct macvlan_dev __rcu *vlan;
  39. struct file *file;
  40. unsigned int flags;
  41. u16 queue_index;
  42. bool enabled;
  43. struct list_head next;
  44. };
  45. #define MACVTAP_FEATURES (IFF_VNET_HDR | IFF_MULTI_QUEUE)
  46. #define MACVTAP_VNET_LE 0x80000000
  47. #define MACVTAP_VNET_BE 0x40000000
  48. #ifdef CONFIG_TUN_VNET_CROSS_LE
  49. static inline bool macvtap_legacy_is_little_endian(struct macvtap_queue *q)
  50. {
  51. return q->flags & MACVTAP_VNET_BE ? false :
  52. virtio_legacy_is_little_endian();
  53. }
  54. static long macvtap_get_vnet_be(struct macvtap_queue *q, int __user *sp)
  55. {
  56. int s = !!(q->flags & MACVTAP_VNET_BE);
  57. if (put_user(s, sp))
  58. return -EFAULT;
  59. return 0;
  60. }
  61. static long macvtap_set_vnet_be(struct macvtap_queue *q, int __user *sp)
  62. {
  63. int s;
  64. if (get_user(s, sp))
  65. return -EFAULT;
  66. if (s)
  67. q->flags |= MACVTAP_VNET_BE;
  68. else
  69. q->flags &= ~MACVTAP_VNET_BE;
  70. return 0;
  71. }
  72. #else
  73. static inline bool macvtap_legacy_is_little_endian(struct macvtap_queue *q)
  74. {
  75. return virtio_legacy_is_little_endian();
  76. }
  77. static long macvtap_get_vnet_be(struct macvtap_queue *q, int __user *argp)
  78. {
  79. return -EINVAL;
  80. }
  81. static long macvtap_set_vnet_be(struct macvtap_queue *q, int __user *argp)
  82. {
  83. return -EINVAL;
  84. }
  85. #endif /* CONFIG_TUN_VNET_CROSS_LE */
  86. static inline bool macvtap_is_little_endian(struct macvtap_queue *q)
  87. {
  88. return q->flags & MACVTAP_VNET_LE ||
  89. macvtap_legacy_is_little_endian(q);
  90. }
  91. static inline u16 macvtap16_to_cpu(struct macvtap_queue *q, __virtio16 val)
  92. {
  93. return __virtio16_to_cpu(macvtap_is_little_endian(q), val);
  94. }
  95. static inline __virtio16 cpu_to_macvtap16(struct macvtap_queue *q, u16 val)
  96. {
  97. return __cpu_to_virtio16(macvtap_is_little_endian(q), val);
  98. }
  99. static struct proto macvtap_proto = {
  100. .name = "macvtap",
  101. .owner = THIS_MODULE,
  102. .obj_size = sizeof (struct macvtap_queue),
  103. };
  104. /*
  105. * Variables for dealing with macvtaps device numbers.
  106. */
  107. static dev_t macvtap_major;
  108. #define MACVTAP_NUM_DEVS (1U << MINORBITS)
  109. static DEFINE_MUTEX(minor_lock);
  110. static DEFINE_IDR(minor_idr);
  111. #define GOODCOPY_LEN 128
  112. static struct class *macvtap_class;
  113. static struct cdev macvtap_cdev;
  114. static const struct proto_ops macvtap_socket_ops;
  115. #define TUN_OFFLOADS (NETIF_F_HW_CSUM | NETIF_F_TSO_ECN | NETIF_F_TSO | \
  116. NETIF_F_TSO6 | NETIF_F_UFO)
  117. #define RX_OFFLOADS (NETIF_F_GRO | NETIF_F_LRO)
  118. #define TAP_FEATURES (NETIF_F_GSO | NETIF_F_SG)
  119. static struct macvlan_dev *macvtap_get_vlan_rcu(const struct net_device *dev)
  120. {
  121. return rcu_dereference(dev->rx_handler_data);
  122. }
  123. /*
  124. * RCU usage:
  125. * The macvtap_queue and the macvlan_dev are loosely coupled, the
  126. * pointers from one to the other can only be read while rcu_read_lock
  127. * or rtnl is held.
  128. *
  129. * Both the file and the macvlan_dev hold a reference on the macvtap_queue
  130. * through sock_hold(&q->sk). When the macvlan_dev goes away first,
  131. * q->vlan becomes inaccessible. When the files gets closed,
  132. * macvtap_get_queue() fails.
  133. *
  134. * There may still be references to the struct sock inside of the
  135. * queue from outbound SKBs, but these never reference back to the
  136. * file or the dev. The data structure is freed through __sk_free
  137. * when both our references and any pending SKBs are gone.
  138. */
  139. static int macvtap_enable_queue(struct net_device *dev, struct file *file,
  140. struct macvtap_queue *q)
  141. {
  142. struct macvlan_dev *vlan = netdev_priv(dev);
  143. int err = -EINVAL;
  144. ASSERT_RTNL();
  145. if (q->enabled)
  146. goto out;
  147. err = 0;
  148. rcu_assign_pointer(vlan->taps[vlan->numvtaps], q);
  149. q->queue_index = vlan->numvtaps;
  150. q->enabled = true;
  151. vlan->numvtaps++;
  152. out:
  153. return err;
  154. }
  155. /* Requires RTNL */
  156. static int macvtap_set_queue(struct net_device *dev, struct file *file,
  157. struct macvtap_queue *q)
  158. {
  159. struct macvlan_dev *vlan = netdev_priv(dev);
  160. if (vlan->numqueues == MAX_MACVTAP_QUEUES)
  161. return -EBUSY;
  162. rcu_assign_pointer(q->vlan, vlan);
  163. rcu_assign_pointer(vlan->taps[vlan->numvtaps], q);
  164. sock_hold(&q->sk);
  165. q->file = file;
  166. q->queue_index = vlan->numvtaps;
  167. q->enabled = true;
  168. file->private_data = q;
  169. list_add_tail(&q->next, &vlan->queue_list);
  170. vlan->numvtaps++;
  171. vlan->numqueues++;
  172. return 0;
  173. }
  174. static int macvtap_disable_queue(struct macvtap_queue *q)
  175. {
  176. struct macvlan_dev *vlan;
  177. struct macvtap_queue *nq;
  178. ASSERT_RTNL();
  179. if (!q->enabled)
  180. return -EINVAL;
  181. vlan = rtnl_dereference(q->vlan);
  182. if (vlan) {
  183. int index = q->queue_index;
  184. BUG_ON(index >= vlan->numvtaps);
  185. nq = rtnl_dereference(vlan->taps[vlan->numvtaps - 1]);
  186. nq->queue_index = index;
  187. rcu_assign_pointer(vlan->taps[index], nq);
  188. RCU_INIT_POINTER(vlan->taps[vlan->numvtaps - 1], NULL);
  189. q->enabled = false;
  190. vlan->numvtaps--;
  191. }
  192. return 0;
  193. }
  194. /*
  195. * The file owning the queue got closed, give up both
  196. * the reference that the files holds as well as the
  197. * one from the macvlan_dev if that still exists.
  198. *
  199. * Using the spinlock makes sure that we don't get
  200. * to the queue again after destroying it.
  201. */
  202. static void macvtap_put_queue(struct macvtap_queue *q)
  203. {
  204. struct macvlan_dev *vlan;
  205. rtnl_lock();
  206. vlan = rtnl_dereference(q->vlan);
  207. if (vlan) {
  208. if (q->enabled)
  209. BUG_ON(macvtap_disable_queue(q));
  210. vlan->numqueues--;
  211. RCU_INIT_POINTER(q->vlan, NULL);
  212. sock_put(&q->sk);
  213. list_del_init(&q->next);
  214. }
  215. rtnl_unlock();
  216. synchronize_rcu();
  217. sock_put(&q->sk);
  218. }
  219. /*
  220. * Select a queue based on the rxq of the device on which this packet
  221. * arrived. If the incoming device is not mq, calculate a flow hash
  222. * to select a queue. If all fails, find the first available queue.
  223. * Cache vlan->numvtaps since it can become zero during the execution
  224. * of this function.
  225. */
  226. static struct macvtap_queue *macvtap_get_queue(struct net_device *dev,
  227. struct sk_buff *skb)
  228. {
  229. struct macvlan_dev *vlan = netdev_priv(dev);
  230. struct macvtap_queue *tap = NULL;
  231. /* Access to taps array is protected by rcu, but access to numvtaps
  232. * isn't. Below we use it to lookup a queue, but treat it as a hint
  233. * and validate that the result isn't NULL - in case we are
  234. * racing against queue removal.
  235. */
  236. int numvtaps = ACCESS_ONCE(vlan->numvtaps);
  237. __u32 rxq;
  238. if (!numvtaps)
  239. goto out;
  240. /* Check if we can use flow to select a queue */
  241. rxq = skb_get_hash(skb);
  242. if (rxq) {
  243. tap = rcu_dereference(vlan->taps[rxq % numvtaps]);
  244. goto out;
  245. }
  246. if (likely(skb_rx_queue_recorded(skb))) {
  247. rxq = skb_get_rx_queue(skb);
  248. while (unlikely(rxq >= numvtaps))
  249. rxq -= numvtaps;
  250. tap = rcu_dereference(vlan->taps[rxq]);
  251. goto out;
  252. }
  253. tap = rcu_dereference(vlan->taps[0]);
  254. out:
  255. return tap;
  256. }
  257. /*
  258. * The net_device is going away, give up the reference
  259. * that it holds on all queues and safely set the pointer
  260. * from the queues to NULL.
  261. */
  262. static void macvtap_del_queues(struct net_device *dev)
  263. {
  264. struct macvlan_dev *vlan = netdev_priv(dev);
  265. struct macvtap_queue *q, *tmp;
  266. ASSERT_RTNL();
  267. list_for_each_entry_safe(q, tmp, &vlan->queue_list, next) {
  268. list_del_init(&q->next);
  269. RCU_INIT_POINTER(q->vlan, NULL);
  270. if (q->enabled)
  271. vlan->numvtaps--;
  272. vlan->numqueues--;
  273. sock_put(&q->sk);
  274. }
  275. BUG_ON(vlan->numvtaps);
  276. BUG_ON(vlan->numqueues);
  277. /* guarantee that any future macvtap_set_queue will fail */
  278. vlan->numvtaps = MAX_MACVTAP_QUEUES;
  279. }
  280. static rx_handler_result_t macvtap_handle_frame(struct sk_buff **pskb)
  281. {
  282. struct sk_buff *skb = *pskb;
  283. struct net_device *dev = skb->dev;
  284. struct macvlan_dev *vlan;
  285. struct macvtap_queue *q;
  286. netdev_features_t features = TAP_FEATURES;
  287. vlan = macvtap_get_vlan_rcu(dev);
  288. if (!vlan)
  289. return RX_HANDLER_PASS;
  290. q = macvtap_get_queue(dev, skb);
  291. if (!q)
  292. return RX_HANDLER_PASS;
  293. if (skb_queue_len(&q->sk.sk_receive_queue) >= dev->tx_queue_len)
  294. goto drop;
  295. skb_push(skb, ETH_HLEN);
  296. /* Apply the forward feature mask so that we perform segmentation
  297. * according to users wishes. This only works if VNET_HDR is
  298. * enabled.
  299. */
  300. if (q->flags & IFF_VNET_HDR)
  301. features |= vlan->tap_features;
  302. if (netif_needs_gso(skb, features)) {
  303. struct sk_buff *segs = __skb_gso_segment(skb, features, false);
  304. if (IS_ERR(segs))
  305. goto drop;
  306. if (!segs) {
  307. skb_queue_tail(&q->sk.sk_receive_queue, skb);
  308. goto wake_up;
  309. }
  310. kfree_skb(skb);
  311. while (segs) {
  312. struct sk_buff *nskb = segs->next;
  313. segs->next = NULL;
  314. skb_queue_tail(&q->sk.sk_receive_queue, segs);
  315. segs = nskb;
  316. }
  317. } else {
  318. /* If we receive a partial checksum and the tap side
  319. * doesn't support checksum offload, compute the checksum.
  320. * Note: it doesn't matter which checksum feature to
  321. * check, we either support them all or none.
  322. */
  323. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  324. !(features & NETIF_F_ALL_CSUM) &&
  325. skb_checksum_help(skb))
  326. goto drop;
  327. skb_queue_tail(&q->sk.sk_receive_queue, skb);
  328. }
  329. wake_up:
  330. wake_up_interruptible_poll(sk_sleep(&q->sk), POLLIN | POLLRDNORM | POLLRDBAND);
  331. return RX_HANDLER_CONSUMED;
  332. drop:
  333. /* Count errors/drops only here, thus don't care about args. */
  334. macvlan_count_rx(vlan, 0, 0, 0);
  335. kfree_skb(skb);
  336. return RX_HANDLER_CONSUMED;
  337. }
  338. static int macvtap_get_minor(struct macvlan_dev *vlan)
  339. {
  340. int retval = -ENOMEM;
  341. mutex_lock(&minor_lock);
  342. retval = idr_alloc(&minor_idr, vlan, 1, MACVTAP_NUM_DEVS, GFP_KERNEL);
  343. if (retval >= 0) {
  344. vlan->minor = retval;
  345. } else if (retval == -ENOSPC) {
  346. printk(KERN_ERR "too many macvtap devices\n");
  347. retval = -EINVAL;
  348. }
  349. mutex_unlock(&minor_lock);
  350. return retval < 0 ? retval : 0;
  351. }
  352. static void macvtap_free_minor(struct macvlan_dev *vlan)
  353. {
  354. mutex_lock(&minor_lock);
  355. if (vlan->minor) {
  356. idr_remove(&minor_idr, vlan->minor);
  357. vlan->minor = 0;
  358. }
  359. mutex_unlock(&minor_lock);
  360. }
  361. static struct net_device *dev_get_by_macvtap_minor(int minor)
  362. {
  363. struct net_device *dev = NULL;
  364. struct macvlan_dev *vlan;
  365. mutex_lock(&minor_lock);
  366. vlan = idr_find(&minor_idr, minor);
  367. if (vlan) {
  368. dev = vlan->dev;
  369. dev_hold(dev);
  370. }
  371. mutex_unlock(&minor_lock);
  372. return dev;
  373. }
  374. static int macvtap_newlink(struct net *src_net,
  375. struct net_device *dev,
  376. struct nlattr *tb[],
  377. struct nlattr *data[])
  378. {
  379. struct macvlan_dev *vlan = netdev_priv(dev);
  380. int err;
  381. INIT_LIST_HEAD(&vlan->queue_list);
  382. /* Since macvlan supports all offloads by default, make
  383. * tap support all offloads also.
  384. */
  385. vlan->tap_features = TUN_OFFLOADS;
  386. err = netdev_rx_handler_register(dev, macvtap_handle_frame, vlan);
  387. if (err)
  388. return err;
  389. /* Don't put anything that may fail after macvlan_common_newlink
  390. * because we can't undo what it does.
  391. */
  392. return macvlan_common_newlink(src_net, dev, tb, data);
  393. }
  394. static void macvtap_dellink(struct net_device *dev,
  395. struct list_head *head)
  396. {
  397. netdev_rx_handler_unregister(dev);
  398. macvtap_del_queues(dev);
  399. macvlan_dellink(dev, head);
  400. }
  401. static void macvtap_setup(struct net_device *dev)
  402. {
  403. macvlan_common_setup(dev);
  404. dev->tx_queue_len = TUN_READQ_SIZE;
  405. }
  406. static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
  407. .kind = "macvtap",
  408. .setup = macvtap_setup,
  409. .newlink = macvtap_newlink,
  410. .dellink = macvtap_dellink,
  411. };
  412. static void macvtap_sock_write_space(struct sock *sk)
  413. {
  414. wait_queue_head_t *wqueue;
  415. if (!sock_writeable(sk) ||
  416. !test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
  417. return;
  418. wqueue = sk_sleep(sk);
  419. if (wqueue && waitqueue_active(wqueue))
  420. wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND);
  421. }
  422. static void macvtap_sock_destruct(struct sock *sk)
  423. {
  424. skb_queue_purge(&sk->sk_receive_queue);
  425. }
  426. static int macvtap_open(struct inode *inode, struct file *file)
  427. {
  428. struct net *net = current->nsproxy->net_ns;
  429. struct net_device *dev;
  430. struct macvtap_queue *q;
  431. int err = -ENODEV;
  432. rtnl_lock();
  433. dev = dev_get_by_macvtap_minor(iminor(inode));
  434. if (!dev)
  435. goto out;
  436. err = -ENOMEM;
  437. q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
  438. &macvtap_proto, 0);
  439. if (!q)
  440. goto out;
  441. RCU_INIT_POINTER(q->sock.wq, &q->wq);
  442. init_waitqueue_head(&q->wq.wait);
  443. q->sock.type = SOCK_RAW;
  444. q->sock.state = SS_CONNECTED;
  445. q->sock.file = file;
  446. q->sock.ops = &macvtap_socket_ops;
  447. sock_init_data(&q->sock, &q->sk);
  448. q->sk.sk_write_space = macvtap_sock_write_space;
  449. q->sk.sk_destruct = macvtap_sock_destruct;
  450. q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
  451. q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
  452. /*
  453. * so far only KVM virtio_net uses macvtap, enable zero copy between
  454. * guest kernel and host kernel when lower device supports zerocopy
  455. *
  456. * The macvlan supports zerocopy iff the lower device supports zero
  457. * copy so we don't have to look at the lower device directly.
  458. */
  459. if ((dev->features & NETIF_F_HIGHDMA) && (dev->features & NETIF_F_SG))
  460. sock_set_flag(&q->sk, SOCK_ZEROCOPY);
  461. err = macvtap_set_queue(dev, file, q);
  462. if (err)
  463. sock_put(&q->sk);
  464. out:
  465. if (dev)
  466. dev_put(dev);
  467. rtnl_unlock();
  468. return err;
  469. }
  470. static int macvtap_release(struct inode *inode, struct file *file)
  471. {
  472. struct macvtap_queue *q = file->private_data;
  473. macvtap_put_queue(q);
  474. return 0;
  475. }
  476. static unsigned int macvtap_poll(struct file *file, poll_table * wait)
  477. {
  478. struct macvtap_queue *q = file->private_data;
  479. unsigned int mask = POLLERR;
  480. if (!q)
  481. goto out;
  482. mask = 0;
  483. poll_wait(file, &q->wq.wait, wait);
  484. if (!skb_queue_empty(&q->sk.sk_receive_queue))
  485. mask |= POLLIN | POLLRDNORM;
  486. if (sock_writeable(&q->sk) ||
  487. (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &q->sock.flags) &&
  488. sock_writeable(&q->sk)))
  489. mask |= POLLOUT | POLLWRNORM;
  490. out:
  491. return mask;
  492. }
  493. static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,
  494. size_t len, size_t linear,
  495. int noblock, int *err)
  496. {
  497. struct sk_buff *skb;
  498. /* Under a page? Don't bother with paged skb. */
  499. if (prepad + len < PAGE_SIZE || !linear)
  500. linear = len;
  501. skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
  502. err, 0);
  503. if (!skb)
  504. return NULL;
  505. skb_reserve(skb, prepad);
  506. skb_put(skb, linear);
  507. skb->data_len = len - linear;
  508. skb->len += len - linear;
  509. return skb;
  510. }
  511. /*
  512. * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
  513. * be shared with the tun/tap driver.
  514. */
  515. static int macvtap_skb_from_vnet_hdr(struct macvtap_queue *q,
  516. struct sk_buff *skb,
  517. struct virtio_net_hdr *vnet_hdr)
  518. {
  519. unsigned short gso_type = 0;
  520. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  521. switch (vnet_hdr->gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
  522. case VIRTIO_NET_HDR_GSO_TCPV4:
  523. gso_type = SKB_GSO_TCPV4;
  524. break;
  525. case VIRTIO_NET_HDR_GSO_TCPV6:
  526. gso_type = SKB_GSO_TCPV6;
  527. break;
  528. case VIRTIO_NET_HDR_GSO_UDP:
  529. gso_type = SKB_GSO_UDP;
  530. break;
  531. default:
  532. return -EINVAL;
  533. }
  534. if (vnet_hdr->gso_type & VIRTIO_NET_HDR_GSO_ECN)
  535. gso_type |= SKB_GSO_TCP_ECN;
  536. if (vnet_hdr->gso_size == 0)
  537. return -EINVAL;
  538. }
  539. if (vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  540. if (!skb_partial_csum_set(skb, macvtap16_to_cpu(q, vnet_hdr->csum_start),
  541. macvtap16_to_cpu(q, vnet_hdr->csum_offset)))
  542. return -EINVAL;
  543. }
  544. if (vnet_hdr->gso_type != VIRTIO_NET_HDR_GSO_NONE) {
  545. skb_shinfo(skb)->gso_size = macvtap16_to_cpu(q, vnet_hdr->gso_size);
  546. skb_shinfo(skb)->gso_type = gso_type;
  547. /* Header must be checked, and gso_segs computed. */
  548. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  549. skb_shinfo(skb)->gso_segs = 0;
  550. }
  551. return 0;
  552. }
  553. static void macvtap_skb_to_vnet_hdr(struct macvtap_queue *q,
  554. const struct sk_buff *skb,
  555. struct virtio_net_hdr *vnet_hdr)
  556. {
  557. memset(vnet_hdr, 0, sizeof(*vnet_hdr));
  558. if (skb_is_gso(skb)) {
  559. struct skb_shared_info *sinfo = skb_shinfo(skb);
  560. /* This is a hint as to how much should be linear. */
  561. vnet_hdr->hdr_len = cpu_to_macvtap16(q, skb_headlen(skb));
  562. vnet_hdr->gso_size = cpu_to_macvtap16(q, sinfo->gso_size);
  563. if (sinfo->gso_type & SKB_GSO_TCPV4)
  564. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
  565. else if (sinfo->gso_type & SKB_GSO_TCPV6)
  566. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
  567. else if (sinfo->gso_type & SKB_GSO_UDP)
  568. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_UDP;
  569. else
  570. BUG();
  571. if (sinfo->gso_type & SKB_GSO_TCP_ECN)
  572. vnet_hdr->gso_type |= VIRTIO_NET_HDR_GSO_ECN;
  573. } else
  574. vnet_hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
  575. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  576. vnet_hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
  577. if (skb_vlan_tag_present(skb))
  578. vnet_hdr->csum_start = cpu_to_macvtap16(q,
  579. skb_checksum_start_offset(skb) + VLAN_HLEN);
  580. else
  581. vnet_hdr->csum_start = cpu_to_macvtap16(q,
  582. skb_checksum_start_offset(skb));
  583. vnet_hdr->csum_offset = cpu_to_macvtap16(q, skb->csum_offset);
  584. } else if (skb->ip_summed == CHECKSUM_UNNECESSARY) {
  585. vnet_hdr->flags = VIRTIO_NET_HDR_F_DATA_VALID;
  586. } /* else everything is zero */
  587. }
  588. /* Neighbour code has some assumptions on HH_DATA_MOD alignment */
  589. #define MACVTAP_RESERVE HH_DATA_OFF(ETH_HLEN)
  590. /* Get packet from user space buffer */
  591. static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
  592. struct iov_iter *from, int noblock)
  593. {
  594. int good_linear = SKB_MAX_HEAD(MACVTAP_RESERVE);
  595. struct sk_buff *skb;
  596. struct macvlan_dev *vlan;
  597. unsigned long total_len = iov_iter_count(from);
  598. unsigned long len = total_len;
  599. int err;
  600. struct virtio_net_hdr vnet_hdr = { 0 };
  601. int vnet_hdr_len = 0;
  602. int copylen = 0;
  603. bool zerocopy = false;
  604. size_t linear;
  605. ssize_t n;
  606. if (q->flags & IFF_VNET_HDR) {
  607. vnet_hdr_len = q->vnet_hdr_sz;
  608. err = -EINVAL;
  609. if (len < vnet_hdr_len)
  610. goto err;
  611. len -= vnet_hdr_len;
  612. err = -EFAULT;
  613. n = copy_from_iter(&vnet_hdr, sizeof(vnet_hdr), from);
  614. if (n != sizeof(vnet_hdr))
  615. goto err;
  616. iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr));
  617. if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  618. macvtap16_to_cpu(q, vnet_hdr.csum_start) +
  619. macvtap16_to_cpu(q, vnet_hdr.csum_offset) + 2 >
  620. macvtap16_to_cpu(q, vnet_hdr.hdr_len))
  621. vnet_hdr.hdr_len = cpu_to_macvtap16(q,
  622. macvtap16_to_cpu(q, vnet_hdr.csum_start) +
  623. macvtap16_to_cpu(q, vnet_hdr.csum_offset) + 2);
  624. err = -EINVAL;
  625. if (macvtap16_to_cpu(q, vnet_hdr.hdr_len) > len)
  626. goto err;
  627. }
  628. err = -EINVAL;
  629. if (unlikely(len < ETH_HLEN))
  630. goto err;
  631. if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
  632. struct iov_iter i;
  633. copylen = vnet_hdr.hdr_len ?
  634. macvtap16_to_cpu(q, vnet_hdr.hdr_len) : GOODCOPY_LEN;
  635. if (copylen > good_linear)
  636. copylen = good_linear;
  637. linear = copylen;
  638. i = *from;
  639. iov_iter_advance(&i, copylen);
  640. if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
  641. zerocopy = true;
  642. }
  643. if (!zerocopy) {
  644. copylen = len;
  645. if (macvtap16_to_cpu(q, vnet_hdr.hdr_len) > good_linear)
  646. linear = good_linear;
  647. else
  648. linear = macvtap16_to_cpu(q, vnet_hdr.hdr_len);
  649. }
  650. skb = macvtap_alloc_skb(&q->sk, MACVTAP_RESERVE, copylen,
  651. linear, noblock, &err);
  652. if (!skb)
  653. goto err;
  654. if (zerocopy)
  655. err = zerocopy_sg_from_iter(skb, from);
  656. else {
  657. err = skb_copy_datagram_from_iter(skb, 0, from, len);
  658. if (!err && m && m->msg_control) {
  659. struct ubuf_info *uarg = m->msg_control;
  660. uarg->callback(uarg, false);
  661. }
  662. }
  663. if (err)
  664. goto err_kfree;
  665. skb_set_network_header(skb, ETH_HLEN);
  666. skb_reset_mac_header(skb);
  667. skb->protocol = eth_hdr(skb)->h_proto;
  668. if (vnet_hdr_len) {
  669. err = macvtap_skb_from_vnet_hdr(q, skb, &vnet_hdr);
  670. if (err)
  671. goto err_kfree;
  672. }
  673. skb_probe_transport_header(skb, ETH_HLEN);
  674. rcu_read_lock();
  675. vlan = rcu_dereference(q->vlan);
  676. /* copy skb_ubuf_info for callback when skb has no error */
  677. if (zerocopy) {
  678. skb_shinfo(skb)->destructor_arg = m->msg_control;
  679. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  680. skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
  681. }
  682. if (vlan) {
  683. skb->dev = vlan->dev;
  684. dev_queue_xmit(skb);
  685. } else {
  686. kfree_skb(skb);
  687. }
  688. rcu_read_unlock();
  689. return total_len;
  690. err_kfree:
  691. kfree_skb(skb);
  692. err:
  693. rcu_read_lock();
  694. vlan = rcu_dereference(q->vlan);
  695. if (vlan)
  696. this_cpu_inc(vlan->pcpu_stats->tx_dropped);
  697. rcu_read_unlock();
  698. return err;
  699. }
  700. static ssize_t macvtap_write_iter(struct kiocb *iocb, struct iov_iter *from)
  701. {
  702. struct file *file = iocb->ki_filp;
  703. struct macvtap_queue *q = file->private_data;
  704. return macvtap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK);
  705. }
  706. /* Put packet to the user space buffer */
  707. static ssize_t macvtap_put_user(struct macvtap_queue *q,
  708. const struct sk_buff *skb,
  709. struct iov_iter *iter)
  710. {
  711. int ret;
  712. int vnet_hdr_len = 0;
  713. int vlan_offset = 0;
  714. int total;
  715. if (q->flags & IFF_VNET_HDR) {
  716. struct virtio_net_hdr vnet_hdr;
  717. vnet_hdr_len = q->vnet_hdr_sz;
  718. if (iov_iter_count(iter) < vnet_hdr_len)
  719. return -EINVAL;
  720. macvtap_skb_to_vnet_hdr(q, skb, &vnet_hdr);
  721. if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) !=
  722. sizeof(vnet_hdr))
  723. return -EFAULT;
  724. iov_iter_advance(iter, vnet_hdr_len - sizeof(vnet_hdr));
  725. }
  726. total = vnet_hdr_len;
  727. total += skb->len;
  728. if (skb_vlan_tag_present(skb)) {
  729. struct {
  730. __be16 h_vlan_proto;
  731. __be16 h_vlan_TCI;
  732. } veth;
  733. veth.h_vlan_proto = skb->vlan_proto;
  734. veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
  735. vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
  736. total += VLAN_HLEN;
  737. ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
  738. if (ret || !iov_iter_count(iter))
  739. goto done;
  740. ret = copy_to_iter(&veth, sizeof(veth), iter);
  741. if (ret != sizeof(veth) || !iov_iter_count(iter))
  742. goto done;
  743. }
  744. ret = skb_copy_datagram_iter(skb, vlan_offset, iter,
  745. skb->len - vlan_offset);
  746. done:
  747. return ret ? ret : total;
  748. }
  749. static ssize_t macvtap_do_read(struct macvtap_queue *q,
  750. struct iov_iter *to,
  751. int noblock)
  752. {
  753. DEFINE_WAIT(wait);
  754. struct sk_buff *skb;
  755. ssize_t ret = 0;
  756. if (!iov_iter_count(to))
  757. return 0;
  758. while (1) {
  759. if (!noblock)
  760. prepare_to_wait(sk_sleep(&q->sk), &wait,
  761. TASK_INTERRUPTIBLE);
  762. /* Read frames from the queue */
  763. skb = skb_dequeue(&q->sk.sk_receive_queue);
  764. if (skb)
  765. break;
  766. if (noblock) {
  767. ret = -EAGAIN;
  768. break;
  769. }
  770. if (signal_pending(current)) {
  771. ret = -ERESTARTSYS;
  772. break;
  773. }
  774. /* Nothing to read, let's sleep */
  775. schedule();
  776. }
  777. if (skb) {
  778. ret = macvtap_put_user(q, skb, to);
  779. if (unlikely(ret < 0))
  780. kfree_skb(skb);
  781. else
  782. consume_skb(skb);
  783. }
  784. if (!noblock)
  785. finish_wait(sk_sleep(&q->sk), &wait);
  786. return ret;
  787. }
  788. static ssize_t macvtap_read_iter(struct kiocb *iocb, struct iov_iter *to)
  789. {
  790. struct file *file = iocb->ki_filp;
  791. struct macvtap_queue *q = file->private_data;
  792. ssize_t len = iov_iter_count(to), ret;
  793. ret = macvtap_do_read(q, to, file->f_flags & O_NONBLOCK);
  794. ret = min_t(ssize_t, ret, len);
  795. if (ret > 0)
  796. iocb->ki_pos = ret;
  797. return ret;
  798. }
  799. static struct macvlan_dev *macvtap_get_vlan(struct macvtap_queue *q)
  800. {
  801. struct macvlan_dev *vlan;
  802. ASSERT_RTNL();
  803. vlan = rtnl_dereference(q->vlan);
  804. if (vlan)
  805. dev_hold(vlan->dev);
  806. return vlan;
  807. }
  808. static void macvtap_put_vlan(struct macvlan_dev *vlan)
  809. {
  810. dev_put(vlan->dev);
  811. }
  812. static int macvtap_ioctl_set_queue(struct file *file, unsigned int flags)
  813. {
  814. struct macvtap_queue *q = file->private_data;
  815. struct macvlan_dev *vlan;
  816. int ret;
  817. vlan = macvtap_get_vlan(q);
  818. if (!vlan)
  819. return -EINVAL;
  820. if (flags & IFF_ATTACH_QUEUE)
  821. ret = macvtap_enable_queue(vlan->dev, file, q);
  822. else if (flags & IFF_DETACH_QUEUE)
  823. ret = macvtap_disable_queue(q);
  824. else
  825. ret = -EINVAL;
  826. macvtap_put_vlan(vlan);
  827. return ret;
  828. }
  829. static int set_offload(struct macvtap_queue *q, unsigned long arg)
  830. {
  831. struct macvlan_dev *vlan;
  832. netdev_features_t features;
  833. netdev_features_t feature_mask = 0;
  834. vlan = rtnl_dereference(q->vlan);
  835. if (!vlan)
  836. return -ENOLINK;
  837. features = vlan->dev->features;
  838. if (arg & TUN_F_CSUM) {
  839. feature_mask = NETIF_F_HW_CSUM;
  840. if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
  841. if (arg & TUN_F_TSO_ECN)
  842. feature_mask |= NETIF_F_TSO_ECN;
  843. if (arg & TUN_F_TSO4)
  844. feature_mask |= NETIF_F_TSO;
  845. if (arg & TUN_F_TSO6)
  846. feature_mask |= NETIF_F_TSO6;
  847. }
  848. if (arg & TUN_F_UFO)
  849. feature_mask |= NETIF_F_UFO;
  850. }
  851. /* tun/tap driver inverts the usage for TSO offloads, where
  852. * setting the TSO bit means that the userspace wants to
  853. * accept TSO frames and turning it off means that user space
  854. * does not support TSO.
  855. * For macvtap, we have to invert it to mean the same thing.
  856. * When user space turns off TSO, we turn off GSO/LRO so that
  857. * user-space will not receive TSO frames.
  858. */
  859. if (feature_mask & (NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_UFO))
  860. features |= RX_OFFLOADS;
  861. else
  862. features &= ~RX_OFFLOADS;
  863. /* tap_features are the same as features on tun/tap and
  864. * reflect user expectations.
  865. */
  866. vlan->tap_features = feature_mask;
  867. vlan->set_features = features;
  868. netdev_update_features(vlan->dev);
  869. return 0;
  870. }
  871. /*
  872. * provide compatibility with generic tun/tap interface
  873. */
  874. static long macvtap_ioctl(struct file *file, unsigned int cmd,
  875. unsigned long arg)
  876. {
  877. struct macvtap_queue *q = file->private_data;
  878. struct macvlan_dev *vlan;
  879. void __user *argp = (void __user *)arg;
  880. struct ifreq __user *ifr = argp;
  881. unsigned int __user *up = argp;
  882. unsigned short u;
  883. int __user *sp = argp;
  884. struct sockaddr sa;
  885. int s;
  886. int ret;
  887. switch (cmd) {
  888. case TUNSETIFF:
  889. /* ignore the name, just look at flags */
  890. if (get_user(u, &ifr->ifr_flags))
  891. return -EFAULT;
  892. ret = 0;
  893. if ((u & ~MACVTAP_FEATURES) != (IFF_NO_PI | IFF_TAP))
  894. ret = -EINVAL;
  895. else
  896. q->flags = (q->flags & ~MACVTAP_FEATURES) | u;
  897. return ret;
  898. case TUNGETIFF:
  899. rtnl_lock();
  900. vlan = macvtap_get_vlan(q);
  901. if (!vlan) {
  902. rtnl_unlock();
  903. return -ENOLINK;
  904. }
  905. ret = 0;
  906. u = q->flags;
  907. if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
  908. put_user(u, &ifr->ifr_flags))
  909. ret = -EFAULT;
  910. macvtap_put_vlan(vlan);
  911. rtnl_unlock();
  912. return ret;
  913. case TUNSETQUEUE:
  914. if (get_user(u, &ifr->ifr_flags))
  915. return -EFAULT;
  916. rtnl_lock();
  917. ret = macvtap_ioctl_set_queue(file, u);
  918. rtnl_unlock();
  919. return ret;
  920. case TUNGETFEATURES:
  921. if (put_user(IFF_TAP | IFF_NO_PI | MACVTAP_FEATURES, up))
  922. return -EFAULT;
  923. return 0;
  924. case TUNSETSNDBUF:
  925. if (get_user(u, up))
  926. return -EFAULT;
  927. q->sk.sk_sndbuf = u;
  928. return 0;
  929. case TUNGETVNETHDRSZ:
  930. s = q->vnet_hdr_sz;
  931. if (put_user(s, sp))
  932. return -EFAULT;
  933. return 0;
  934. case TUNSETVNETHDRSZ:
  935. if (get_user(s, sp))
  936. return -EFAULT;
  937. if (s < (int)sizeof(struct virtio_net_hdr))
  938. return -EINVAL;
  939. q->vnet_hdr_sz = s;
  940. return 0;
  941. case TUNGETVNETLE:
  942. s = !!(q->flags & MACVTAP_VNET_LE);
  943. if (put_user(s, sp))
  944. return -EFAULT;
  945. return 0;
  946. case TUNSETVNETLE:
  947. if (get_user(s, sp))
  948. return -EFAULT;
  949. if (s)
  950. q->flags |= MACVTAP_VNET_LE;
  951. else
  952. q->flags &= ~MACVTAP_VNET_LE;
  953. return 0;
  954. case TUNGETVNETBE:
  955. return macvtap_get_vnet_be(q, sp);
  956. case TUNSETVNETBE:
  957. return macvtap_set_vnet_be(q, sp);
  958. case TUNSETOFFLOAD:
  959. /* let the user check for future flags */
  960. if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
  961. TUN_F_TSO_ECN | TUN_F_UFO))
  962. return -EINVAL;
  963. rtnl_lock();
  964. ret = set_offload(q, arg);
  965. rtnl_unlock();
  966. return ret;
  967. case SIOCGIFHWADDR:
  968. rtnl_lock();
  969. vlan = macvtap_get_vlan(q);
  970. if (!vlan) {
  971. rtnl_unlock();
  972. return -ENOLINK;
  973. }
  974. ret = 0;
  975. u = vlan->dev->type;
  976. if (copy_to_user(&ifr->ifr_name, vlan->dev->name, IFNAMSIZ) ||
  977. copy_to_user(&ifr->ifr_hwaddr.sa_data, vlan->dev->dev_addr, ETH_ALEN) ||
  978. put_user(u, &ifr->ifr_hwaddr.sa_family))
  979. ret = -EFAULT;
  980. macvtap_put_vlan(vlan);
  981. rtnl_unlock();
  982. return ret;
  983. case SIOCSIFHWADDR:
  984. if (copy_from_user(&sa, &ifr->ifr_hwaddr, sizeof(sa)))
  985. return -EFAULT;
  986. rtnl_lock();
  987. vlan = macvtap_get_vlan(q);
  988. if (!vlan) {
  989. rtnl_unlock();
  990. return -ENOLINK;
  991. }
  992. ret = dev_set_mac_address(vlan->dev, &sa);
  993. macvtap_put_vlan(vlan);
  994. rtnl_unlock();
  995. return ret;
  996. default:
  997. return -EINVAL;
  998. }
  999. }
  1000. #ifdef CONFIG_COMPAT
  1001. static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
  1002. unsigned long arg)
  1003. {
  1004. return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  1005. }
  1006. #endif
  1007. static const struct file_operations macvtap_fops = {
  1008. .owner = THIS_MODULE,
  1009. .open = macvtap_open,
  1010. .release = macvtap_release,
  1011. .read_iter = macvtap_read_iter,
  1012. .write_iter = macvtap_write_iter,
  1013. .poll = macvtap_poll,
  1014. .llseek = no_llseek,
  1015. .unlocked_ioctl = macvtap_ioctl,
  1016. #ifdef CONFIG_COMPAT
  1017. .compat_ioctl = macvtap_compat_ioctl,
  1018. #endif
  1019. };
  1020. static int macvtap_sendmsg(struct socket *sock, struct msghdr *m,
  1021. size_t total_len)
  1022. {
  1023. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  1024. return macvtap_get_user(q, m, &m->msg_iter, m->msg_flags & MSG_DONTWAIT);
  1025. }
  1026. static int macvtap_recvmsg(struct socket *sock, struct msghdr *m,
  1027. size_t total_len, int flags)
  1028. {
  1029. struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
  1030. int ret;
  1031. if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
  1032. return -EINVAL;
  1033. ret = macvtap_do_read(q, &m->msg_iter, flags & MSG_DONTWAIT);
  1034. if (ret > total_len) {
  1035. m->msg_flags |= MSG_TRUNC;
  1036. ret = flags & MSG_TRUNC ? ret : total_len;
  1037. }
  1038. return ret;
  1039. }
  1040. /* Ops structure to mimic raw sockets with tun */
  1041. static const struct proto_ops macvtap_socket_ops = {
  1042. .sendmsg = macvtap_sendmsg,
  1043. .recvmsg = macvtap_recvmsg,
  1044. };
  1045. /* Get an underlying socket object from tun file. Returns error unless file is
  1046. * attached to a device. The returned object works like a packet socket, it
  1047. * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
  1048. * holding a reference to the file for as long as the socket is in use. */
  1049. struct socket *macvtap_get_socket(struct file *file)
  1050. {
  1051. struct macvtap_queue *q;
  1052. if (file->f_op != &macvtap_fops)
  1053. return ERR_PTR(-EINVAL);
  1054. q = file->private_data;
  1055. if (!q)
  1056. return ERR_PTR(-EBADFD);
  1057. return &q->sock;
  1058. }
  1059. EXPORT_SYMBOL_GPL(macvtap_get_socket);
  1060. static int macvtap_device_event(struct notifier_block *unused,
  1061. unsigned long event, void *ptr)
  1062. {
  1063. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1064. struct macvlan_dev *vlan;
  1065. struct device *classdev;
  1066. dev_t devt;
  1067. int err;
  1068. if (dev->rtnl_link_ops != &macvtap_link_ops)
  1069. return NOTIFY_DONE;
  1070. vlan = netdev_priv(dev);
  1071. switch (event) {
  1072. case NETDEV_REGISTER:
  1073. /* Create the device node here after the network device has
  1074. * been registered but before register_netdevice has
  1075. * finished running.
  1076. */
  1077. err = macvtap_get_minor(vlan);
  1078. if (err)
  1079. return notifier_from_errno(err);
  1080. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  1081. classdev = device_create(macvtap_class, &dev->dev, devt,
  1082. dev, "tap%d", dev->ifindex);
  1083. if (IS_ERR(classdev)) {
  1084. macvtap_free_minor(vlan);
  1085. return notifier_from_errno(PTR_ERR(classdev));
  1086. }
  1087. break;
  1088. case NETDEV_UNREGISTER:
  1089. devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
  1090. device_destroy(macvtap_class, devt);
  1091. macvtap_free_minor(vlan);
  1092. break;
  1093. }
  1094. return NOTIFY_DONE;
  1095. }
  1096. static struct notifier_block macvtap_notifier_block __read_mostly = {
  1097. .notifier_call = macvtap_device_event,
  1098. };
  1099. static int macvtap_init(void)
  1100. {
  1101. int err;
  1102. err = alloc_chrdev_region(&macvtap_major, 0,
  1103. MACVTAP_NUM_DEVS, "macvtap");
  1104. if (err)
  1105. goto out1;
  1106. cdev_init(&macvtap_cdev, &macvtap_fops);
  1107. err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS);
  1108. if (err)
  1109. goto out2;
  1110. macvtap_class = class_create(THIS_MODULE, "macvtap");
  1111. if (IS_ERR(macvtap_class)) {
  1112. err = PTR_ERR(macvtap_class);
  1113. goto out3;
  1114. }
  1115. err = register_netdevice_notifier(&macvtap_notifier_block);
  1116. if (err)
  1117. goto out4;
  1118. err = macvlan_link_register(&macvtap_link_ops);
  1119. if (err)
  1120. goto out5;
  1121. return 0;
  1122. out5:
  1123. unregister_netdevice_notifier(&macvtap_notifier_block);
  1124. out4:
  1125. class_unregister(macvtap_class);
  1126. out3:
  1127. cdev_del(&macvtap_cdev);
  1128. out2:
  1129. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  1130. out1:
  1131. return err;
  1132. }
  1133. module_init(macvtap_init);
  1134. static void macvtap_exit(void)
  1135. {
  1136. rtnl_link_unregister(&macvtap_link_ops);
  1137. unregister_netdevice_notifier(&macvtap_notifier_block);
  1138. class_unregister(macvtap_class);
  1139. cdev_del(&macvtap_cdev);
  1140. unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
  1141. }
  1142. module_exit(macvtap_exit);
  1143. MODULE_ALIAS_RTNL_LINK("macvtap");
  1144. MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
  1145. MODULE_LICENSE("GPL");