vxlan.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  1. /*
  2. * VXLAN: Virtual eXtensible Local Area Network
  3. *
  4. * Copyright (c) 2012-2013 Vyatta 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. * TODO
  11. * - IPv6 (not in RFC)
  12. */
  13. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/kernel.h>
  15. #include <linux/types.h>
  16. #include <linux/module.h>
  17. #include <linux/errno.h>
  18. #include <linux/slab.h>
  19. #include <linux/skbuff.h>
  20. #include <linux/rculist.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/in.h>
  23. #include <linux/ip.h>
  24. #include <linux/udp.h>
  25. #include <linux/igmp.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/hash.h>
  29. #include <linux/ethtool.h>
  30. #include <net/arp.h>
  31. #include <net/ndisc.h>
  32. #include <net/ip.h>
  33. #include <net/ip_tunnels.h>
  34. #include <net/icmp.h>
  35. #include <net/udp.h>
  36. #include <net/rtnetlink.h>
  37. #include <net/route.h>
  38. #include <net/dsfield.h>
  39. #include <net/inet_ecn.h>
  40. #include <net/net_namespace.h>
  41. #include <net/netns/generic.h>
  42. #include <net/vxlan.h>
  43. #define VXLAN_VERSION "0.1"
  44. #define PORT_HASH_BITS 8
  45. #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
  46. #define VNI_HASH_BITS 10
  47. #define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
  48. #define FDB_HASH_BITS 8
  49. #define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
  50. #define FDB_AGE_DEFAULT 300 /* 5 min */
  51. #define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
  52. #define VXLAN_N_VID (1u << 24)
  53. #define VXLAN_VID_MASK (VXLAN_N_VID - 1)
  54. /* IP header + UDP + VXLAN + Ethernet header */
  55. #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
  56. #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
  57. #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
  58. /* VXLAN protocol header */
  59. struct vxlanhdr {
  60. __be32 vx_flags;
  61. __be32 vx_vni;
  62. };
  63. /* UDP port for VXLAN traffic.
  64. * The IANA assigned port is 4789, but the Linux default is 8472
  65. * for compatibility with early adopters.
  66. */
  67. static unsigned short vxlan_port __read_mostly = 8472;
  68. module_param_named(udp_port, vxlan_port, ushort, 0444);
  69. MODULE_PARM_DESC(udp_port, "Destination UDP port");
  70. static bool log_ecn_error = true;
  71. module_param(log_ecn_error, bool, 0644);
  72. MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
  73. static int vxlan_net_id;
  74. static const u8 all_zeros_mac[ETH_ALEN];
  75. /* per-network namespace private data for this module */
  76. struct vxlan_net {
  77. struct list_head vxlan_list;
  78. struct hlist_head sock_list[PORT_HASH_SIZE];
  79. spinlock_t sock_lock;
  80. };
  81. struct vxlan_rdst {
  82. __be32 remote_ip;
  83. __be16 remote_port;
  84. u32 remote_vni;
  85. u32 remote_ifindex;
  86. struct list_head list;
  87. struct rcu_head rcu;
  88. };
  89. /* Forwarding table entry */
  90. struct vxlan_fdb {
  91. struct hlist_node hlist; /* linked list of entries */
  92. struct rcu_head rcu;
  93. unsigned long updated; /* jiffies */
  94. unsigned long used;
  95. struct list_head remotes;
  96. u16 state; /* see ndm_state */
  97. u8 flags; /* see ndm_flags */
  98. u8 eth_addr[ETH_ALEN];
  99. };
  100. /* Pseudo network device */
  101. struct vxlan_dev {
  102. struct hlist_node hlist; /* vni hash table */
  103. struct list_head next; /* vxlan's per namespace list */
  104. struct vxlan_sock *vn_sock; /* listening socket */
  105. struct net_device *dev;
  106. struct vxlan_rdst default_dst; /* default destination */
  107. __be32 saddr; /* source address */
  108. __be16 dst_port;
  109. __u16 port_min; /* source port range */
  110. __u16 port_max;
  111. __u8 tos; /* TOS override */
  112. __u8 ttl;
  113. u32 flags; /* VXLAN_F_* below */
  114. struct work_struct sock_work;
  115. struct work_struct igmp_join;
  116. struct work_struct igmp_leave;
  117. unsigned long age_interval;
  118. struct timer_list age_timer;
  119. spinlock_t hash_lock;
  120. unsigned int addrcnt;
  121. unsigned int addrmax;
  122. struct hlist_head fdb_head[FDB_HASH_SIZE];
  123. };
  124. #define VXLAN_F_LEARN 0x01
  125. #define VXLAN_F_PROXY 0x02
  126. #define VXLAN_F_RSC 0x04
  127. #define VXLAN_F_L2MISS 0x08
  128. #define VXLAN_F_L3MISS 0x10
  129. /* salt for hash table */
  130. static u32 vxlan_salt __read_mostly;
  131. static struct workqueue_struct *vxlan_wq;
  132. static void vxlan_sock_work(struct work_struct *work);
  133. /* Virtual Network hash table head */
  134. static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
  135. {
  136. return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
  137. }
  138. /* Socket hash table head */
  139. static inline struct hlist_head *vs_head(struct net *net, __be16 port)
  140. {
  141. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  142. return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
  143. }
  144. /* First remote destination for a forwarding entry.
  145. * Guaranteed to be non-NULL because remotes are never deleted.
  146. */
  147. static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
  148. {
  149. return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
  150. }
  151. static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
  152. {
  153. return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
  154. }
  155. /* Find VXLAN socket based on network namespace and UDP port */
  156. static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
  157. {
  158. struct vxlan_sock *vs;
  159. hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
  160. if (inet_sk(vs->sock->sk)->inet_sport == port)
  161. return vs;
  162. }
  163. return NULL;
  164. }
  165. static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
  166. {
  167. struct vxlan_dev *vxlan;
  168. hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
  169. if (vxlan->default_dst.remote_vni == id)
  170. return vxlan;
  171. }
  172. return NULL;
  173. }
  174. /* Look up VNI in a per net namespace table */
  175. static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
  176. {
  177. struct vxlan_sock *vs;
  178. vs = vxlan_find_sock(net, port);
  179. if (!vs)
  180. return NULL;
  181. return vxlan_vs_find_vni(vs, id);
  182. }
  183. /* Fill in neighbour message in skbuff. */
  184. static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
  185. const struct vxlan_fdb *fdb,
  186. u32 portid, u32 seq, int type, unsigned int flags,
  187. const struct vxlan_rdst *rdst)
  188. {
  189. unsigned long now = jiffies;
  190. struct nda_cacheinfo ci;
  191. struct nlmsghdr *nlh;
  192. struct ndmsg *ndm;
  193. bool send_ip, send_eth;
  194. nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
  195. if (nlh == NULL)
  196. return -EMSGSIZE;
  197. ndm = nlmsg_data(nlh);
  198. memset(ndm, 0, sizeof(*ndm));
  199. send_eth = send_ip = true;
  200. if (type == RTM_GETNEIGH) {
  201. ndm->ndm_family = AF_INET;
  202. send_ip = rdst->remote_ip != htonl(INADDR_ANY);
  203. send_eth = !is_zero_ether_addr(fdb->eth_addr);
  204. } else
  205. ndm->ndm_family = AF_BRIDGE;
  206. ndm->ndm_state = fdb->state;
  207. ndm->ndm_ifindex = vxlan->dev->ifindex;
  208. ndm->ndm_flags = fdb->flags;
  209. ndm->ndm_type = NDA_DST;
  210. if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
  211. goto nla_put_failure;
  212. if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
  213. goto nla_put_failure;
  214. if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
  215. nla_put_be16(skb, NDA_PORT, rdst->remote_port))
  216. goto nla_put_failure;
  217. if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
  218. nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
  219. goto nla_put_failure;
  220. if (rdst->remote_ifindex &&
  221. nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
  222. goto nla_put_failure;
  223. ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
  224. ci.ndm_confirmed = 0;
  225. ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
  226. ci.ndm_refcnt = 0;
  227. if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
  228. goto nla_put_failure;
  229. return nlmsg_end(skb, nlh);
  230. nla_put_failure:
  231. nlmsg_cancel(skb, nlh);
  232. return -EMSGSIZE;
  233. }
  234. static inline size_t vxlan_nlmsg_size(void)
  235. {
  236. return NLMSG_ALIGN(sizeof(struct ndmsg))
  237. + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
  238. + nla_total_size(sizeof(__be32)) /* NDA_DST */
  239. + nla_total_size(sizeof(__be16)) /* NDA_PORT */
  240. + nla_total_size(sizeof(__be32)) /* NDA_VNI */
  241. + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
  242. + nla_total_size(sizeof(struct nda_cacheinfo));
  243. }
  244. static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
  245. struct vxlan_fdb *fdb, int type)
  246. {
  247. struct net *net = dev_net(vxlan->dev);
  248. struct sk_buff *skb;
  249. int err = -ENOBUFS;
  250. skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
  251. if (skb == NULL)
  252. goto errout;
  253. err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0,
  254. first_remote_rtnl(fdb));
  255. if (err < 0) {
  256. /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
  257. WARN_ON(err == -EMSGSIZE);
  258. kfree_skb(skb);
  259. goto errout;
  260. }
  261. rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
  262. return;
  263. errout:
  264. if (err < 0)
  265. rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
  266. }
  267. static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
  268. {
  269. struct vxlan_dev *vxlan = netdev_priv(dev);
  270. struct vxlan_fdb f = {
  271. .state = NUD_STALE,
  272. };
  273. struct vxlan_rdst remote = {
  274. .remote_ip = ipa, /* goes to NDA_DST */
  275. .remote_vni = VXLAN_N_VID,
  276. };
  277. INIT_LIST_HEAD(&f.remotes);
  278. list_add_rcu(&remote.list, &f.remotes);
  279. vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
  280. }
  281. static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
  282. {
  283. struct vxlan_fdb f = {
  284. .state = NUD_STALE,
  285. };
  286. INIT_LIST_HEAD(&f.remotes);
  287. memcpy(f.eth_addr, eth_addr, ETH_ALEN);
  288. vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
  289. }
  290. /* Hash Ethernet address */
  291. static u32 eth_hash(const unsigned char *addr)
  292. {
  293. u64 value = get_unaligned((u64 *)addr);
  294. /* only want 6 bytes */
  295. #ifdef __BIG_ENDIAN
  296. value >>= 16;
  297. #else
  298. value <<= 16;
  299. #endif
  300. return hash_64(value, FDB_HASH_BITS);
  301. }
  302. /* Hash chain to use given mac address */
  303. static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
  304. const u8 *mac)
  305. {
  306. return &vxlan->fdb_head[eth_hash(mac)];
  307. }
  308. /* Look up Ethernet address in forwarding table */
  309. static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
  310. const u8 *mac)
  311. {
  312. struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
  313. struct vxlan_fdb *f;
  314. hlist_for_each_entry_rcu(f, head, hlist) {
  315. if (compare_ether_addr(mac, f->eth_addr) == 0)
  316. return f;
  317. }
  318. return NULL;
  319. }
  320. static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
  321. const u8 *mac)
  322. {
  323. struct vxlan_fdb *f;
  324. f = __vxlan_find_mac(vxlan, mac);
  325. if (f)
  326. f->used = jiffies;
  327. return f;
  328. }
  329. /* caller should hold vxlan->hash_lock */
  330. static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
  331. __be32 ip, __be16 port,
  332. __u32 vni, __u32 ifindex)
  333. {
  334. struct vxlan_rdst *rd;
  335. list_for_each_entry(rd, &f->remotes, list) {
  336. if (rd->remote_ip == ip &&
  337. rd->remote_port == port &&
  338. rd->remote_vni == vni &&
  339. rd->remote_ifindex == ifindex)
  340. return rd;
  341. }
  342. return NULL;
  343. }
  344. /* Replace destination of unicast mac */
  345. static int vxlan_fdb_replace(struct vxlan_fdb *f,
  346. __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
  347. {
  348. struct vxlan_rdst *rd;
  349. rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
  350. if (rd)
  351. return 0;
  352. rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
  353. if (!rd)
  354. return 0;
  355. rd->remote_ip = ip;
  356. rd->remote_port = port;
  357. rd->remote_vni = vni;
  358. rd->remote_ifindex = ifindex;
  359. return 1;
  360. }
  361. /* Add/update destinations for multicast */
  362. static int vxlan_fdb_append(struct vxlan_fdb *f,
  363. __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
  364. {
  365. struct vxlan_rdst *rd;
  366. rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
  367. if (rd)
  368. return 0;
  369. rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
  370. if (rd == NULL)
  371. return -ENOBUFS;
  372. rd->remote_ip = ip;
  373. rd->remote_port = port;
  374. rd->remote_vni = vni;
  375. rd->remote_ifindex = ifindex;
  376. list_add_tail_rcu(&rd->list, &f->remotes);
  377. return 1;
  378. }
  379. /* Add new entry to forwarding table -- assumes lock held */
  380. static int vxlan_fdb_create(struct vxlan_dev *vxlan,
  381. const u8 *mac, __be32 ip,
  382. __u16 state, __u16 flags,
  383. __be16 port, __u32 vni, __u32 ifindex,
  384. __u8 ndm_flags)
  385. {
  386. struct vxlan_fdb *f;
  387. int notify = 0;
  388. f = __vxlan_find_mac(vxlan, mac);
  389. if (f) {
  390. if (flags & NLM_F_EXCL) {
  391. netdev_dbg(vxlan->dev,
  392. "lost race to create %pM\n", mac);
  393. return -EEXIST;
  394. }
  395. if (f->state != state) {
  396. f->state = state;
  397. f->updated = jiffies;
  398. notify = 1;
  399. }
  400. if (f->flags != ndm_flags) {
  401. f->flags = ndm_flags;
  402. f->updated = jiffies;
  403. notify = 1;
  404. }
  405. if ((flags & NLM_F_REPLACE)) {
  406. /* Only change unicasts */
  407. if (!(is_multicast_ether_addr(f->eth_addr) ||
  408. is_zero_ether_addr(f->eth_addr))) {
  409. int rc = vxlan_fdb_replace(f, ip, port, vni,
  410. ifindex);
  411. if (rc < 0)
  412. return rc;
  413. notify |= rc;
  414. } else
  415. return -EOPNOTSUPP;
  416. }
  417. if ((flags & NLM_F_APPEND) &&
  418. (is_multicast_ether_addr(f->eth_addr) ||
  419. is_zero_ether_addr(f->eth_addr))) {
  420. int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
  421. if (rc < 0)
  422. return rc;
  423. notify |= rc;
  424. }
  425. } else {
  426. if (!(flags & NLM_F_CREATE))
  427. return -ENOENT;
  428. if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
  429. return -ENOSPC;
  430. /* Disallow replace to add a multicast entry */
  431. if ((flags & NLM_F_REPLACE) &&
  432. (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
  433. return -EOPNOTSUPP;
  434. netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
  435. f = kmalloc(sizeof(*f), GFP_ATOMIC);
  436. if (!f)
  437. return -ENOMEM;
  438. notify = 1;
  439. f->state = state;
  440. f->flags = ndm_flags;
  441. f->updated = f->used = jiffies;
  442. INIT_LIST_HEAD(&f->remotes);
  443. memcpy(f->eth_addr, mac, ETH_ALEN);
  444. vxlan_fdb_append(f, ip, port, vni, ifindex);
  445. ++vxlan->addrcnt;
  446. hlist_add_head_rcu(&f->hlist,
  447. vxlan_fdb_head(vxlan, mac));
  448. }
  449. if (notify)
  450. vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
  451. return 0;
  452. }
  453. static void vxlan_fdb_free_rdst(struct rcu_head *head)
  454. {
  455. struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
  456. kfree(rd);
  457. }
  458. static void vxlan_fdb_free(struct rcu_head *head)
  459. {
  460. struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
  461. struct vxlan_rdst *rd, *nd;
  462. list_for_each_entry_safe(rd, nd, &f->remotes, list)
  463. kfree(rd);
  464. kfree(f);
  465. }
  466. static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
  467. {
  468. netdev_dbg(vxlan->dev,
  469. "delete %pM\n", f->eth_addr);
  470. --vxlan->addrcnt;
  471. vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
  472. hlist_del_rcu(&f->hlist);
  473. call_rcu(&f->rcu, vxlan_fdb_free);
  474. }
  475. static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
  476. __be32 *ip, __be16 *port, u32 *vni, u32 *ifindex)
  477. {
  478. struct net *net = dev_net(vxlan->dev);
  479. if (tb[NDA_DST]) {
  480. if (nla_len(tb[NDA_DST]) != sizeof(__be32))
  481. return -EAFNOSUPPORT;
  482. *ip = nla_get_be32(tb[NDA_DST]);
  483. } else {
  484. *ip = htonl(INADDR_ANY);
  485. }
  486. if (tb[NDA_PORT]) {
  487. if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
  488. return -EINVAL;
  489. *port = nla_get_be16(tb[NDA_PORT]);
  490. } else {
  491. *port = vxlan->dst_port;
  492. }
  493. if (tb[NDA_VNI]) {
  494. if (nla_len(tb[NDA_VNI]) != sizeof(u32))
  495. return -EINVAL;
  496. *vni = nla_get_u32(tb[NDA_VNI]);
  497. } else {
  498. *vni = vxlan->default_dst.remote_vni;
  499. }
  500. if (tb[NDA_IFINDEX]) {
  501. struct net_device *tdev;
  502. if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
  503. return -EINVAL;
  504. *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
  505. tdev = dev_get_by_index(net, *ifindex);
  506. if (!tdev)
  507. return -EADDRNOTAVAIL;
  508. dev_put(tdev);
  509. } else {
  510. *ifindex = 0;
  511. }
  512. return 0;
  513. }
  514. /* Add static entry (via netlink) */
  515. static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  516. struct net_device *dev,
  517. const unsigned char *addr, u16 flags)
  518. {
  519. struct vxlan_dev *vxlan = netdev_priv(dev);
  520. /* struct net *net = dev_net(vxlan->dev); */
  521. __be32 ip;
  522. __be16 port;
  523. u32 vni, ifindex;
  524. int err;
  525. if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
  526. pr_info("RTM_NEWNEIGH with invalid state %#x\n",
  527. ndm->ndm_state);
  528. return -EINVAL;
  529. }
  530. if (tb[NDA_DST] == NULL)
  531. return -EINVAL;
  532. err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
  533. if (err)
  534. return err;
  535. spin_lock_bh(&vxlan->hash_lock);
  536. err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
  537. port, vni, ifindex, ndm->ndm_flags);
  538. spin_unlock_bh(&vxlan->hash_lock);
  539. return err;
  540. }
  541. /* Delete entry (via netlink) */
  542. static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
  543. struct net_device *dev,
  544. const unsigned char *addr)
  545. {
  546. struct vxlan_dev *vxlan = netdev_priv(dev);
  547. struct vxlan_fdb *f;
  548. struct vxlan_rdst *rd = NULL;
  549. __be32 ip;
  550. __be16 port;
  551. u32 vni, ifindex;
  552. int err;
  553. err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
  554. if (err)
  555. return err;
  556. err = -ENOENT;
  557. spin_lock_bh(&vxlan->hash_lock);
  558. f = vxlan_find_mac(vxlan, addr);
  559. if (!f)
  560. goto out;
  561. if (ip != htonl(INADDR_ANY)) {
  562. rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
  563. if (!rd)
  564. goto out;
  565. }
  566. err = 0;
  567. /* remove a destination if it's not the only one on the list,
  568. * otherwise destroy the fdb entry
  569. */
  570. if (rd && !list_is_singular(&f->remotes)) {
  571. list_del_rcu(&rd->list);
  572. call_rcu(&rd->rcu, vxlan_fdb_free_rdst);
  573. goto out;
  574. }
  575. vxlan_fdb_destroy(vxlan, f);
  576. out:
  577. spin_unlock_bh(&vxlan->hash_lock);
  578. return err;
  579. }
  580. /* Dump forwarding table */
  581. static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  582. struct net_device *dev, int idx)
  583. {
  584. struct vxlan_dev *vxlan = netdev_priv(dev);
  585. unsigned int h;
  586. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  587. struct vxlan_fdb *f;
  588. int err;
  589. hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
  590. struct vxlan_rdst *rd;
  591. if (idx < cb->args[0])
  592. goto skip;
  593. list_for_each_entry_rcu(rd, &f->remotes, list) {
  594. err = vxlan_fdb_info(skb, vxlan, f,
  595. NETLINK_CB(cb->skb).portid,
  596. cb->nlh->nlmsg_seq,
  597. RTM_NEWNEIGH,
  598. NLM_F_MULTI, rd);
  599. if (err < 0)
  600. goto out;
  601. }
  602. skip:
  603. ++idx;
  604. }
  605. }
  606. out:
  607. return idx;
  608. }
  609. /* Watch incoming packets to learn mapping between Ethernet address
  610. * and Tunnel endpoint.
  611. * Return true if packet is bogus and should be droppped.
  612. */
  613. static bool vxlan_snoop(struct net_device *dev,
  614. __be32 src_ip, const u8 *src_mac)
  615. {
  616. struct vxlan_dev *vxlan = netdev_priv(dev);
  617. struct vxlan_fdb *f;
  618. f = vxlan_find_mac(vxlan, src_mac);
  619. if (likely(f)) {
  620. struct vxlan_rdst *rdst = first_remote_rcu(f);
  621. if (likely(rdst->remote_ip == src_ip))
  622. return false;
  623. /* Don't migrate static entries, drop packets */
  624. if (f->state & NUD_NOARP)
  625. return true;
  626. if (net_ratelimit())
  627. netdev_info(dev,
  628. "%pM migrated from %pI4 to %pI4\n",
  629. src_mac, &rdst->remote_ip, &src_ip);
  630. rdst->remote_ip = src_ip;
  631. f->updated = jiffies;
  632. vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
  633. } else {
  634. /* learned new entry */
  635. spin_lock(&vxlan->hash_lock);
  636. /* close off race between vxlan_flush and incoming packets */
  637. if (netif_running(dev))
  638. vxlan_fdb_create(vxlan, src_mac, src_ip,
  639. NUD_REACHABLE,
  640. NLM_F_EXCL|NLM_F_CREATE,
  641. vxlan->dst_port,
  642. vxlan->default_dst.remote_vni,
  643. 0, NTF_SELF);
  644. spin_unlock(&vxlan->hash_lock);
  645. }
  646. return false;
  647. }
  648. /* See if multicast group is already in use by other ID */
  649. static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
  650. {
  651. struct vxlan_dev *vxlan;
  652. list_for_each_entry(vxlan, &vn->vxlan_list, next) {
  653. if (!netif_running(vxlan->dev))
  654. continue;
  655. if (vxlan->default_dst.remote_ip == remote_ip)
  656. return true;
  657. }
  658. return false;
  659. }
  660. static void vxlan_sock_hold(struct vxlan_sock *vs)
  661. {
  662. atomic_inc(&vs->refcnt);
  663. }
  664. void vxlan_sock_release(struct vxlan_sock *vs)
  665. {
  666. struct vxlan_net *vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
  667. if (!atomic_dec_and_test(&vs->refcnt))
  668. return;
  669. spin_lock(&vn->sock_lock);
  670. hlist_del_rcu(&vs->hlist);
  671. spin_unlock(&vn->sock_lock);
  672. queue_work(vxlan_wq, &vs->del_work);
  673. }
  674. EXPORT_SYMBOL_GPL(vxlan_sock_release);
  675. /* Callback to update multicast group membership when first VNI on
  676. * multicast asddress is brought up
  677. * Done as workqueue because ip_mc_join_group acquires RTNL.
  678. */
  679. static void vxlan_igmp_join(struct work_struct *work)
  680. {
  681. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
  682. struct vxlan_sock *vs = vxlan->vn_sock;
  683. struct sock *sk = vs->sock->sk;
  684. struct ip_mreqn mreq = {
  685. .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
  686. .imr_ifindex = vxlan->default_dst.remote_ifindex,
  687. };
  688. lock_sock(sk);
  689. ip_mc_join_group(sk, &mreq);
  690. release_sock(sk);
  691. vxlan_sock_release(vs);
  692. dev_put(vxlan->dev);
  693. }
  694. /* Inverse of vxlan_igmp_join when last VNI is brought down */
  695. static void vxlan_igmp_leave(struct work_struct *work)
  696. {
  697. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
  698. struct vxlan_sock *vs = vxlan->vn_sock;
  699. struct sock *sk = vs->sock->sk;
  700. struct ip_mreqn mreq = {
  701. .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
  702. .imr_ifindex = vxlan->default_dst.remote_ifindex,
  703. };
  704. lock_sock(sk);
  705. ip_mc_leave_group(sk, &mreq);
  706. release_sock(sk);
  707. vxlan_sock_release(vs);
  708. dev_put(vxlan->dev);
  709. }
  710. /* Callback from net/ipv4/udp.c to receive packets */
  711. static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
  712. {
  713. struct vxlan_sock *vs;
  714. struct vxlanhdr *vxh;
  715. __be16 port;
  716. /* Need Vxlan and inner Ethernet header to be present */
  717. if (!pskb_may_pull(skb, VXLAN_HLEN))
  718. goto error;
  719. /* Return packets with reserved bits set */
  720. vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
  721. if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
  722. (vxh->vx_vni & htonl(0xff))) {
  723. netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
  724. ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
  725. goto error;
  726. }
  727. if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
  728. goto drop;
  729. port = inet_sk(sk)->inet_sport;
  730. vs = vxlan_find_sock(sock_net(sk), port);
  731. if (!vs)
  732. goto drop;
  733. vs->rcv(vs, skb, vxh->vx_vni);
  734. return 0;
  735. drop:
  736. /* Consume bad packet */
  737. kfree_skb(skb);
  738. return 0;
  739. error:
  740. /* Return non vxlan pkt */
  741. return 1;
  742. }
  743. static void vxlan_rcv(struct vxlan_sock *vs,
  744. struct sk_buff *skb, __be32 vx_vni)
  745. {
  746. struct iphdr *oip;
  747. struct vxlan_dev *vxlan;
  748. struct pcpu_tstats *stats;
  749. __u32 vni;
  750. int err;
  751. vni = ntohl(vx_vni) >> 8;
  752. /* Is this VNI defined? */
  753. vxlan = vxlan_vs_find_vni(vs, vni);
  754. if (!vxlan)
  755. goto drop;
  756. skb_reset_mac_header(skb);
  757. skb->protocol = eth_type_trans(skb, vxlan->dev);
  758. /* Ignore packet loops (and multicast echo) */
  759. if (compare_ether_addr(eth_hdr(skb)->h_source,
  760. vxlan->dev->dev_addr) == 0)
  761. goto drop;
  762. /* Re-examine inner Ethernet packet */
  763. oip = ip_hdr(skb);
  764. if ((vxlan->flags & VXLAN_F_LEARN) &&
  765. vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
  766. goto drop;
  767. skb_reset_network_header(skb);
  768. /* If the NIC driver gave us an encapsulated packet with
  769. * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
  770. * leave the CHECKSUM_UNNECESSARY, the device checksummed it
  771. * for us. Otherwise force the upper layers to verify it.
  772. */
  773. if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
  774. !(vxlan->dev->features & NETIF_F_RXCSUM))
  775. skb->ip_summed = CHECKSUM_NONE;
  776. skb->encapsulation = 0;
  777. err = IP_ECN_decapsulate(oip, skb);
  778. if (unlikely(err)) {
  779. if (log_ecn_error)
  780. net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
  781. &oip->saddr, oip->tos);
  782. if (err > 1) {
  783. ++vxlan->dev->stats.rx_frame_errors;
  784. ++vxlan->dev->stats.rx_errors;
  785. goto drop;
  786. }
  787. }
  788. stats = this_cpu_ptr(vxlan->dev->tstats);
  789. u64_stats_update_begin(&stats->syncp);
  790. stats->rx_packets++;
  791. stats->rx_bytes += skb->len;
  792. u64_stats_update_end(&stats->syncp);
  793. netif_rx(skb);
  794. return;
  795. drop:
  796. /* Consume bad packet */
  797. kfree_skb(skb);
  798. }
  799. static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
  800. {
  801. struct vxlan_dev *vxlan = netdev_priv(dev);
  802. struct arphdr *parp;
  803. u8 *arpptr, *sha;
  804. __be32 sip, tip;
  805. struct neighbour *n;
  806. if (dev->flags & IFF_NOARP)
  807. goto out;
  808. if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
  809. dev->stats.tx_dropped++;
  810. goto out;
  811. }
  812. parp = arp_hdr(skb);
  813. if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
  814. parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
  815. parp->ar_pro != htons(ETH_P_IP) ||
  816. parp->ar_op != htons(ARPOP_REQUEST) ||
  817. parp->ar_hln != dev->addr_len ||
  818. parp->ar_pln != 4)
  819. goto out;
  820. arpptr = (u8 *)parp + sizeof(struct arphdr);
  821. sha = arpptr;
  822. arpptr += dev->addr_len; /* sha */
  823. memcpy(&sip, arpptr, sizeof(sip));
  824. arpptr += sizeof(sip);
  825. arpptr += dev->addr_len; /* tha */
  826. memcpy(&tip, arpptr, sizeof(tip));
  827. if (ipv4_is_loopback(tip) ||
  828. ipv4_is_multicast(tip))
  829. goto out;
  830. n = neigh_lookup(&arp_tbl, &tip, dev);
  831. if (n) {
  832. struct vxlan_fdb *f;
  833. struct sk_buff *reply;
  834. if (!(n->nud_state & NUD_CONNECTED)) {
  835. neigh_release(n);
  836. goto out;
  837. }
  838. f = vxlan_find_mac(vxlan, n->ha);
  839. if (f && first_remote_rcu(f)->remote_ip == htonl(INADDR_ANY)) {
  840. /* bridge-local neighbor */
  841. neigh_release(n);
  842. goto out;
  843. }
  844. reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
  845. n->ha, sha);
  846. neigh_release(n);
  847. skb_reset_mac_header(reply);
  848. __skb_pull(reply, skb_network_offset(reply));
  849. reply->ip_summed = CHECKSUM_UNNECESSARY;
  850. reply->pkt_type = PACKET_HOST;
  851. if (netif_rx_ni(reply) == NET_RX_DROP)
  852. dev->stats.rx_dropped++;
  853. } else if (vxlan->flags & VXLAN_F_L3MISS)
  854. vxlan_ip_miss(dev, tip);
  855. out:
  856. consume_skb(skb);
  857. return NETDEV_TX_OK;
  858. }
  859. static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
  860. {
  861. struct vxlan_dev *vxlan = netdev_priv(dev);
  862. struct neighbour *n;
  863. struct iphdr *pip;
  864. if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
  865. return false;
  866. n = NULL;
  867. switch (ntohs(eth_hdr(skb)->h_proto)) {
  868. case ETH_P_IP:
  869. if (!pskb_may_pull(skb, sizeof(struct iphdr)))
  870. return false;
  871. pip = ip_hdr(skb);
  872. n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
  873. break;
  874. default:
  875. return false;
  876. }
  877. if (n) {
  878. bool diff;
  879. diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
  880. if (diff) {
  881. memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
  882. dev->addr_len);
  883. memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
  884. }
  885. neigh_release(n);
  886. return diff;
  887. } else if (vxlan->flags & VXLAN_F_L3MISS)
  888. vxlan_ip_miss(dev, pip->daddr);
  889. return false;
  890. }
  891. static void vxlan_sock_put(struct sk_buff *skb)
  892. {
  893. sock_put(skb->sk);
  894. }
  895. /* On transmit, associate with the tunnel socket */
  896. static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
  897. {
  898. struct vxlan_dev *vxlan = netdev_priv(dev);
  899. struct sock *sk = vxlan->vn_sock->sock->sk;
  900. skb_orphan(skb);
  901. sock_hold(sk);
  902. skb->sk = sk;
  903. skb->destructor = vxlan_sock_put;
  904. }
  905. /* Compute source port for outgoing packet
  906. * first choice to use L4 flow hash since it will spread
  907. * better and maybe available from hardware
  908. * secondary choice is to use jhash on the Ethernet header
  909. */
  910. static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
  911. {
  912. unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
  913. u32 hash;
  914. hash = skb_get_rxhash(skb);
  915. if (!hash)
  916. hash = jhash(skb->data, 2 * ETH_ALEN,
  917. (__force u32) skb->protocol);
  918. return htons((((u64) hash * range) >> 32) + vxlan->port_min);
  919. }
  920. static int handle_offloads(struct sk_buff *skb)
  921. {
  922. if (skb_is_gso(skb)) {
  923. int err = skb_unclone(skb, GFP_ATOMIC);
  924. if (unlikely(err))
  925. return err;
  926. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  927. } else if (skb->ip_summed != CHECKSUM_PARTIAL)
  928. skb->ip_summed = CHECKSUM_NONE;
  929. return 0;
  930. }
  931. /* Bypass encapsulation if the destination is local */
  932. static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
  933. struct vxlan_dev *dst_vxlan)
  934. {
  935. struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
  936. struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
  937. skb->pkt_type = PACKET_HOST;
  938. skb->encapsulation = 0;
  939. skb->dev = dst_vxlan->dev;
  940. __skb_pull(skb, skb_network_offset(skb));
  941. if (dst_vxlan->flags & VXLAN_F_LEARN)
  942. vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
  943. eth_hdr(skb)->h_source);
  944. u64_stats_update_begin(&tx_stats->syncp);
  945. tx_stats->tx_packets++;
  946. tx_stats->tx_bytes += skb->len;
  947. u64_stats_update_end(&tx_stats->syncp);
  948. if (netif_rx(skb) == NET_RX_SUCCESS) {
  949. u64_stats_update_begin(&rx_stats->syncp);
  950. rx_stats->rx_packets++;
  951. rx_stats->rx_bytes += skb->len;
  952. u64_stats_update_end(&rx_stats->syncp);
  953. } else {
  954. skb->dev->stats.rx_dropped++;
  955. }
  956. }
  957. static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
  958. struct vxlan_rdst *rdst, bool did_rsc)
  959. {
  960. struct vxlan_dev *vxlan = netdev_priv(dev);
  961. struct rtable *rt;
  962. const struct iphdr *old_iph;
  963. struct vxlanhdr *vxh;
  964. struct udphdr *uh;
  965. struct flowi4 fl4;
  966. __be32 dst;
  967. __be16 src_port, dst_port;
  968. u32 vni;
  969. __be16 df = 0;
  970. __u8 tos, ttl;
  971. int err;
  972. dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
  973. vni = rdst->remote_vni;
  974. dst = rdst->remote_ip;
  975. if (!dst) {
  976. if (did_rsc) {
  977. /* short-circuited back to local bridge */
  978. vxlan_encap_bypass(skb, vxlan, vxlan);
  979. return;
  980. }
  981. goto drop;
  982. }
  983. if (!skb->encapsulation) {
  984. skb_reset_inner_headers(skb);
  985. skb->encapsulation = 1;
  986. }
  987. /* Need space for new headers (invalidates iph ptr) */
  988. if (skb_cow_head(skb, VXLAN_HEADROOM))
  989. goto drop;
  990. old_iph = ip_hdr(skb);
  991. ttl = vxlan->ttl;
  992. if (!ttl && IN_MULTICAST(ntohl(dst)))
  993. ttl = 1;
  994. tos = vxlan->tos;
  995. if (tos == 1)
  996. tos = ip_tunnel_get_dsfield(old_iph, skb);
  997. src_port = vxlan_src_port(vxlan, skb);
  998. memset(&fl4, 0, sizeof(fl4));
  999. fl4.flowi4_oif = rdst->remote_ifindex;
  1000. fl4.flowi4_tos = RT_TOS(tos);
  1001. fl4.daddr = dst;
  1002. fl4.saddr = vxlan->saddr;
  1003. rt = ip_route_output_key(dev_net(dev), &fl4);
  1004. if (IS_ERR(rt)) {
  1005. netdev_dbg(dev, "no route to %pI4\n", &dst);
  1006. dev->stats.tx_carrier_errors++;
  1007. goto tx_error;
  1008. }
  1009. if (rt->dst.dev == dev) {
  1010. netdev_dbg(dev, "circular route to %pI4\n", &dst);
  1011. ip_rt_put(rt);
  1012. dev->stats.collisions++;
  1013. goto tx_error;
  1014. }
  1015. /* Bypass encapsulation if the destination is local */
  1016. if (rt->rt_flags & RTCF_LOCAL &&
  1017. !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
  1018. struct vxlan_dev *dst_vxlan;
  1019. ip_rt_put(rt);
  1020. dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
  1021. if (!dst_vxlan)
  1022. goto tx_error;
  1023. vxlan_encap_bypass(skb, vxlan, dst_vxlan);
  1024. return;
  1025. }
  1026. vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
  1027. vxh->vx_flags = htonl(VXLAN_FLAGS);
  1028. vxh->vx_vni = htonl(vni << 8);
  1029. __skb_push(skb, sizeof(*uh));
  1030. skb_reset_transport_header(skb);
  1031. uh = udp_hdr(skb);
  1032. uh->dest = dst_port;
  1033. uh->source = src_port;
  1034. uh->len = htons(skb->len);
  1035. uh->check = 0;
  1036. vxlan_set_owner(dev, skb);
  1037. if (handle_offloads(skb))
  1038. goto drop;
  1039. tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
  1040. ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
  1041. err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
  1042. IPPROTO_UDP, tos, ttl, df);
  1043. iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
  1044. return;
  1045. drop:
  1046. dev->stats.tx_dropped++;
  1047. goto tx_free;
  1048. tx_error:
  1049. dev->stats.tx_errors++;
  1050. tx_free:
  1051. dev_kfree_skb(skb);
  1052. }
  1053. /* Transmit local packets over Vxlan
  1054. *
  1055. * Outer IP header inherits ECN and DF from inner header.
  1056. * Outer UDP destination is the VXLAN assigned port.
  1057. * source port is based on hash of flow
  1058. */
  1059. static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
  1060. {
  1061. struct vxlan_dev *vxlan = netdev_priv(dev);
  1062. struct ethhdr *eth;
  1063. bool did_rsc = false;
  1064. struct vxlan_rdst *rdst;
  1065. struct vxlan_fdb *f;
  1066. skb_reset_mac_header(skb);
  1067. eth = eth_hdr(skb);
  1068. if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
  1069. return arp_reduce(dev, skb);
  1070. f = vxlan_find_mac(vxlan, eth->h_dest);
  1071. did_rsc = false;
  1072. if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
  1073. ntohs(eth->h_proto) == ETH_P_IP) {
  1074. did_rsc = route_shortcircuit(dev, skb);
  1075. if (did_rsc)
  1076. f = vxlan_find_mac(vxlan, eth->h_dest);
  1077. }
  1078. if (f == NULL) {
  1079. f = vxlan_find_mac(vxlan, all_zeros_mac);
  1080. if (f == NULL) {
  1081. if ((vxlan->flags & VXLAN_F_L2MISS) &&
  1082. !is_multicast_ether_addr(eth->h_dest))
  1083. vxlan_fdb_miss(vxlan, eth->h_dest);
  1084. dev->stats.tx_dropped++;
  1085. dev_kfree_skb(skb);
  1086. return NETDEV_TX_OK;
  1087. }
  1088. }
  1089. list_for_each_entry_rcu(rdst, &f->remotes, list) {
  1090. struct sk_buff *skb1;
  1091. skb1 = skb_clone(skb, GFP_ATOMIC);
  1092. if (skb1)
  1093. vxlan_xmit_one(skb1, dev, rdst, did_rsc);
  1094. }
  1095. dev_kfree_skb(skb);
  1096. return NETDEV_TX_OK;
  1097. }
  1098. /* Walk the forwarding table and purge stale entries */
  1099. static void vxlan_cleanup(unsigned long arg)
  1100. {
  1101. struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
  1102. unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
  1103. unsigned int h;
  1104. if (!netif_running(vxlan->dev))
  1105. return;
  1106. spin_lock_bh(&vxlan->hash_lock);
  1107. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1108. struct hlist_node *p, *n;
  1109. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1110. struct vxlan_fdb *f
  1111. = container_of(p, struct vxlan_fdb, hlist);
  1112. unsigned long timeout;
  1113. if (f->state & NUD_PERMANENT)
  1114. continue;
  1115. timeout = f->used + vxlan->age_interval * HZ;
  1116. if (time_before_eq(timeout, jiffies)) {
  1117. netdev_dbg(vxlan->dev,
  1118. "garbage collect %pM\n",
  1119. f->eth_addr);
  1120. f->state = NUD_STALE;
  1121. vxlan_fdb_destroy(vxlan, f);
  1122. } else if (time_before(timeout, next_timer))
  1123. next_timer = timeout;
  1124. }
  1125. }
  1126. spin_unlock_bh(&vxlan->hash_lock);
  1127. mod_timer(&vxlan->age_timer, next_timer);
  1128. }
  1129. static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
  1130. {
  1131. __u32 vni = vxlan->default_dst.remote_vni;
  1132. vxlan->vn_sock = vs;
  1133. hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
  1134. }
  1135. /* Setup stats when device is created */
  1136. static int vxlan_init(struct net_device *dev)
  1137. {
  1138. struct vxlan_dev *vxlan = netdev_priv(dev);
  1139. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1140. struct vxlan_sock *vs;
  1141. dev->tstats = alloc_percpu(struct pcpu_tstats);
  1142. if (!dev->tstats)
  1143. return -ENOMEM;
  1144. spin_lock(&vn->sock_lock);
  1145. vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
  1146. if (vs) {
  1147. /* If we have a socket with same port already, reuse it */
  1148. atomic_inc(&vs->refcnt);
  1149. vxlan_vs_add_dev(vs, vxlan);
  1150. } else {
  1151. /* otherwise make new socket outside of RTNL */
  1152. dev_hold(dev);
  1153. queue_work(vxlan_wq, &vxlan->sock_work);
  1154. }
  1155. spin_unlock(&vn->sock_lock);
  1156. return 0;
  1157. }
  1158. static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
  1159. {
  1160. struct vxlan_fdb *f;
  1161. spin_lock_bh(&vxlan->hash_lock);
  1162. f = __vxlan_find_mac(vxlan, all_zeros_mac);
  1163. if (f)
  1164. vxlan_fdb_destroy(vxlan, f);
  1165. spin_unlock_bh(&vxlan->hash_lock);
  1166. }
  1167. static void vxlan_uninit(struct net_device *dev)
  1168. {
  1169. struct vxlan_dev *vxlan = netdev_priv(dev);
  1170. struct vxlan_sock *vs = vxlan->vn_sock;
  1171. vxlan_fdb_delete_default(vxlan);
  1172. if (vs)
  1173. vxlan_sock_release(vs);
  1174. free_percpu(dev->tstats);
  1175. }
  1176. /* Start ageing timer and join group when device is brought up */
  1177. static int vxlan_open(struct net_device *dev)
  1178. {
  1179. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1180. struct vxlan_dev *vxlan = netdev_priv(dev);
  1181. struct vxlan_sock *vs = vxlan->vn_sock;
  1182. /* socket hasn't been created */
  1183. if (!vs)
  1184. return -ENOTCONN;
  1185. if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
  1186. vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
  1187. vxlan_sock_hold(vs);
  1188. dev_hold(dev);
  1189. queue_work(vxlan_wq, &vxlan->igmp_join);
  1190. }
  1191. if (vxlan->age_interval)
  1192. mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
  1193. return 0;
  1194. }
  1195. /* Purge the forwarding table */
  1196. static void vxlan_flush(struct vxlan_dev *vxlan)
  1197. {
  1198. unsigned int h;
  1199. spin_lock_bh(&vxlan->hash_lock);
  1200. for (h = 0; h < FDB_HASH_SIZE; ++h) {
  1201. struct hlist_node *p, *n;
  1202. hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
  1203. struct vxlan_fdb *f
  1204. = container_of(p, struct vxlan_fdb, hlist);
  1205. /* the all_zeros_mac entry is deleted at vxlan_uninit */
  1206. if (!is_zero_ether_addr(f->eth_addr))
  1207. vxlan_fdb_destroy(vxlan, f);
  1208. }
  1209. }
  1210. spin_unlock_bh(&vxlan->hash_lock);
  1211. }
  1212. /* Cleanup timer and forwarding table on shutdown */
  1213. static int vxlan_stop(struct net_device *dev)
  1214. {
  1215. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1216. struct vxlan_dev *vxlan = netdev_priv(dev);
  1217. struct vxlan_sock *vs = vxlan->vn_sock;
  1218. if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip)) &&
  1219. ! vxlan_group_used(vn, vxlan->default_dst.remote_ip)) {
  1220. vxlan_sock_hold(vs);
  1221. dev_hold(dev);
  1222. queue_work(vxlan_wq, &vxlan->igmp_leave);
  1223. }
  1224. del_timer_sync(&vxlan->age_timer);
  1225. vxlan_flush(vxlan);
  1226. return 0;
  1227. }
  1228. /* Stub, nothing needs to be done. */
  1229. static void vxlan_set_multicast_list(struct net_device *dev)
  1230. {
  1231. }
  1232. static const struct net_device_ops vxlan_netdev_ops = {
  1233. .ndo_init = vxlan_init,
  1234. .ndo_uninit = vxlan_uninit,
  1235. .ndo_open = vxlan_open,
  1236. .ndo_stop = vxlan_stop,
  1237. .ndo_start_xmit = vxlan_xmit,
  1238. .ndo_get_stats64 = ip_tunnel_get_stats64,
  1239. .ndo_set_rx_mode = vxlan_set_multicast_list,
  1240. .ndo_change_mtu = eth_change_mtu,
  1241. .ndo_validate_addr = eth_validate_addr,
  1242. .ndo_set_mac_address = eth_mac_addr,
  1243. .ndo_fdb_add = vxlan_fdb_add,
  1244. .ndo_fdb_del = vxlan_fdb_delete,
  1245. .ndo_fdb_dump = vxlan_fdb_dump,
  1246. };
  1247. /* Info for udev, that this is a virtual tunnel endpoint */
  1248. static struct device_type vxlan_type = {
  1249. .name = "vxlan",
  1250. };
  1251. /* Initialize the device structure. */
  1252. static void vxlan_setup(struct net_device *dev)
  1253. {
  1254. struct vxlan_dev *vxlan = netdev_priv(dev);
  1255. unsigned int h;
  1256. int low, high;
  1257. eth_hw_addr_random(dev);
  1258. ether_setup(dev);
  1259. dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
  1260. dev->netdev_ops = &vxlan_netdev_ops;
  1261. dev->destructor = free_netdev;
  1262. SET_NETDEV_DEVTYPE(dev, &vxlan_type);
  1263. dev->tx_queue_len = 0;
  1264. dev->features |= NETIF_F_LLTX;
  1265. dev->features |= NETIF_F_NETNS_LOCAL;
  1266. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
  1267. dev->features |= NETIF_F_RXCSUM;
  1268. dev->features |= NETIF_F_GSO_SOFTWARE;
  1269. dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
  1270. dev->hw_features |= NETIF_F_GSO_SOFTWARE;
  1271. dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
  1272. dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
  1273. INIT_LIST_HEAD(&vxlan->next);
  1274. spin_lock_init(&vxlan->hash_lock);
  1275. INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
  1276. INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
  1277. INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
  1278. init_timer_deferrable(&vxlan->age_timer);
  1279. vxlan->age_timer.function = vxlan_cleanup;
  1280. vxlan->age_timer.data = (unsigned long) vxlan;
  1281. inet_get_local_port_range(&low, &high);
  1282. vxlan->port_min = low;
  1283. vxlan->port_max = high;
  1284. vxlan->dst_port = htons(vxlan_port);
  1285. vxlan->dev = dev;
  1286. for (h = 0; h < FDB_HASH_SIZE; ++h)
  1287. INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
  1288. }
  1289. static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
  1290. [IFLA_VXLAN_ID] = { .type = NLA_U32 },
  1291. [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
  1292. [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
  1293. [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
  1294. [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
  1295. [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
  1296. [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
  1297. [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
  1298. [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
  1299. [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
  1300. [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
  1301. [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
  1302. [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
  1303. [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
  1304. [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
  1305. };
  1306. static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
  1307. {
  1308. if (tb[IFLA_ADDRESS]) {
  1309. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
  1310. pr_debug("invalid link address (not ethernet)\n");
  1311. return -EINVAL;
  1312. }
  1313. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
  1314. pr_debug("invalid all zero ethernet address\n");
  1315. return -EADDRNOTAVAIL;
  1316. }
  1317. }
  1318. if (!data)
  1319. return -EINVAL;
  1320. if (data[IFLA_VXLAN_ID]) {
  1321. __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
  1322. if (id >= VXLAN_VID_MASK)
  1323. return -ERANGE;
  1324. }
  1325. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1326. const struct ifla_vxlan_port_range *p
  1327. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1328. if (ntohs(p->high) < ntohs(p->low)) {
  1329. pr_debug("port range %u .. %u not valid\n",
  1330. ntohs(p->low), ntohs(p->high));
  1331. return -EINVAL;
  1332. }
  1333. }
  1334. return 0;
  1335. }
  1336. static void vxlan_get_drvinfo(struct net_device *netdev,
  1337. struct ethtool_drvinfo *drvinfo)
  1338. {
  1339. strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
  1340. strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
  1341. }
  1342. static const struct ethtool_ops vxlan_ethtool_ops = {
  1343. .get_drvinfo = vxlan_get_drvinfo,
  1344. .get_link = ethtool_op_get_link,
  1345. };
  1346. static void vxlan_del_work(struct work_struct *work)
  1347. {
  1348. struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
  1349. sk_release_kernel(vs->sock->sk);
  1350. kfree_rcu(vs, rcu);
  1351. }
  1352. static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
  1353. vxlan_rcv_t *rcv, void *data)
  1354. {
  1355. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1356. struct vxlan_sock *vs;
  1357. struct sock *sk;
  1358. struct sockaddr_in vxlan_addr = {
  1359. .sin_family = AF_INET,
  1360. .sin_addr.s_addr = htonl(INADDR_ANY),
  1361. .sin_port = port,
  1362. };
  1363. int rc;
  1364. unsigned int h;
  1365. vs = kmalloc(sizeof(*vs), GFP_KERNEL);
  1366. if (!vs) {
  1367. pr_debug("memory alocation failure\n");
  1368. return ERR_PTR(-ENOMEM);
  1369. }
  1370. for (h = 0; h < VNI_HASH_SIZE; ++h)
  1371. INIT_HLIST_HEAD(&vs->vni_list[h]);
  1372. INIT_WORK(&vs->del_work, vxlan_del_work);
  1373. /* Create UDP socket for encapsulation receive. */
  1374. rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
  1375. if (rc < 0) {
  1376. pr_debug("UDP socket create failed\n");
  1377. kfree(vs);
  1378. return ERR_PTR(rc);
  1379. }
  1380. /* Put in proper namespace */
  1381. sk = vs->sock->sk;
  1382. sk_change_net(sk, net);
  1383. rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
  1384. sizeof(vxlan_addr));
  1385. if (rc < 0) {
  1386. pr_debug("bind for UDP socket %pI4:%u (%d)\n",
  1387. &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
  1388. sk_release_kernel(sk);
  1389. kfree(vs);
  1390. return ERR_PTR(rc);
  1391. }
  1392. atomic_set(&vs->refcnt, 1);
  1393. vs->rcv = rcv;
  1394. vs->data = data;
  1395. /* Disable multicast loopback */
  1396. inet_sk(sk)->mc_loop = 0;
  1397. spin_lock(&vn->sock_lock);
  1398. hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
  1399. spin_unlock(&vn->sock_lock);
  1400. /* Mark socket as an encapsulation socket. */
  1401. udp_sk(sk)->encap_type = 1;
  1402. udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
  1403. udp_encap_enable();
  1404. return vs;
  1405. }
  1406. struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
  1407. vxlan_rcv_t *rcv, void *data,
  1408. bool no_share)
  1409. {
  1410. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1411. struct vxlan_sock *vs;
  1412. vs = vxlan_socket_create(net, port, rcv, data);
  1413. if (!IS_ERR(vs))
  1414. return vs;
  1415. if (no_share) /* Return error if sharing is not allowed. */
  1416. return vs;
  1417. spin_lock(&vn->sock_lock);
  1418. vs = vxlan_find_sock(net, port);
  1419. if (vs) {
  1420. if (vs->rcv == rcv)
  1421. atomic_inc(&vs->refcnt);
  1422. else
  1423. vs = ERR_PTR(-EBUSY);
  1424. }
  1425. spin_unlock(&vn->sock_lock);
  1426. if (!vs)
  1427. vs = ERR_PTR(-EINVAL);
  1428. return vs;
  1429. }
  1430. EXPORT_SYMBOL_GPL(vxlan_sock_add);
  1431. /* Scheduled at device creation to bind to a socket */
  1432. static void vxlan_sock_work(struct work_struct *work)
  1433. {
  1434. struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
  1435. struct net *net = dev_net(vxlan->dev);
  1436. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1437. __be16 port = vxlan->dst_port;
  1438. struct vxlan_sock *nvs;
  1439. nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false);
  1440. spin_lock(&vn->sock_lock);
  1441. if (!IS_ERR(nvs))
  1442. vxlan_vs_add_dev(nvs, vxlan);
  1443. spin_unlock(&vn->sock_lock);
  1444. dev_put(vxlan->dev);
  1445. }
  1446. static int vxlan_newlink(struct net *net, struct net_device *dev,
  1447. struct nlattr *tb[], struct nlattr *data[])
  1448. {
  1449. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1450. struct vxlan_dev *vxlan = netdev_priv(dev);
  1451. struct vxlan_rdst *dst = &vxlan->default_dst;
  1452. __u32 vni;
  1453. int err;
  1454. if (!data[IFLA_VXLAN_ID])
  1455. return -EINVAL;
  1456. vni = nla_get_u32(data[IFLA_VXLAN_ID]);
  1457. dst->remote_vni = vni;
  1458. if (data[IFLA_VXLAN_GROUP])
  1459. dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
  1460. if (data[IFLA_VXLAN_LOCAL])
  1461. vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
  1462. if (data[IFLA_VXLAN_LINK] &&
  1463. (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
  1464. struct net_device *lowerdev
  1465. = __dev_get_by_index(net, dst->remote_ifindex);
  1466. if (!lowerdev) {
  1467. pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
  1468. return -ENODEV;
  1469. }
  1470. if (!tb[IFLA_MTU])
  1471. dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
  1472. /* update header length based on lower device */
  1473. dev->hard_header_len = lowerdev->hard_header_len +
  1474. VXLAN_HEADROOM;
  1475. }
  1476. if (data[IFLA_VXLAN_TOS])
  1477. vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
  1478. if (data[IFLA_VXLAN_TTL])
  1479. vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
  1480. if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
  1481. vxlan->flags |= VXLAN_F_LEARN;
  1482. if (data[IFLA_VXLAN_AGEING])
  1483. vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
  1484. else
  1485. vxlan->age_interval = FDB_AGE_DEFAULT;
  1486. if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
  1487. vxlan->flags |= VXLAN_F_PROXY;
  1488. if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
  1489. vxlan->flags |= VXLAN_F_RSC;
  1490. if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
  1491. vxlan->flags |= VXLAN_F_L2MISS;
  1492. if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
  1493. vxlan->flags |= VXLAN_F_L3MISS;
  1494. if (data[IFLA_VXLAN_LIMIT])
  1495. vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
  1496. if (data[IFLA_VXLAN_PORT_RANGE]) {
  1497. const struct ifla_vxlan_port_range *p
  1498. = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
  1499. vxlan->port_min = ntohs(p->low);
  1500. vxlan->port_max = ntohs(p->high);
  1501. }
  1502. if (data[IFLA_VXLAN_PORT])
  1503. vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
  1504. if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
  1505. pr_info("duplicate VNI %u\n", vni);
  1506. return -EEXIST;
  1507. }
  1508. SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
  1509. /* create an fdb entry for default destination */
  1510. err = vxlan_fdb_create(vxlan, all_zeros_mac,
  1511. vxlan->default_dst.remote_ip,
  1512. NUD_REACHABLE|NUD_PERMANENT,
  1513. NLM_F_EXCL|NLM_F_CREATE,
  1514. vxlan->dst_port, vxlan->default_dst.remote_vni,
  1515. vxlan->default_dst.remote_ifindex, NTF_SELF);
  1516. if (err)
  1517. return err;
  1518. err = register_netdevice(dev);
  1519. if (err) {
  1520. vxlan_fdb_delete_default(vxlan);
  1521. return err;
  1522. }
  1523. list_add(&vxlan->next, &vn->vxlan_list);
  1524. return 0;
  1525. }
  1526. static void vxlan_dellink(struct net_device *dev, struct list_head *head)
  1527. {
  1528. struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
  1529. struct vxlan_dev *vxlan = netdev_priv(dev);
  1530. spin_lock(&vn->sock_lock);
  1531. if (!hlist_unhashed(&vxlan->hlist))
  1532. hlist_del_rcu(&vxlan->hlist);
  1533. spin_unlock(&vn->sock_lock);
  1534. list_del(&vxlan->next);
  1535. unregister_netdevice_queue(dev, head);
  1536. }
  1537. static size_t vxlan_get_size(const struct net_device *dev)
  1538. {
  1539. return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
  1540. nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
  1541. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
  1542. nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
  1543. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
  1544. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
  1545. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
  1546. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
  1547. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
  1548. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
  1549. nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
  1550. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
  1551. nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
  1552. nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
  1553. nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
  1554. 0;
  1555. }
  1556. static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
  1557. {
  1558. const struct vxlan_dev *vxlan = netdev_priv(dev);
  1559. const struct vxlan_rdst *dst = &vxlan->default_dst;
  1560. struct ifla_vxlan_port_range ports = {
  1561. .low = htons(vxlan->port_min),
  1562. .high = htons(vxlan->port_max),
  1563. };
  1564. if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
  1565. goto nla_put_failure;
  1566. if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
  1567. goto nla_put_failure;
  1568. if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
  1569. goto nla_put_failure;
  1570. if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
  1571. goto nla_put_failure;
  1572. if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
  1573. nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
  1574. nla_put_u8(skb, IFLA_VXLAN_LEARNING,
  1575. !!(vxlan->flags & VXLAN_F_LEARN)) ||
  1576. nla_put_u8(skb, IFLA_VXLAN_PROXY,
  1577. !!(vxlan->flags & VXLAN_F_PROXY)) ||
  1578. nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
  1579. nla_put_u8(skb, IFLA_VXLAN_L2MISS,
  1580. !!(vxlan->flags & VXLAN_F_L2MISS)) ||
  1581. nla_put_u8(skb, IFLA_VXLAN_L3MISS,
  1582. !!(vxlan->flags & VXLAN_F_L3MISS)) ||
  1583. nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
  1584. nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
  1585. nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
  1586. goto nla_put_failure;
  1587. if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
  1588. goto nla_put_failure;
  1589. return 0;
  1590. nla_put_failure:
  1591. return -EMSGSIZE;
  1592. }
  1593. static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
  1594. .kind = "vxlan",
  1595. .maxtype = IFLA_VXLAN_MAX,
  1596. .policy = vxlan_policy,
  1597. .priv_size = sizeof(struct vxlan_dev),
  1598. .setup = vxlan_setup,
  1599. .validate = vxlan_validate,
  1600. .newlink = vxlan_newlink,
  1601. .dellink = vxlan_dellink,
  1602. .get_size = vxlan_get_size,
  1603. .fill_info = vxlan_fill_info,
  1604. };
  1605. static __net_init int vxlan_init_net(struct net *net)
  1606. {
  1607. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1608. unsigned int h;
  1609. INIT_LIST_HEAD(&vn->vxlan_list);
  1610. spin_lock_init(&vn->sock_lock);
  1611. for (h = 0; h < PORT_HASH_SIZE; ++h)
  1612. INIT_HLIST_HEAD(&vn->sock_list[h]);
  1613. return 0;
  1614. }
  1615. static __net_exit void vxlan_exit_net(struct net *net)
  1616. {
  1617. struct vxlan_net *vn = net_generic(net, vxlan_net_id);
  1618. struct vxlan_dev *vxlan;
  1619. LIST_HEAD(list);
  1620. rtnl_lock();
  1621. list_for_each_entry(vxlan, &vn->vxlan_list, next)
  1622. unregister_netdevice_queue(vxlan->dev, &list);
  1623. unregister_netdevice_many(&list);
  1624. rtnl_unlock();
  1625. }
  1626. static struct pernet_operations vxlan_net_ops = {
  1627. .init = vxlan_init_net,
  1628. .exit = vxlan_exit_net,
  1629. .id = &vxlan_net_id,
  1630. .size = sizeof(struct vxlan_net),
  1631. };
  1632. static int __init vxlan_init_module(void)
  1633. {
  1634. int rc;
  1635. vxlan_wq = alloc_workqueue("vxlan", 0, 0);
  1636. if (!vxlan_wq)
  1637. return -ENOMEM;
  1638. get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
  1639. rc = register_pernet_device(&vxlan_net_ops);
  1640. if (rc)
  1641. goto out1;
  1642. rc = rtnl_link_register(&vxlan_link_ops);
  1643. if (rc)
  1644. goto out2;
  1645. return 0;
  1646. out2:
  1647. unregister_pernet_device(&vxlan_net_ops);
  1648. out1:
  1649. destroy_workqueue(vxlan_wq);
  1650. return rc;
  1651. }
  1652. late_initcall(vxlan_init_module);
  1653. static void __exit vxlan_cleanup_module(void)
  1654. {
  1655. rtnl_link_unregister(&vxlan_link_ops);
  1656. destroy_workqueue(vxlan_wq);
  1657. unregister_pernet_device(&vxlan_net_ops);
  1658. rcu_barrier();
  1659. }
  1660. module_exit(vxlan_cleanup_module);
  1661. MODULE_LICENSE("GPL");
  1662. MODULE_VERSION(VXLAN_VERSION);
  1663. MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
  1664. MODULE_ALIAS_RTNL_LINK("vxlan");