geneve.c 50 KB

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