geneve.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. /*
  2. * GENEVE: Generic Network Virtualization Encapsulation
  3. *
  4. * Copyright (c) 2015 Red Hat, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/hash.h>
  15. #include <net/dst_metadata.h>
  16. #include <net/gro_cells.h>
  17. #include <net/rtnetlink.h>
  18. #include <net/geneve.h>
  19. #include <net/protocol.h>
  20. #define GENEVE_NETDEV_VER "0.6"
  21. #define GENEVE_UDP_PORT 6081
  22. #define GENEVE_N_VID (1u << 24)
  23. #define GENEVE_VID_MASK (GENEVE_N_VID - 1)
  24. #define VNI_HASH_BITS 10
  25. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  26. static bool log_ecn_error = true;
  27. module_param(log_ecn_error, bool, 0644);
  28. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  29. #define GENEVE_VER 0
  30. #define GENEVE_BASE_HLEN (sizeof(struct udphdr) + sizeof(struct genevehdr))
  31. /* per-network namespace private data for this module */
  32. struct geneve_net {
  33. struct list_head geneve_list;
  34. struct list_head sock_list;
  35. };
  36. static int geneve_net_id;
  37. union geneve_addr {
  38. struct sockaddr_in sin;
  39. struct sockaddr_in6 sin6;
  40. struct sockaddr sa;
  41. };
  42. static union geneve_addr geneve_remote_unspec = { .sa.sa_family = AF_UNSPEC, };
  43. /* Pseudo network device */
  44. struct geneve_dev {
  45. struct hlist_node hlist; /* vni hash table */
  46. struct net *net; /* netns for packet i/o */
  47. struct net_device *dev; /* netdev for geneve tunnel */
  48. struct geneve_sock __rcu *sock4; /* IPv4 socket used for geneve tunnel */
  49. #if IS_ENABLED(CONFIG_IPV6)
  50. struct geneve_sock __rcu *sock6; /* IPv6 socket used for geneve tunnel */
  51. #endif
  52. u8 vni[3]; /* virtual network ID for tunnel */
  53. u8 ttl; /* TTL override */
  54. u8 tos; /* TOS override */
  55. union geneve_addr remote; /* IP address for link partner */
  56. struct list_head next; /* geneve's per namespace list */
  57. __be32 label; /* IPv6 flowlabel override */
  58. __be16 dst_port;
  59. bool collect_md;
  60. struct gro_cells gro_cells;
  61. u32 flags;
  62. struct dst_cache dst_cache;
  63. };
  64. /* Geneve device flags */
  65. #define GENEVE_F_UDP_ZERO_CSUM_TX BIT(0)
  66. #define GENEVE_F_UDP_ZERO_CSUM6_TX BIT(1)
  67. #define GENEVE_F_UDP_ZERO_CSUM6_RX BIT(2)
  68. struct geneve_sock {
  69. bool collect_md;
  70. struct list_head list;
  71. struct socket *sock;
  72. struct rcu_head rcu;
  73. int refcnt;
  74. struct hlist_head vni_list[VNI_HASH_SIZE];
  75. u32 flags;
  76. };
  77. static inline __u32 geneve_net_vni_hash(u8 vni[3])
  78. {
  79. __u32 vnid;
  80. vnid = (vni[0] << 16) | (vni[1] << 8) | vni[2];
  81. return hash_32(vnid, VNI_HASH_BITS);
  82. }
  83. static __be64 vni_to_tunnel_id(const __u8 *vni)
  84. {
  85. #ifdef __BIG_ENDIAN
  86. return (vni[0] << 16) | (vni[1] << 8) | vni[2];
  87. #else
  88. return (__force __be64)(((__force u64)vni[0] << 40) |
  89. ((__force u64)vni[1] << 48) |
  90. ((__force u64)vni[2] << 56));
  91. #endif
  92. }
  93. static sa_family_t geneve_get_sk_family(struct geneve_sock *gs)
  94. {
  95. return gs->sock->sk->sk_family;
  96. }
  97. static struct geneve_dev *geneve_lookup(struct geneve_sock *gs,
  98. __be32 addr, u8 vni[])
  99. {
  100. struct hlist_head *vni_list_head;
  101. struct geneve_dev *geneve;
  102. __u32 hash;
  103. /* Find the device for this VNI */
  104. hash = geneve_net_vni_hash(vni);
  105. vni_list_head = &gs->vni_list[hash];
  106. hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
  107. if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
  108. addr == geneve->remote.sin.sin_addr.s_addr)
  109. return geneve;
  110. }
  111. return NULL;
  112. }
  113. #if IS_ENABLED(CONFIG_IPV6)
  114. static struct geneve_dev *geneve6_lookup(struct geneve_sock *gs,
  115. struct in6_addr addr6, u8 vni[])
  116. {
  117. struct hlist_head *vni_list_head;
  118. struct geneve_dev *geneve;
  119. __u32 hash;
  120. /* Find the device for this VNI */
  121. hash = geneve_net_vni_hash(vni);
  122. vni_list_head = &gs->vni_list[hash];
  123. hlist_for_each_entry_rcu(geneve, vni_list_head, hlist) {
  124. if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
  125. ipv6_addr_equal(&addr6, &geneve->remote.sin6.sin6_addr))
  126. return geneve;
  127. }
  128. return NULL;
  129. }
  130. #endif
  131. static inline struct genevehdr *geneve_hdr(const struct sk_buff *skb)
  132. {
  133. return (struct genevehdr *)(udp_hdr(skb) + 1);
  134. }
  135. static struct geneve_dev *geneve_lookup_skb(struct geneve_sock *gs,
  136. struct sk_buff *skb)
  137. {
  138. u8 *vni;
  139. __be32 addr;
  140. static u8 zero_vni[3];
  141. #if IS_ENABLED(CONFIG_IPV6)
  142. static struct in6_addr zero_addr6;
  143. #endif
  144. if (geneve_get_sk_family(gs) == AF_INET) {
  145. struct iphdr *iph;
  146. iph = ip_hdr(skb); /* outer IP header... */
  147. if (gs->collect_md) {
  148. vni = zero_vni;
  149. addr = 0;
  150. } else {
  151. vni = geneve_hdr(skb)->vni;
  152. addr = iph->saddr;
  153. }
  154. return geneve_lookup(gs, addr, vni);
  155. #if IS_ENABLED(CONFIG_IPV6)
  156. } else if (geneve_get_sk_family(gs) == AF_INET6) {
  157. struct ipv6hdr *ip6h;
  158. struct in6_addr addr6;
  159. ip6h = ipv6_hdr(skb); /* outer IPv6 header... */
  160. if (gs->collect_md) {
  161. vni = zero_vni;
  162. addr6 = zero_addr6;
  163. } else {
  164. vni = geneve_hdr(skb)->vni;
  165. addr6 = ip6h->saddr;
  166. }
  167. return geneve6_lookup(gs, addr6, vni);
  168. #endif
  169. }
  170. return NULL;
  171. }
  172. /* geneve receive/decap routine */
  173. static void geneve_rx(struct geneve_dev *geneve, struct geneve_sock *gs,
  174. struct sk_buff *skb)
  175. {
  176. struct genevehdr *gnvh = geneve_hdr(skb);
  177. struct metadata_dst *tun_dst = NULL;
  178. struct pcpu_sw_netstats *stats;
  179. unsigned int len;
  180. int err = 0;
  181. void *oiph;
  182. if (ip_tunnel_collect_metadata() || gs->collect_md) {
  183. __be16 flags;
  184. flags = TUNNEL_KEY | TUNNEL_GENEVE_OPT |
  185. (gnvh->oam ? TUNNEL_OAM : 0) |
  186. (gnvh->critical ? TUNNEL_CRIT_OPT : 0);
  187. tun_dst = udp_tun_rx_dst(skb, geneve_get_sk_family(gs), flags,
  188. vni_to_tunnel_id(gnvh->vni),
  189. gnvh->opt_len * 4);
  190. if (!tun_dst) {
  191. geneve->dev->stats.rx_dropped++;
  192. goto drop;
  193. }
  194. /* Update tunnel dst according to Geneve options. */
  195. ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
  196. gnvh->options, gnvh->opt_len * 4);
  197. } else {
  198. /* Drop packets w/ critical options,
  199. * since we don't support any...
  200. */
  201. if (gnvh->critical) {
  202. geneve->dev->stats.rx_frame_errors++;
  203. geneve->dev->stats.rx_errors++;
  204. goto drop;
  205. }
  206. }
  207. skb_reset_mac_header(skb);
  208. skb->protocol = eth_type_trans(skb, geneve->dev);
  209. skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
  210. if (tun_dst)
  211. skb_dst_set(skb, &tun_dst->dst);
  212. /* Ignore packet loops (and multicast echo) */
  213. if (ether_addr_equal(eth_hdr(skb)->h_source, geneve->dev->dev_addr)) {
  214. geneve->dev->stats.rx_errors++;
  215. goto drop;
  216. }
  217. oiph = skb_network_header(skb);
  218. skb_reset_network_header(skb);
  219. if (geneve_get_sk_family(gs) == AF_INET)
  220. err = IP_ECN_decapsulate(oiph, skb);
  221. #if IS_ENABLED(CONFIG_IPV6)
  222. else
  223. err = IP6_ECN_decapsulate(oiph, skb);
  224. #endif
  225. if (unlikely(err)) {
  226. if (log_ecn_error) {
  227. if (geneve_get_sk_family(gs) == AF_INET)
  228. net_info_ratelimited("non-ECT from %pI4 "
  229. "with TOS=%#x\n",
  230. &((struct iphdr *)oiph)->saddr,
  231. ((struct iphdr *)oiph)->tos);
  232. #if IS_ENABLED(CONFIG_IPV6)
  233. else
  234. net_info_ratelimited("non-ECT from %pI6\n",
  235. &((struct ipv6hdr *)oiph)->saddr);
  236. #endif
  237. }
  238. if (err > 1) {
  239. ++geneve->dev->stats.rx_frame_errors;
  240. ++geneve->dev->stats.rx_errors;
  241. goto drop;
  242. }
  243. }
  244. len = skb->len;
  245. err = gro_cells_receive(&geneve->gro_cells, skb);
  246. if (likely(err == NET_RX_SUCCESS)) {
  247. stats = this_cpu_ptr(geneve->dev->tstats);
  248. u64_stats_update_begin(&stats->syncp);
  249. stats->rx_packets++;
  250. stats->rx_bytes += len;
  251. u64_stats_update_end(&stats->syncp);
  252. }
  253. return;
  254. drop:
  255. /* Consume bad packet */
  256. kfree_skb(skb);
  257. }
  258. /* Setup stats when device is created */
  259. static int geneve_init(struct net_device *dev)
  260. {
  261. struct geneve_dev *geneve = netdev_priv(dev);
  262. int err;
  263. dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
  264. if (!dev->tstats)
  265. return -ENOMEM;
  266. err = gro_cells_init(&geneve->gro_cells, dev);
  267. if (err) {
  268. free_percpu(dev->tstats);
  269. return err;
  270. }
  271. err = dst_cache_init(&geneve->dst_cache, GFP_KERNEL);
  272. if (err) {
  273. free_percpu(dev->tstats);
  274. gro_cells_destroy(&geneve->gro_cells);
  275. return err;
  276. }
  277. return 0;
  278. }
  279. static void geneve_uninit(struct net_device *dev)
  280. {
  281. struct geneve_dev *geneve = netdev_priv(dev);
  282. dst_cache_destroy(&geneve->dst_cache);
  283. gro_cells_destroy(&geneve->gro_cells);
  284. free_percpu(dev->tstats);
  285. }
  286. /* Callback from net/ipv4/udp.c to receive packets */
  287. static int geneve_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  288. {
  289. struct genevehdr *geneveh;
  290. struct geneve_dev *geneve;
  291. struct geneve_sock *gs;
  292. int opts_len;
  293. /* Need UDP and Geneve header to be present */
  294. if (unlikely(!pskb_may_pull(skb, GENEVE_BASE_HLEN)))
  295. goto drop;
  296. /* Return packets with reserved bits set */
  297. geneveh = geneve_hdr(skb);
  298. if (unlikely(geneveh->ver != GENEVE_VER))
  299. goto drop;
  300. if (unlikely(geneveh->proto_type != htons(ETH_P_TEB)))
  301. goto drop;
  302. gs = rcu_dereference_sk_user_data(sk);
  303. if (!gs)
  304. goto drop;
  305. geneve = geneve_lookup_skb(gs, skb);
  306. if (!geneve)
  307. goto drop;
  308. opts_len = geneveh->opt_len * 4;
  309. if (iptunnel_pull_header(skb, GENEVE_BASE_HLEN + opts_len,
  310. htons(ETH_P_TEB),
  311. !net_eq(geneve->net, dev_net(geneve->dev)))) {
  312. geneve->dev->stats.rx_dropped++;
  313. goto drop;
  314. }
  315. geneve_rx(geneve, gs, skb);
  316. return 0;
  317. drop:
  318. /* Consume bad packet */
  319. kfree_skb(skb);
  320. return 0;
  321. }
  322. static struct socket *geneve_create_sock(struct net *net, bool ipv6,
  323. __be16 port, u32 flags)
  324. {
  325. struct socket *sock;
  326. struct udp_port_cfg udp_conf;
  327. int err;
  328. memset(&udp_conf, 0, sizeof(udp_conf));
  329. if (ipv6) {
  330. udp_conf.family = AF_INET6;
  331. udp_conf.ipv6_v6only = 1;
  332. udp_conf.use_udp6_rx_checksums =
  333. !(flags & GENEVE_F_UDP_ZERO_CSUM6_RX);
  334. } else {
  335. udp_conf.family = AF_INET;
  336. udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
  337. }
  338. udp_conf.local_udp_port = port;
  339. /* Open UDP socket */
  340. err = udp_sock_create(net, &udp_conf, &sock);
  341. if (err < 0)
  342. return ERR_PTR(err);
  343. return sock;
  344. }
  345. static int geneve_hlen(struct genevehdr *gh)
  346. {
  347. return sizeof(*gh) + gh->opt_len * 4;
  348. }
  349. static struct sk_buff **geneve_gro_receive(struct sock *sk,
  350. struct sk_buff **head,
  351. struct sk_buff *skb)
  352. {
  353. struct sk_buff *p, **pp = NULL;
  354. struct genevehdr *gh, *gh2;
  355. unsigned int hlen, gh_len, off_gnv;
  356. const struct packet_offload *ptype;
  357. __be16 type;
  358. int flush = 1;
  359. off_gnv = skb_gro_offset(skb);
  360. hlen = off_gnv + sizeof(*gh);
  361. gh = skb_gro_header_fast(skb, off_gnv);
  362. if (skb_gro_header_hard(skb, hlen)) {
  363. gh = skb_gro_header_slow(skb, hlen, off_gnv);
  364. if (unlikely(!gh))
  365. goto out;
  366. }
  367. if (gh->ver != GENEVE_VER || gh->oam)
  368. goto out;
  369. gh_len = geneve_hlen(gh);
  370. hlen = off_gnv + gh_len;
  371. if (skb_gro_header_hard(skb, hlen)) {
  372. gh = skb_gro_header_slow(skb, hlen, off_gnv);
  373. if (unlikely(!gh))
  374. goto out;
  375. }
  376. for (p = *head; p; p = p->next) {
  377. if (!NAPI_GRO_CB(p)->same_flow)
  378. continue;
  379. gh2 = (struct genevehdr *)(p->data + off_gnv);
  380. if (gh->opt_len != gh2->opt_len ||
  381. memcmp(gh, gh2, gh_len)) {
  382. NAPI_GRO_CB(p)->same_flow = 0;
  383. continue;
  384. }
  385. }
  386. type = gh->proto_type;
  387. rcu_read_lock();
  388. ptype = gro_find_receive_by_type(type);
  389. if (!ptype)
  390. goto out_unlock;
  391. skb_gro_pull(skb, gh_len);
  392. skb_gro_postpull_rcsum(skb, gh, gh_len);
  393. pp = call_gro_receive(ptype->callbacks.gro_receive, head, skb);
  394. flush = 0;
  395. out_unlock:
  396. rcu_read_unlock();
  397. out:
  398. NAPI_GRO_CB(skb)->flush |= flush;
  399. return pp;
  400. }
  401. static int geneve_gro_complete(struct sock *sk, struct sk_buff *skb,
  402. int nhoff)
  403. {
  404. struct genevehdr *gh;
  405. struct packet_offload *ptype;
  406. __be16 type;
  407. int gh_len;
  408. int err = -ENOSYS;
  409. gh = (struct genevehdr *)(skb->data + nhoff);
  410. gh_len = geneve_hlen(gh);
  411. type = gh->proto_type;
  412. rcu_read_lock();
  413. ptype = gro_find_complete_by_type(type);
  414. if (ptype)
  415. err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
  416. rcu_read_unlock();
  417. skb_set_inner_mac_header(skb, nhoff + gh_len);
  418. return err;
  419. }
  420. /* Create new listen socket if needed */
  421. static struct geneve_sock *geneve_socket_create(struct net *net, __be16 port,
  422. bool ipv6, u32 flags)
  423. {
  424. struct geneve_net *gn = net_generic(net, geneve_net_id);
  425. struct geneve_sock *gs;
  426. struct socket *sock;
  427. struct udp_tunnel_sock_cfg tunnel_cfg;
  428. int h;
  429. gs = kzalloc(sizeof(*gs), GFP_KERNEL);
  430. if (!gs)
  431. return ERR_PTR(-ENOMEM);
  432. sock = geneve_create_sock(net, ipv6, port, flags);
  433. if (IS_ERR(sock)) {
  434. kfree(gs);
  435. return ERR_CAST(sock);
  436. }
  437. gs->sock = sock;
  438. gs->refcnt = 1;
  439. for (h = 0; h < VNI_HASH_SIZE; ++h)
  440. INIT_HLIST_HEAD(&gs->vni_list[h]);
  441. /* Initialize the geneve udp offloads structure */
  442. udp_tunnel_notify_add_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
  443. /* Mark socket as an encapsulation socket */
  444. memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
  445. tunnel_cfg.sk_user_data = gs;
  446. tunnel_cfg.encap_type = 1;
  447. tunnel_cfg.gro_receive = geneve_gro_receive;
  448. tunnel_cfg.gro_complete = geneve_gro_complete;
  449. tunnel_cfg.encap_rcv = geneve_udp_encap_recv;
  450. tunnel_cfg.encap_destroy = NULL;
  451. setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
  452. list_add(&gs->list, &gn->sock_list);
  453. return gs;
  454. }
  455. static void __geneve_sock_release(struct geneve_sock *gs)
  456. {
  457. if (!gs || --gs->refcnt)
  458. return;
  459. list_del(&gs->list);
  460. udp_tunnel_notify_del_rx_port(gs->sock, UDP_TUNNEL_TYPE_GENEVE);
  461. udp_tunnel_sock_release(gs->sock);
  462. kfree_rcu(gs, rcu);
  463. }
  464. static void geneve_sock_release(struct geneve_dev *geneve)
  465. {
  466. struct geneve_sock *gs4 = rtnl_dereference(geneve->sock4);
  467. #if IS_ENABLED(CONFIG_IPV6)
  468. struct geneve_sock *gs6 = rtnl_dereference(geneve->sock6);
  469. rcu_assign_pointer(geneve->sock6, NULL);
  470. #endif
  471. rcu_assign_pointer(geneve->sock4, NULL);
  472. synchronize_net();
  473. __geneve_sock_release(gs4);
  474. #if IS_ENABLED(CONFIG_IPV6)
  475. __geneve_sock_release(gs6);
  476. #endif
  477. }
  478. static struct geneve_sock *geneve_find_sock(struct geneve_net *gn,
  479. sa_family_t family,
  480. __be16 dst_port)
  481. {
  482. struct geneve_sock *gs;
  483. list_for_each_entry(gs, &gn->sock_list, list) {
  484. if (inet_sk(gs->sock->sk)->inet_sport == dst_port &&
  485. geneve_get_sk_family(gs) == family) {
  486. return gs;
  487. }
  488. }
  489. return NULL;
  490. }
  491. static int geneve_sock_add(struct geneve_dev *geneve, bool ipv6)
  492. {
  493. struct net *net = geneve->net;
  494. struct geneve_net *gn = net_generic(net, geneve_net_id);
  495. struct geneve_sock *gs;
  496. __u32 hash;
  497. gs = geneve_find_sock(gn, ipv6 ? AF_INET6 : AF_INET, geneve->dst_port);
  498. if (gs) {
  499. gs->refcnt++;
  500. goto out;
  501. }
  502. gs = geneve_socket_create(net, geneve->dst_port, ipv6, geneve->flags);
  503. if (IS_ERR(gs))
  504. return PTR_ERR(gs);
  505. out:
  506. gs->collect_md = geneve->collect_md;
  507. gs->flags = geneve->flags;
  508. #if IS_ENABLED(CONFIG_IPV6)
  509. if (ipv6)
  510. rcu_assign_pointer(geneve->sock6, gs);
  511. else
  512. #endif
  513. rcu_assign_pointer(geneve->sock4, gs);
  514. hash = geneve_net_vni_hash(geneve->vni);
  515. hlist_add_head_rcu(&geneve->hlist, &gs->vni_list[hash]);
  516. return 0;
  517. }
  518. static int geneve_open(struct net_device *dev)
  519. {
  520. struct geneve_dev *geneve = netdev_priv(dev);
  521. bool ipv6 = geneve->remote.sa.sa_family == AF_INET6;
  522. bool metadata = geneve->collect_md;
  523. int ret = 0;
  524. #if IS_ENABLED(CONFIG_IPV6)
  525. if (ipv6 || metadata)
  526. ret = geneve_sock_add(geneve, true);
  527. #endif
  528. if (!ret && (!ipv6 || metadata))
  529. ret = geneve_sock_add(geneve, false);
  530. if (ret < 0)
  531. geneve_sock_release(geneve);
  532. return ret;
  533. }
  534. static int geneve_stop(struct net_device *dev)
  535. {
  536. struct geneve_dev *geneve = netdev_priv(dev);
  537. if (!hlist_unhashed(&geneve->hlist))
  538. hlist_del_rcu(&geneve->hlist);
  539. geneve_sock_release(geneve);
  540. return 0;
  541. }
  542. static void geneve_build_header(struct genevehdr *geneveh,
  543. __be16 tun_flags, u8 vni[3],
  544. u8 options_len, u8 *options)
  545. {
  546. geneveh->ver = GENEVE_VER;
  547. geneveh->opt_len = options_len / 4;
  548. geneveh->oam = !!(tun_flags & TUNNEL_OAM);
  549. geneveh->critical = !!(tun_flags & TUNNEL_CRIT_OPT);
  550. geneveh->rsvd1 = 0;
  551. memcpy(geneveh->vni, vni, 3);
  552. geneveh->proto_type = htons(ETH_P_TEB);
  553. geneveh->rsvd2 = 0;
  554. memcpy(geneveh->options, options, options_len);
  555. }
  556. static int geneve_build_skb(struct rtable *rt, struct sk_buff *skb,
  557. __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
  558. u32 flags, bool xnet)
  559. {
  560. struct genevehdr *gnvh;
  561. int min_headroom;
  562. int err;
  563. bool udp_sum = !(flags & GENEVE_F_UDP_ZERO_CSUM_TX);
  564. skb_scrub_packet(skb, xnet);
  565. min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
  566. + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr);
  567. err = skb_cow_head(skb, min_headroom);
  568. if (unlikely(err))
  569. goto free_rt;
  570. err = udp_tunnel_handle_offloads(skb, udp_sum);
  571. if (err)
  572. goto free_rt;
  573. gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
  574. geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
  575. skb_set_inner_protocol(skb, htons(ETH_P_TEB));
  576. return 0;
  577. free_rt:
  578. ip_rt_put(rt);
  579. return err;
  580. }
  581. #if IS_ENABLED(CONFIG_IPV6)
  582. static int geneve6_build_skb(struct dst_entry *dst, struct sk_buff *skb,
  583. __be16 tun_flags, u8 vni[3], u8 opt_len, u8 *opt,
  584. u32 flags, bool xnet)
  585. {
  586. struct genevehdr *gnvh;
  587. int min_headroom;
  588. int err;
  589. bool udp_sum = !(flags & GENEVE_F_UDP_ZERO_CSUM6_TX);
  590. skb_scrub_packet(skb, xnet);
  591. min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
  592. + GENEVE_BASE_HLEN + opt_len + sizeof(struct ipv6hdr);
  593. err = skb_cow_head(skb, min_headroom);
  594. if (unlikely(err))
  595. goto free_dst;
  596. err = udp_tunnel_handle_offloads(skb, udp_sum);
  597. if (err)
  598. goto free_dst;
  599. gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
  600. geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
  601. skb_set_inner_protocol(skb, htons(ETH_P_TEB));
  602. return 0;
  603. free_dst:
  604. dst_release(dst);
  605. return err;
  606. }
  607. #endif
  608. static struct rtable *geneve_get_v4_rt(struct sk_buff *skb,
  609. struct net_device *dev,
  610. struct flowi4 *fl4,
  611. struct ip_tunnel_info *info)
  612. {
  613. bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
  614. struct geneve_dev *geneve = netdev_priv(dev);
  615. struct dst_cache *dst_cache;
  616. struct rtable *rt = NULL;
  617. __u8 tos;
  618. if (!rcu_dereference(geneve->sock4))
  619. return ERR_PTR(-EIO);
  620. memset(fl4, 0, sizeof(*fl4));
  621. fl4->flowi4_mark = skb->mark;
  622. fl4->flowi4_proto = IPPROTO_UDP;
  623. if (info) {
  624. fl4->daddr = info->key.u.ipv4.dst;
  625. fl4->saddr = info->key.u.ipv4.src;
  626. fl4->flowi4_tos = RT_TOS(info->key.tos);
  627. dst_cache = &info->dst_cache;
  628. } else {
  629. tos = geneve->tos;
  630. if (tos == 1) {
  631. const struct iphdr *iip = ip_hdr(skb);
  632. tos = ip_tunnel_get_dsfield(iip, skb);
  633. use_cache = false;
  634. }
  635. fl4->flowi4_tos = RT_TOS(tos);
  636. fl4->daddr = geneve->remote.sin.sin_addr.s_addr;
  637. dst_cache = &geneve->dst_cache;
  638. }
  639. if (use_cache) {
  640. rt = dst_cache_get_ip4(dst_cache, &fl4->saddr);
  641. if (rt)
  642. return rt;
  643. }
  644. rt = ip_route_output_key(geneve->net, fl4);
  645. if (IS_ERR(rt)) {
  646. netdev_dbg(dev, "no route to %pI4\n", &fl4->daddr);
  647. return ERR_PTR(-ENETUNREACH);
  648. }
  649. if (rt->dst.dev == dev) { /* is this necessary? */
  650. netdev_dbg(dev, "circular route to %pI4\n", &fl4->daddr);
  651. ip_rt_put(rt);
  652. return ERR_PTR(-ELOOP);
  653. }
  654. if (use_cache)
  655. dst_cache_set_ip4(dst_cache, &rt->dst, fl4->saddr);
  656. return rt;
  657. }
  658. #if IS_ENABLED(CONFIG_IPV6)
  659. static struct dst_entry *geneve_get_v6_dst(struct sk_buff *skb,
  660. struct net_device *dev,
  661. struct flowi6 *fl6,
  662. struct ip_tunnel_info *info)
  663. {
  664. bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
  665. struct geneve_dev *geneve = netdev_priv(dev);
  666. struct dst_entry *dst = NULL;
  667. struct dst_cache *dst_cache;
  668. struct geneve_sock *gs6;
  669. __u8 prio;
  670. gs6 = rcu_dereference(geneve->sock6);
  671. if (!gs6)
  672. return ERR_PTR(-EIO);
  673. memset(fl6, 0, sizeof(*fl6));
  674. fl6->flowi6_mark = skb->mark;
  675. fl6->flowi6_proto = IPPROTO_UDP;
  676. if (info) {
  677. fl6->daddr = info->key.u.ipv6.dst;
  678. fl6->saddr = info->key.u.ipv6.src;
  679. fl6->flowlabel = ip6_make_flowinfo(RT_TOS(info->key.tos),
  680. info->key.label);
  681. dst_cache = &info->dst_cache;
  682. } else {
  683. prio = geneve->tos;
  684. if (prio == 1) {
  685. const struct iphdr *iip = ip_hdr(skb);
  686. prio = ip_tunnel_get_dsfield(iip, skb);
  687. use_cache = false;
  688. }
  689. fl6->flowlabel = ip6_make_flowinfo(RT_TOS(prio),
  690. geneve->label);
  691. fl6->daddr = geneve->remote.sin6.sin6_addr;
  692. dst_cache = &geneve->dst_cache;
  693. }
  694. if (use_cache) {
  695. dst = dst_cache_get_ip6(dst_cache, &fl6->saddr);
  696. if (dst)
  697. return dst;
  698. }
  699. if (ipv6_stub->ipv6_dst_lookup(geneve->net, gs6->sock->sk, &dst, fl6)) {
  700. netdev_dbg(dev, "no route to %pI6\n", &fl6->daddr);
  701. return ERR_PTR(-ENETUNREACH);
  702. }
  703. if (dst->dev == dev) { /* is this necessary? */
  704. netdev_dbg(dev, "circular route to %pI6\n", &fl6->daddr);
  705. dst_release(dst);
  706. return ERR_PTR(-ELOOP);
  707. }
  708. if (use_cache)
  709. dst_cache_set_ip6(dst_cache, dst, &fl6->saddr);
  710. return dst;
  711. }
  712. #endif
  713. /* Convert 64 bit tunnel ID to 24 bit VNI. */
  714. static void tunnel_id_to_vni(__be64 tun_id, __u8 *vni)
  715. {
  716. #ifdef __BIG_ENDIAN
  717. vni[0] = (__force __u8)(tun_id >> 16);
  718. vni[1] = (__force __u8)(tun_id >> 8);
  719. vni[2] = (__force __u8)tun_id;
  720. #else
  721. vni[0] = (__force __u8)((__force u64)tun_id >> 40);
  722. vni[1] = (__force __u8)((__force u64)tun_id >> 48);
  723. vni[2] = (__force __u8)((__force u64)tun_id >> 56);
  724. #endif
  725. }
  726. static netdev_tx_t geneve_xmit_skb(struct sk_buff *skb, struct net_device *dev,
  727. struct ip_tunnel_info *info)
  728. {
  729. struct geneve_dev *geneve = netdev_priv(dev);
  730. struct geneve_sock *gs4;
  731. struct rtable *rt = NULL;
  732. int err = -EINVAL;
  733. struct flowi4 fl4;
  734. __u8 tos, ttl;
  735. __be16 sport;
  736. __be16 df;
  737. bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
  738. u32 flags = geneve->flags;
  739. gs4 = rcu_dereference(geneve->sock4);
  740. if (!gs4)
  741. goto tx_error;
  742. if (geneve->collect_md) {
  743. if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
  744. netdev_dbg(dev, "no tunnel metadata\n");
  745. goto tx_error;
  746. }
  747. if (info && ip_tunnel_info_af(info) != AF_INET)
  748. goto tx_error;
  749. }
  750. rt = geneve_get_v4_rt(skb, dev, &fl4, info);
  751. if (IS_ERR(rt)) {
  752. err = PTR_ERR(rt);
  753. goto tx_error;
  754. }
  755. sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
  756. skb_reset_mac_header(skb);
  757. if (info) {
  758. const struct ip_tunnel_key *key = &info->key;
  759. u8 *opts = NULL;
  760. u8 vni[3];
  761. tunnel_id_to_vni(key->tun_id, vni);
  762. if (info->options_len)
  763. opts = ip_tunnel_info_opts(info);
  764. if (key->tun_flags & TUNNEL_CSUM)
  765. flags &= ~GENEVE_F_UDP_ZERO_CSUM_TX;
  766. else
  767. flags |= GENEVE_F_UDP_ZERO_CSUM_TX;
  768. err = geneve_build_skb(rt, skb, key->tun_flags, vni,
  769. info->options_len, opts, flags, xnet);
  770. if (unlikely(err))
  771. goto tx_error;
  772. tos = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
  773. ttl = key->ttl;
  774. df = key->tun_flags & TUNNEL_DONT_FRAGMENT ? htons(IP_DF) : 0;
  775. } else {
  776. err = geneve_build_skb(rt, skb, 0, geneve->vni,
  777. 0, NULL, flags, xnet);
  778. if (unlikely(err))
  779. goto tx_error;
  780. tos = ip_tunnel_ecn_encap(fl4.flowi4_tos, ip_hdr(skb), skb);
  781. ttl = geneve->ttl;
  782. if (!ttl && IN_MULTICAST(ntohl(fl4.daddr)))
  783. ttl = 1;
  784. ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  785. df = 0;
  786. }
  787. udp_tunnel_xmit_skb(rt, gs4->sock->sk, skb, fl4.saddr, fl4.daddr,
  788. tos, ttl, df, sport, geneve->dst_port,
  789. !net_eq(geneve->net, dev_net(geneve->dev)),
  790. !!(flags & GENEVE_F_UDP_ZERO_CSUM_TX));
  791. return NETDEV_TX_OK;
  792. tx_error:
  793. dev_kfree_skb(skb);
  794. if (err == -ELOOP)
  795. dev->stats.collisions++;
  796. else if (err == -ENETUNREACH)
  797. dev->stats.tx_carrier_errors++;
  798. dev->stats.tx_errors++;
  799. return NETDEV_TX_OK;
  800. }
  801. #if IS_ENABLED(CONFIG_IPV6)
  802. static netdev_tx_t geneve6_xmit_skb(struct sk_buff *skb, struct net_device *dev,
  803. struct ip_tunnel_info *info)
  804. {
  805. struct geneve_dev *geneve = netdev_priv(dev);
  806. struct dst_entry *dst = NULL;
  807. struct geneve_sock *gs6;
  808. int err = -EINVAL;
  809. struct flowi6 fl6;
  810. __u8 prio, ttl;
  811. __be16 sport;
  812. __be32 label;
  813. bool xnet = !net_eq(geneve->net, dev_net(geneve->dev));
  814. u32 flags = geneve->flags;
  815. gs6 = rcu_dereference(geneve->sock6);
  816. if (!gs6)
  817. goto tx_error;
  818. if (geneve->collect_md) {
  819. if (unlikely(!info || !(info->mode & IP_TUNNEL_INFO_TX))) {
  820. netdev_dbg(dev, "no tunnel metadata\n");
  821. goto tx_error;
  822. }
  823. }
  824. dst = geneve_get_v6_dst(skb, dev, &fl6, info);
  825. if (IS_ERR(dst)) {
  826. err = PTR_ERR(dst);
  827. goto tx_error;
  828. }
  829. sport = udp_flow_src_port(geneve->net, skb, 1, USHRT_MAX, true);
  830. skb_reset_mac_header(skb);
  831. if (info) {
  832. const struct ip_tunnel_key *key = &info->key;
  833. u8 *opts = NULL;
  834. u8 vni[3];
  835. tunnel_id_to_vni(key->tun_id, vni);
  836. if (info->options_len)
  837. opts = ip_tunnel_info_opts(info);
  838. if (key->tun_flags & TUNNEL_CSUM)
  839. flags &= ~GENEVE_F_UDP_ZERO_CSUM6_TX;
  840. else
  841. flags |= GENEVE_F_UDP_ZERO_CSUM6_TX;
  842. err = geneve6_build_skb(dst, skb, key->tun_flags, vni,
  843. info->options_len, opts,
  844. flags, xnet);
  845. if (unlikely(err))
  846. goto tx_error;
  847. prio = ip_tunnel_ecn_encap(key->tos, ip_hdr(skb), skb);
  848. ttl = key->ttl;
  849. label = info->key.label;
  850. } else {
  851. err = geneve6_build_skb(dst, skb, 0, geneve->vni,
  852. 0, NULL, flags, xnet);
  853. if (unlikely(err))
  854. goto tx_error;
  855. prio = ip_tunnel_ecn_encap(ip6_tclass(fl6.flowlabel),
  856. ip_hdr(skb), skb);
  857. ttl = geneve->ttl;
  858. if (!ttl && ipv6_addr_is_multicast(&fl6.daddr))
  859. ttl = 1;
  860. ttl = ttl ? : ip6_dst_hoplimit(dst);
  861. label = geneve->label;
  862. }
  863. udp_tunnel6_xmit_skb(dst, gs6->sock->sk, skb, dev,
  864. &fl6.saddr, &fl6.daddr, prio, ttl, label,
  865. sport, geneve->dst_port,
  866. !!(flags & GENEVE_F_UDP_ZERO_CSUM6_TX));
  867. return NETDEV_TX_OK;
  868. tx_error:
  869. dev_kfree_skb(skb);
  870. if (err == -ELOOP)
  871. dev->stats.collisions++;
  872. else if (err == -ENETUNREACH)
  873. dev->stats.tx_carrier_errors++;
  874. dev->stats.tx_errors++;
  875. return NETDEV_TX_OK;
  876. }
  877. #endif
  878. static netdev_tx_t geneve_xmit(struct sk_buff *skb, struct net_device *dev)
  879. {
  880. struct geneve_dev *geneve = netdev_priv(dev);
  881. struct ip_tunnel_info *info = NULL;
  882. int err;
  883. if (geneve->collect_md)
  884. info = skb_tunnel_info(skb);
  885. rcu_read_lock();
  886. #if IS_ENABLED(CONFIG_IPV6)
  887. if ((info && ip_tunnel_info_af(info) == AF_INET6) ||
  888. (!info && geneve->remote.sa.sa_family == AF_INET6))
  889. err = geneve6_xmit_skb(skb, dev, info);
  890. else
  891. #endif
  892. err = geneve_xmit_skb(skb, dev, info);
  893. rcu_read_unlock();
  894. return err;
  895. }
  896. static int __geneve_change_mtu(struct net_device *dev, int new_mtu, bool strict)
  897. {
  898. struct geneve_dev *geneve = netdev_priv(dev);
  899. /* The max_mtu calculation does not take account of GENEVE
  900. * options, to avoid excluding potentially valid
  901. * configurations.
  902. */
  903. int max_mtu = IP_MAX_MTU - GENEVE_BASE_HLEN - dev->hard_header_len;
  904. if (geneve->remote.sa.sa_family == AF_INET6)
  905. max_mtu -= sizeof(struct ipv6hdr);
  906. else
  907. max_mtu -= sizeof(struct iphdr);
  908. if (new_mtu < 68)
  909. return -EINVAL;
  910. if (new_mtu > max_mtu) {
  911. if (strict)
  912. return -EINVAL;
  913. new_mtu = max_mtu;
  914. }
  915. dev->mtu = new_mtu;
  916. return 0;
  917. }
  918. static int geneve_change_mtu(struct net_device *dev, int new_mtu)
  919. {
  920. return __geneve_change_mtu(dev, new_mtu, true);
  921. }
  922. static int geneve_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
  923. {
  924. struct ip_tunnel_info *info = skb_tunnel_info(skb);
  925. struct geneve_dev *geneve = netdev_priv(dev);
  926. struct rtable *rt;
  927. struct flowi4 fl4;
  928. #if IS_ENABLED(CONFIG_IPV6)
  929. struct dst_entry *dst;
  930. struct flowi6 fl6;
  931. #endif
  932. if (ip_tunnel_info_af(info) == AF_INET) {
  933. rt = geneve_get_v4_rt(skb, dev, &fl4, info);
  934. if (IS_ERR(rt))
  935. return PTR_ERR(rt);
  936. ip_rt_put(rt);
  937. info->key.u.ipv4.src = fl4.saddr;
  938. #if IS_ENABLED(CONFIG_IPV6)
  939. } else if (ip_tunnel_info_af(info) == AF_INET6) {
  940. dst = geneve_get_v6_dst(skb, dev, &fl6, info);
  941. if (IS_ERR(dst))
  942. return PTR_ERR(dst);
  943. dst_release(dst);
  944. info->key.u.ipv6.src = fl6.saddr;
  945. #endif
  946. } else {
  947. return -EINVAL;
  948. }
  949. info->key.tp_src = udp_flow_src_port(geneve->net, skb,
  950. 1, USHRT_MAX, true);
  951. info->key.tp_dst = geneve->dst_port;
  952. return 0;
  953. }
  954. static const struct net_device_ops geneve_netdev_ops = {
  955. .ndo_init = geneve_init,
  956. .ndo_uninit = geneve_uninit,
  957. .ndo_open = geneve_open,
  958. .ndo_stop = geneve_stop,
  959. .ndo_start_xmit = geneve_xmit,
  960. .ndo_get_stats64 = ip_tunnel_get_stats64,
  961. .ndo_change_mtu = geneve_change_mtu,
  962. .ndo_validate_addr = eth_validate_addr,
  963. .ndo_set_mac_address = eth_mac_addr,
  964. .ndo_fill_metadata_dst = geneve_fill_metadata_dst,
  965. };
  966. static void geneve_get_drvinfo(struct net_device *dev,
  967. struct ethtool_drvinfo *drvinfo)
  968. {
  969. strlcpy(drvinfo->version, GENEVE_NETDEV_VER, sizeof(drvinfo->version));
  970. strlcpy(drvinfo->driver, "geneve", sizeof(drvinfo->driver));
  971. }
  972. static const struct ethtool_ops geneve_ethtool_ops = {
  973. .get_drvinfo = geneve_get_drvinfo,
  974. .get_link = ethtool_op_get_link,
  975. };
  976. /* Info for udev, that this is a virtual tunnel endpoint */
  977. static struct device_type geneve_type = {
  978. .name = "geneve",
  979. };
  980. /* Calls the ndo_udp_tunnel_add of the caller in order to
  981. * supply the listening GENEVE udp ports. Callers are expected
  982. * to implement the ndo_udp_tunnel_add.
  983. */
  984. static void geneve_push_rx_ports(struct net_device *dev)
  985. {
  986. struct net *net = dev_net(dev);
  987. struct geneve_net *gn = net_generic(net, geneve_net_id);
  988. struct geneve_sock *gs;
  989. rcu_read_lock();
  990. list_for_each_entry_rcu(gs, &gn->sock_list, list)
  991. udp_tunnel_push_rx_port(dev, gs->sock,
  992. UDP_TUNNEL_TYPE_GENEVE);
  993. rcu_read_unlock();
  994. }
  995. /* Initialize the device structure. */
  996. static void geneve_setup(struct net_device *dev)
  997. {
  998. ether_setup(dev);
  999. dev->netdev_ops = &geneve_netdev_ops;
  1000. dev->ethtool_ops = &geneve_ethtool_ops;
  1001. dev->destructor = free_netdev;
  1002. SET_NETDEV_DEVTYPE(dev, &geneve_type);
  1003. dev->features |= NETIF_F_LLTX;
  1004. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  1005. dev->features |= NETIF_F_RXCSUM;
  1006. dev->features |= NETIF_F_GSO_SOFTWARE;
  1007. dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  1008. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1009. netif_keep_dst(dev);
  1010. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  1011. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE | IFF_NO_QUEUE;
  1012. eth_hw_addr_random(dev);
  1013. }
  1014. static const struct nla_policy geneve_policy[IFLA_GENEVE_MAX + 1] = {
  1015. [IFLA_GENEVE_ID] = { .type = NLA_U32 },
  1016. [IFLA_GENEVE_REMOTE] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  1017. [IFLA_GENEVE_REMOTE6] = { .len = sizeof(struct in6_addr) },
  1018. [IFLA_GENEVE_TTL] = { .type = NLA_U8 },
  1019. [IFLA_GENEVE_TOS] = { .type = NLA_U8 },
  1020. [IFLA_GENEVE_LABEL] = { .type = NLA_U32 },
  1021. [IFLA_GENEVE_PORT] = { .type = NLA_U16 },
  1022. [IFLA_GENEVE_COLLECT_METADATA] = { .type = NLA_FLAG },
  1023. [IFLA_GENEVE_UDP_CSUM] = { .type = NLA_U8 },
  1024. [IFLA_GENEVE_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
  1025. [IFLA_GENEVE_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
  1026. };
  1027. static int geneve_validate(struct nlattr *tb[], struct nlattr *data[])
  1028. {
  1029. if (tb[IFLA_ADDRESS]) {
  1030. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  1031. return -EINVAL;
  1032. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1033. return -EADDRNOTAVAIL;
  1034. }
  1035. if (!data)
  1036. return -EINVAL;
  1037. if (data[IFLA_GENEVE_ID]) {
  1038. __u32 vni = nla_get_u32(data[IFLA_GENEVE_ID]);
  1039. if (vni >= GENEVE_VID_MASK)
  1040. return -ERANGE;
  1041. }
  1042. return 0;
  1043. }
  1044. static struct geneve_dev *geneve_find_dev(struct geneve_net *gn,
  1045. __be16 dst_port,
  1046. union geneve_addr *remote,
  1047. u8 vni[],
  1048. bool *tun_on_same_port,
  1049. bool *tun_collect_md)
  1050. {
  1051. struct geneve_dev *geneve, *t;
  1052. *tun_on_same_port = false;
  1053. *tun_collect_md = false;
  1054. t = NULL;
  1055. list_for_each_entry(geneve, &gn->geneve_list, next) {
  1056. if (geneve->dst_port == dst_port) {
  1057. *tun_collect_md = geneve->collect_md;
  1058. *tun_on_same_port = true;
  1059. }
  1060. if (!memcmp(vni, geneve->vni, sizeof(geneve->vni)) &&
  1061. !memcmp(remote, &geneve->remote, sizeof(geneve->remote)) &&
  1062. dst_port == geneve->dst_port)
  1063. t = geneve;
  1064. }
  1065. return t;
  1066. }
  1067. static int geneve_configure(struct net *net, struct net_device *dev,
  1068. union geneve_addr *remote,
  1069. __u32 vni, __u8 ttl, __u8 tos, __be32 label,
  1070. __be16 dst_port, bool metadata, u32 flags)
  1071. {
  1072. struct geneve_net *gn = net_generic(net, geneve_net_id);
  1073. struct geneve_dev *t, *geneve = netdev_priv(dev);
  1074. bool tun_collect_md, tun_on_same_port;
  1075. int err, encap_len;
  1076. if (!remote)
  1077. return -EINVAL;
  1078. if (metadata &&
  1079. (remote->sa.sa_family != AF_UNSPEC || vni || tos || ttl || label))
  1080. return -EINVAL;
  1081. geneve->net = net;
  1082. geneve->dev = dev;
  1083. geneve->vni[0] = (vni & 0x00ff0000) >> 16;
  1084. geneve->vni[1] = (vni & 0x0000ff00) >> 8;
  1085. geneve->vni[2] = vni & 0x000000ff;
  1086. if ((remote->sa.sa_family == AF_INET &&
  1087. IN_MULTICAST(ntohl(remote->sin.sin_addr.s_addr))) ||
  1088. (remote->sa.sa_family == AF_INET6 &&
  1089. ipv6_addr_is_multicast(&remote->sin6.sin6_addr)))
  1090. return -EINVAL;
  1091. if (label && remote->sa.sa_family != AF_INET6)
  1092. return -EINVAL;
  1093. geneve->remote = *remote;
  1094. geneve->ttl = ttl;
  1095. geneve->tos = tos;
  1096. geneve->label = label;
  1097. geneve->dst_port = dst_port;
  1098. geneve->collect_md = metadata;
  1099. geneve->flags = flags;
  1100. t = geneve_find_dev(gn, dst_port, remote, geneve->vni,
  1101. &tun_on_same_port, &tun_collect_md);
  1102. if (t)
  1103. return -EBUSY;
  1104. /* make enough headroom for basic scenario */
  1105. encap_len = GENEVE_BASE_HLEN + ETH_HLEN;
  1106. if (remote->sa.sa_family == AF_INET)
  1107. encap_len += sizeof(struct iphdr);
  1108. else
  1109. encap_len += sizeof(struct ipv6hdr);
  1110. dev->needed_headroom = encap_len + ETH_HLEN;
  1111. if (metadata) {
  1112. if (tun_on_same_port)
  1113. return -EPERM;
  1114. } else {
  1115. if (tun_collect_md)
  1116. return -EPERM;
  1117. }
  1118. dst_cache_reset(&geneve->dst_cache);
  1119. err = register_netdevice(dev);
  1120. if (err)
  1121. return err;
  1122. list_add(&geneve->next, &gn->geneve_list);
  1123. return 0;
  1124. }
  1125. static int geneve_newlink(struct net *net, struct net_device *dev,
  1126. struct nlattr *tb[], struct nlattr *data[])
  1127. {
  1128. __be16 dst_port = htons(GENEVE_UDP_PORT);
  1129. __u8 ttl = 0, tos = 0;
  1130. bool metadata = false;
  1131. union geneve_addr remote = geneve_remote_unspec;
  1132. __be32 label = 0;
  1133. __u32 vni = 0;
  1134. u32 flags = 0;
  1135. if (data[IFLA_GENEVE_REMOTE] && data[IFLA_GENEVE_REMOTE6])
  1136. return -EINVAL;
  1137. if (data[IFLA_GENEVE_REMOTE]) {
  1138. remote.sa.sa_family = AF_INET;
  1139. remote.sin.sin_addr.s_addr =
  1140. nla_get_in_addr(data[IFLA_GENEVE_REMOTE]);
  1141. }
  1142. if (data[IFLA_GENEVE_REMOTE6]) {
  1143. if (!IS_ENABLED(CONFIG_IPV6))
  1144. return -EPFNOSUPPORT;
  1145. remote.sa.sa_family = AF_INET6;
  1146. remote.sin6.sin6_addr =
  1147. nla_get_in6_addr(data[IFLA_GENEVE_REMOTE6]);
  1148. if (ipv6_addr_type(&remote.sin6.sin6_addr) &
  1149. IPV6_ADDR_LINKLOCAL) {
  1150. netdev_dbg(dev, "link-local remote is unsupported\n");
  1151. return -EINVAL;
  1152. }
  1153. }
  1154. if (data[IFLA_GENEVE_ID])
  1155. vni = nla_get_u32(data[IFLA_GENEVE_ID]);
  1156. if (data[IFLA_GENEVE_TTL])
  1157. ttl = nla_get_u8(data[IFLA_GENEVE_TTL]);
  1158. if (data[IFLA_GENEVE_TOS])
  1159. tos = nla_get_u8(data[IFLA_GENEVE_TOS]);
  1160. if (data[IFLA_GENEVE_LABEL])
  1161. label = nla_get_be32(data[IFLA_GENEVE_LABEL]) &
  1162. IPV6_FLOWLABEL_MASK;
  1163. if (data[IFLA_GENEVE_PORT])
  1164. dst_port = nla_get_be16(data[IFLA_GENEVE_PORT]);
  1165. if (data[IFLA_GENEVE_COLLECT_METADATA])
  1166. metadata = true;
  1167. if (data[IFLA_GENEVE_UDP_CSUM] &&
  1168. !nla_get_u8(data[IFLA_GENEVE_UDP_CSUM]))
  1169. flags |= GENEVE_F_UDP_ZERO_CSUM_TX;
  1170. if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX] &&
  1171. nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_TX]))
  1172. flags |= GENEVE_F_UDP_ZERO_CSUM6_TX;
  1173. if (data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX] &&
  1174. nla_get_u8(data[IFLA_GENEVE_UDP_ZERO_CSUM6_RX]))
  1175. flags |= GENEVE_F_UDP_ZERO_CSUM6_RX;
  1176. return geneve_configure(net, dev, &remote, vni, ttl, tos, label,
  1177. dst_port, metadata, flags);
  1178. }
  1179. static void geneve_dellink(struct net_device *dev, struct list_head *head)
  1180. {
  1181. struct geneve_dev *geneve = netdev_priv(dev);
  1182. list_del(&geneve->next);
  1183. unregister_netdevice_queue(dev, head);
  1184. }
  1185. static size_t geneve_get_size(const struct net_device *dev)
  1186. {
  1187. return nla_total_size(sizeof(__u32)) + /* IFLA_GENEVE_ID */
  1188. nla_total_size(sizeof(struct in6_addr)) + /* IFLA_GENEVE_REMOTE{6} */
  1189. nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TTL */
  1190. nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_TOS */
  1191. nla_total_size(sizeof(__be32)) + /* IFLA_GENEVE_LABEL */
  1192. nla_total_size(sizeof(__be16)) + /* IFLA_GENEVE_PORT */
  1193. nla_total_size(0) + /* IFLA_GENEVE_COLLECT_METADATA */
  1194. nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_CSUM */
  1195. nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_TX */
  1196. nla_total_size(sizeof(__u8)) + /* IFLA_GENEVE_UDP_ZERO_CSUM6_RX */
  1197. 0;
  1198. }
  1199. static int geneve_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1200. {
  1201. struct geneve_dev *geneve = netdev_priv(dev);
  1202. __u32 vni;
  1203. vni = (geneve->vni[0] << 16) | (geneve->vni[1] << 8) | geneve->vni[2];
  1204. if (nla_put_u32(skb, IFLA_GENEVE_ID, vni))
  1205. goto nla_put_failure;
  1206. if (geneve->remote.sa.sa_family == AF_INET) {
  1207. if (nla_put_in_addr(skb, IFLA_GENEVE_REMOTE,
  1208. geneve->remote.sin.sin_addr.s_addr))
  1209. goto nla_put_failure;
  1210. #if IS_ENABLED(CONFIG_IPV6)
  1211. } else {
  1212. if (nla_put_in6_addr(skb, IFLA_GENEVE_REMOTE6,
  1213. &geneve->remote.sin6.sin6_addr))
  1214. goto nla_put_failure;
  1215. #endif
  1216. }
  1217. if (nla_put_u8(skb, IFLA_GENEVE_TTL, geneve->ttl) ||
  1218. nla_put_u8(skb, IFLA_GENEVE_TOS, geneve->tos) ||
  1219. nla_put_be32(skb, IFLA_GENEVE_LABEL, geneve->label))
  1220. goto nla_put_failure;
  1221. if (nla_put_be16(skb, IFLA_GENEVE_PORT, geneve->dst_port))
  1222. goto nla_put_failure;
  1223. if (geneve->collect_md) {
  1224. if (nla_put_flag(skb, IFLA_GENEVE_COLLECT_METADATA))
  1225. goto nla_put_failure;
  1226. }
  1227. if (nla_put_u8(skb, IFLA_GENEVE_UDP_CSUM,
  1228. !(geneve->flags & GENEVE_F_UDP_ZERO_CSUM_TX)) ||
  1229. nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_TX,
  1230. !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_TX)) ||
  1231. nla_put_u8(skb, IFLA_GENEVE_UDP_ZERO_CSUM6_RX,
  1232. !!(geneve->flags & GENEVE_F_UDP_ZERO_CSUM6_RX)))
  1233. goto nla_put_failure;
  1234. return 0;
  1235. nla_put_failure:
  1236. return -EMSGSIZE;
  1237. }
  1238. static struct rtnl_link_ops geneve_link_ops __read_mostly = {
  1239. .kind = "geneve",
  1240. .maxtype = IFLA_GENEVE_MAX,
  1241. .policy = geneve_policy,
  1242. .priv_size = sizeof(struct geneve_dev),
  1243. .setup = geneve_setup,
  1244. .validate = geneve_validate,
  1245. .newlink = geneve_newlink,
  1246. .dellink = geneve_dellink,
  1247. .get_size = geneve_get_size,
  1248. .fill_info = geneve_fill_info,
  1249. };
  1250. struct net_device *geneve_dev_create_fb(struct net *net, const char *name,
  1251. u8 name_assign_type, u16 dst_port)
  1252. {
  1253. struct nlattr *tb[IFLA_MAX + 1];
  1254. struct net_device *dev;
  1255. LIST_HEAD(list_kill);
  1256. int err;
  1257. memset(tb, 0, sizeof(tb));
  1258. dev = rtnl_create_link(net, name, name_assign_type,
  1259. &geneve_link_ops, tb);
  1260. if (IS_ERR(dev))
  1261. return dev;
  1262. err = geneve_configure(net, dev, &geneve_remote_unspec,
  1263. 0, 0, 0, 0, htons(dst_port), true,
  1264. GENEVE_F_UDP_ZERO_CSUM6_RX);
  1265. if (err) {
  1266. free_netdev(dev);
  1267. return ERR_PTR(err);
  1268. }
  1269. /* openvswitch users expect packet sizes to be unrestricted,
  1270. * so set the largest MTU we can.
  1271. */
  1272. err = __geneve_change_mtu(dev, IP_MAX_MTU, false);
  1273. if (err)
  1274. goto err;
  1275. err = rtnl_configure_link(dev, NULL);
  1276. if (err < 0)
  1277. goto err;
  1278. return dev;
  1279. err:
  1280. geneve_dellink(dev, &list_kill);
  1281. unregister_netdevice_many(&list_kill);
  1282. return ERR_PTR(err);
  1283. }
  1284. EXPORT_SYMBOL_GPL(geneve_dev_create_fb);
  1285. static int geneve_netdevice_event(struct notifier_block *unused,
  1286. unsigned long event, void *ptr)
  1287. {
  1288. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1289. if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
  1290. geneve_push_rx_ports(dev);
  1291. return NOTIFY_DONE;
  1292. }
  1293. static struct notifier_block geneve_notifier_block __read_mostly = {
  1294. .notifier_call = geneve_netdevice_event,
  1295. };
  1296. static __net_init int geneve_init_net(struct net *net)
  1297. {
  1298. struct geneve_net *gn = net_generic(net, geneve_net_id);
  1299. INIT_LIST_HEAD(&gn->geneve_list);
  1300. INIT_LIST_HEAD(&gn->sock_list);
  1301. return 0;
  1302. }
  1303. static void __net_exit geneve_exit_net(struct net *net)
  1304. {
  1305. struct geneve_net *gn = net_generic(net, geneve_net_id);
  1306. struct geneve_dev *geneve, *next;
  1307. struct net_device *dev, *aux;
  1308. LIST_HEAD(list);
  1309. rtnl_lock();
  1310. /* gather any geneve devices that were moved into this ns */
  1311. for_each_netdev_safe(net, dev, aux)
  1312. if (dev->rtnl_link_ops == &geneve_link_ops)
  1313. unregister_netdevice_queue(dev, &list);
  1314. /* now gather any other geneve devices that were created in this ns */
  1315. list_for_each_entry_safe(geneve, next, &gn->geneve_list, next) {
  1316. /* If geneve->dev is in the same netns, it was already added
  1317. * to the list by the previous loop.
  1318. */
  1319. if (!net_eq(dev_net(geneve->dev), net))
  1320. unregister_netdevice_queue(geneve->dev, &list);
  1321. }
  1322. /* unregister the devices gathered above */
  1323. unregister_netdevice_many(&list);
  1324. rtnl_unlock();
  1325. }
  1326. static struct pernet_operations geneve_net_ops = {
  1327. .init = geneve_init_net,
  1328. .exit = geneve_exit_net,
  1329. .id = &geneve_net_id,
  1330. .size = sizeof(struct geneve_net),
  1331. };
  1332. static int __init geneve_init_module(void)
  1333. {
  1334. int rc;
  1335. rc = register_pernet_subsys(&geneve_net_ops);
  1336. if (rc)
  1337. goto out1;
  1338. rc = register_netdevice_notifier(&geneve_notifier_block);
  1339. if (rc)
  1340. goto out2;
  1341. rc = rtnl_link_register(&geneve_link_ops);
  1342. if (rc)
  1343. goto out3;
  1344. return 0;
  1345. out3:
  1346. unregister_netdevice_notifier(&geneve_notifier_block);
  1347. out2:
  1348. unregister_pernet_subsys(&geneve_net_ops);
  1349. out1:
  1350. return rc;
  1351. }
  1352. late_initcall(geneve_init_module);
  1353. static void __exit geneve_cleanup_module(void)
  1354. {
  1355. rtnl_link_unregister(&geneve_link_ops);
  1356. unregister_netdevice_notifier(&geneve_notifier_block);
  1357. unregister_pernet_subsys(&geneve_net_ops);
  1358. }
  1359. module_exit(geneve_cleanup_module);
  1360. MODULE_LICENSE("GPL");
  1361. MODULE_VERSION(GENEVE_NETDEV_VER);
  1362. MODULE_AUTHOR("John W. Linville <linville@tuxdriver.com>");
  1363. MODULE_DESCRIPTION("Interface driver for GENEVE encapsulated traffic");
  1364. MODULE_ALIAS_RTNL_LINK("geneve");