af_mpls.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269
  1. #include <linux/types.h>
  2. #include <linux/skbuff.h>
  3. #include <linux/socket.h>
  4. #include <linux/sysctl.h>
  5. #include <linux/net.h>
  6. #include <linux/module.h>
  7. #include <linux/if_arp.h>
  8. #include <linux/ipv6.h>
  9. #include <linux/mpls.h>
  10. #include <linux/netconf.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/percpu.h>
  13. #include <net/ip.h>
  14. #include <net/dst.h>
  15. #include <net/sock.h>
  16. #include <net/arp.h>
  17. #include <net/ip_fib.h>
  18. #include <net/netevent.h>
  19. #include <net/netns/generic.h>
  20. #if IS_ENABLED(CONFIG_IPV6)
  21. #include <net/ipv6.h>
  22. #endif
  23. #include <net/addrconf.h>
  24. #include <net/nexthop.h>
  25. #include "internal.h"
  26. /* max memory we will use for mpls_route */
  27. #define MAX_MPLS_ROUTE_MEM 4096
  28. /* Maximum number of labels to look ahead at when selecting a path of
  29. * a multipath route
  30. */
  31. #define MAX_MP_SELECT_LABELS 4
  32. #define MPLS_NEIGH_TABLE_UNSPEC (NEIGH_LINK_TABLE + 1)
  33. static int zero = 0;
  34. static int one = 1;
  35. static int label_limit = (1 << 20) - 1;
  36. static int ttl_max = 255;
  37. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  38. struct nlmsghdr *nlh, struct net *net, u32 portid,
  39. unsigned int nlm_flags);
  40. static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
  41. {
  42. struct mpls_route *rt = NULL;
  43. if (index < net->mpls.platform_labels) {
  44. struct mpls_route __rcu **platform_label =
  45. rcu_dereference(net->mpls.platform_label);
  46. rt = rcu_dereference(platform_label[index]);
  47. }
  48. return rt;
  49. }
  50. bool mpls_output_possible(const struct net_device *dev)
  51. {
  52. return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
  53. }
  54. EXPORT_SYMBOL_GPL(mpls_output_possible);
  55. static u8 *__mpls_nh_via(struct mpls_route *rt, struct mpls_nh *nh)
  56. {
  57. return (u8 *)nh + rt->rt_via_offset;
  58. }
  59. static const u8 *mpls_nh_via(const struct mpls_route *rt,
  60. const struct mpls_nh *nh)
  61. {
  62. return __mpls_nh_via((struct mpls_route *)rt, (struct mpls_nh *)nh);
  63. }
  64. static unsigned int mpls_nh_header_size(const struct mpls_nh *nh)
  65. {
  66. /* The size of the layer 2.5 labels to be added for this route */
  67. return nh->nh_labels * sizeof(struct mpls_shim_hdr);
  68. }
  69. unsigned int mpls_dev_mtu(const struct net_device *dev)
  70. {
  71. /* The amount of data the layer 2 frame can hold */
  72. return dev->mtu;
  73. }
  74. EXPORT_SYMBOL_GPL(mpls_dev_mtu);
  75. bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
  76. {
  77. if (skb->len <= mtu)
  78. return false;
  79. if (skb_is_gso(skb) && skb_gso_validate_mtu(skb, mtu))
  80. return false;
  81. return true;
  82. }
  83. EXPORT_SYMBOL_GPL(mpls_pkt_too_big);
  84. void mpls_stats_inc_outucastpkts(struct net_device *dev,
  85. const struct sk_buff *skb)
  86. {
  87. struct mpls_dev *mdev;
  88. if (skb->protocol == htons(ETH_P_MPLS_UC)) {
  89. mdev = mpls_dev_get(dev);
  90. if (mdev)
  91. MPLS_INC_STATS_LEN(mdev, skb->len,
  92. tx_packets,
  93. tx_bytes);
  94. } else if (skb->protocol == htons(ETH_P_IP)) {
  95. IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
  96. #if IS_ENABLED(CONFIG_IPV6)
  97. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  98. struct inet6_dev *in6dev = __in6_dev_get(dev);
  99. if (in6dev)
  100. IP6_UPD_PO_STATS(dev_net(dev), in6dev,
  101. IPSTATS_MIB_OUT, skb->len);
  102. #endif
  103. }
  104. }
  105. EXPORT_SYMBOL_GPL(mpls_stats_inc_outucastpkts);
  106. static u32 mpls_multipath_hash(struct mpls_route *rt, struct sk_buff *skb)
  107. {
  108. struct mpls_entry_decoded dec;
  109. unsigned int mpls_hdr_len = 0;
  110. struct mpls_shim_hdr *hdr;
  111. bool eli_seen = false;
  112. int label_index;
  113. u32 hash = 0;
  114. for (label_index = 0; label_index < MAX_MP_SELECT_LABELS;
  115. label_index++) {
  116. mpls_hdr_len += sizeof(*hdr);
  117. if (!pskb_may_pull(skb, mpls_hdr_len))
  118. break;
  119. /* Read and decode the current label */
  120. hdr = mpls_hdr(skb) + label_index;
  121. dec = mpls_entry_decode(hdr);
  122. /* RFC6790 - reserved labels MUST NOT be used as keys
  123. * for the load-balancing function
  124. */
  125. if (likely(dec.label >= MPLS_LABEL_FIRST_UNRESERVED)) {
  126. hash = jhash_1word(dec.label, hash);
  127. /* The entropy label follows the entropy label
  128. * indicator, so this means that the entropy
  129. * label was just added to the hash - no need to
  130. * go any deeper either in the label stack or in the
  131. * payload
  132. */
  133. if (eli_seen)
  134. break;
  135. } else if (dec.label == MPLS_LABEL_ENTROPY) {
  136. eli_seen = true;
  137. }
  138. if (!dec.bos)
  139. continue;
  140. /* found bottom label; does skb have room for a header? */
  141. if (pskb_may_pull(skb, mpls_hdr_len + sizeof(struct iphdr))) {
  142. const struct iphdr *v4hdr;
  143. v4hdr = (const struct iphdr *)(hdr + 1);
  144. if (v4hdr->version == 4) {
  145. hash = jhash_3words(ntohl(v4hdr->saddr),
  146. ntohl(v4hdr->daddr),
  147. v4hdr->protocol, hash);
  148. } else if (v4hdr->version == 6 &&
  149. pskb_may_pull(skb, mpls_hdr_len +
  150. sizeof(struct ipv6hdr))) {
  151. const struct ipv6hdr *v6hdr;
  152. v6hdr = (const struct ipv6hdr *)(hdr + 1);
  153. hash = __ipv6_addr_jhash(&v6hdr->saddr, hash);
  154. hash = __ipv6_addr_jhash(&v6hdr->daddr, hash);
  155. hash = jhash_1word(v6hdr->nexthdr, hash);
  156. }
  157. }
  158. break;
  159. }
  160. return hash;
  161. }
  162. static struct mpls_nh *mpls_get_nexthop(struct mpls_route *rt, u8 index)
  163. {
  164. return (struct mpls_nh *)((u8 *)rt->rt_nh + index * rt->rt_nh_size);
  165. }
  166. /* number of alive nexthops (rt->rt_nhn_alive) and the flags for
  167. * a next hop (nh->nh_flags) are modified by netdev event handlers.
  168. * Since those fields can change at any moment, use READ_ONCE to
  169. * access both.
  170. */
  171. static struct mpls_nh *mpls_select_multipath(struct mpls_route *rt,
  172. struct sk_buff *skb)
  173. {
  174. u32 hash = 0;
  175. int nh_index = 0;
  176. int n = 0;
  177. u8 alive;
  178. /* No need to look further into packet if there's only
  179. * one path
  180. */
  181. if (rt->rt_nhn == 1)
  182. return rt->rt_nh;
  183. alive = READ_ONCE(rt->rt_nhn_alive);
  184. if (alive == 0)
  185. return NULL;
  186. hash = mpls_multipath_hash(rt, skb);
  187. nh_index = hash % alive;
  188. if (alive == rt->rt_nhn)
  189. goto out;
  190. for_nexthops(rt) {
  191. unsigned int nh_flags = READ_ONCE(nh->nh_flags);
  192. if (nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  193. continue;
  194. if (n == nh_index)
  195. return nh;
  196. n++;
  197. } endfor_nexthops(rt);
  198. out:
  199. return mpls_get_nexthop(rt, nh_index);
  200. }
  201. static bool mpls_egress(struct net *net, struct mpls_route *rt,
  202. struct sk_buff *skb, struct mpls_entry_decoded dec)
  203. {
  204. enum mpls_payload_type payload_type;
  205. bool success = false;
  206. /* The IPv4 code below accesses through the IPv4 header
  207. * checksum, which is 12 bytes into the packet.
  208. * The IPv6 code below accesses through the IPv6 hop limit
  209. * which is 8 bytes into the packet.
  210. *
  211. * For all supported cases there should always be at least 12
  212. * bytes of packet data present. The IPv4 header is 20 bytes
  213. * without options and the IPv6 header is always 40 bytes
  214. * long.
  215. */
  216. if (!pskb_may_pull(skb, 12))
  217. return false;
  218. payload_type = rt->rt_payload_type;
  219. if (payload_type == MPT_UNSPEC)
  220. payload_type = ip_hdr(skb)->version;
  221. switch (payload_type) {
  222. case MPT_IPV4: {
  223. struct iphdr *hdr4 = ip_hdr(skb);
  224. u8 new_ttl;
  225. skb->protocol = htons(ETH_P_IP);
  226. /* If propagating TTL, take the decremented TTL from
  227. * the incoming MPLS header, otherwise decrement the
  228. * TTL, but only if not 0 to avoid underflow.
  229. */
  230. if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
  231. (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  232. net->mpls.ip_ttl_propagate))
  233. new_ttl = dec.ttl;
  234. else
  235. new_ttl = hdr4->ttl ? hdr4->ttl - 1 : 0;
  236. csum_replace2(&hdr4->check,
  237. htons(hdr4->ttl << 8),
  238. htons(new_ttl << 8));
  239. hdr4->ttl = new_ttl;
  240. success = true;
  241. break;
  242. }
  243. case MPT_IPV6: {
  244. struct ipv6hdr *hdr6 = ipv6_hdr(skb);
  245. skb->protocol = htons(ETH_P_IPV6);
  246. /* If propagating TTL, take the decremented TTL from
  247. * the incoming MPLS header, otherwise decrement the
  248. * hop limit, but only if not 0 to avoid underflow.
  249. */
  250. if (rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED ||
  251. (rt->rt_ttl_propagate == MPLS_TTL_PROP_DEFAULT &&
  252. net->mpls.ip_ttl_propagate))
  253. hdr6->hop_limit = dec.ttl;
  254. else if (hdr6->hop_limit)
  255. hdr6->hop_limit = hdr6->hop_limit - 1;
  256. success = true;
  257. break;
  258. }
  259. case MPT_UNSPEC:
  260. /* Should have decided which protocol it is by now */
  261. break;
  262. }
  263. return success;
  264. }
  265. static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
  266. struct packet_type *pt, struct net_device *orig_dev)
  267. {
  268. struct net *net = dev_net(dev);
  269. struct mpls_shim_hdr *hdr;
  270. struct mpls_route *rt;
  271. struct mpls_nh *nh;
  272. struct mpls_entry_decoded dec;
  273. struct net_device *out_dev;
  274. struct mpls_dev *out_mdev;
  275. struct mpls_dev *mdev;
  276. unsigned int hh_len;
  277. unsigned int new_header_size;
  278. unsigned int mtu;
  279. int err;
  280. /* Careful this entire function runs inside of an rcu critical section */
  281. mdev = mpls_dev_get(dev);
  282. if (!mdev)
  283. goto drop;
  284. MPLS_INC_STATS_LEN(mdev, skb->len, rx_packets,
  285. rx_bytes);
  286. if (!mdev->input_enabled) {
  287. MPLS_INC_STATS(mdev, rx_dropped);
  288. goto drop;
  289. }
  290. if (skb->pkt_type != PACKET_HOST)
  291. goto err;
  292. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
  293. goto err;
  294. if (!pskb_may_pull(skb, sizeof(*hdr)))
  295. goto err;
  296. /* Read and decode the label */
  297. hdr = mpls_hdr(skb);
  298. dec = mpls_entry_decode(hdr);
  299. rt = mpls_route_input_rcu(net, dec.label);
  300. if (!rt) {
  301. MPLS_INC_STATS(mdev, rx_noroute);
  302. goto drop;
  303. }
  304. nh = mpls_select_multipath(rt, skb);
  305. if (!nh)
  306. goto err;
  307. /* Pop the label */
  308. skb_pull(skb, sizeof(*hdr));
  309. skb_reset_network_header(skb);
  310. skb_orphan(skb);
  311. if (skb_warn_if_lro(skb))
  312. goto err;
  313. skb_forward_csum(skb);
  314. /* Verify ttl is valid */
  315. if (dec.ttl <= 1)
  316. goto err;
  317. dec.ttl -= 1;
  318. /* Find the output device */
  319. out_dev = rcu_dereference(nh->nh_dev);
  320. if (!mpls_output_possible(out_dev))
  321. goto tx_err;
  322. /* Verify the destination can hold the packet */
  323. new_header_size = mpls_nh_header_size(nh);
  324. mtu = mpls_dev_mtu(out_dev);
  325. if (mpls_pkt_too_big(skb, mtu - new_header_size))
  326. goto tx_err;
  327. hh_len = LL_RESERVED_SPACE(out_dev);
  328. if (!out_dev->header_ops)
  329. hh_len = 0;
  330. /* Ensure there is enough space for the headers in the skb */
  331. if (skb_cow(skb, hh_len + new_header_size))
  332. goto tx_err;
  333. skb->dev = out_dev;
  334. skb->protocol = htons(ETH_P_MPLS_UC);
  335. if (unlikely(!new_header_size && dec.bos)) {
  336. /* Penultimate hop popping */
  337. if (!mpls_egress(dev_net(out_dev), rt, skb, dec))
  338. goto err;
  339. } else {
  340. bool bos;
  341. int i;
  342. skb_push(skb, new_header_size);
  343. skb_reset_network_header(skb);
  344. /* Push the new labels */
  345. hdr = mpls_hdr(skb);
  346. bos = dec.bos;
  347. for (i = nh->nh_labels - 1; i >= 0; i--) {
  348. hdr[i] = mpls_entry_encode(nh->nh_label[i],
  349. dec.ttl, 0, bos);
  350. bos = false;
  351. }
  352. }
  353. mpls_stats_inc_outucastpkts(out_dev, skb);
  354. /* If via wasn't specified then send out using device address */
  355. if (nh->nh_via_table == MPLS_NEIGH_TABLE_UNSPEC)
  356. err = neigh_xmit(NEIGH_LINK_TABLE, out_dev,
  357. out_dev->dev_addr, skb);
  358. else
  359. err = neigh_xmit(nh->nh_via_table, out_dev,
  360. mpls_nh_via(rt, nh), skb);
  361. if (err)
  362. net_dbg_ratelimited("%s: packet transmission failed: %d\n",
  363. __func__, err);
  364. return 0;
  365. tx_err:
  366. out_mdev = out_dev ? mpls_dev_get(out_dev) : NULL;
  367. if (out_mdev)
  368. MPLS_INC_STATS(out_mdev, tx_errors);
  369. goto drop;
  370. err:
  371. MPLS_INC_STATS(mdev, rx_errors);
  372. drop:
  373. kfree_skb(skb);
  374. return NET_RX_DROP;
  375. }
  376. static struct packet_type mpls_packet_type __read_mostly = {
  377. .type = cpu_to_be16(ETH_P_MPLS_UC),
  378. .func = mpls_forward,
  379. };
  380. static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
  381. [RTA_DST] = { .type = NLA_U32 },
  382. [RTA_OIF] = { .type = NLA_U32 },
  383. [RTA_TTL_PROPAGATE] = { .type = NLA_U8 },
  384. };
  385. struct mpls_route_config {
  386. u32 rc_protocol;
  387. u32 rc_ifindex;
  388. u8 rc_via_table;
  389. u8 rc_via_alen;
  390. u8 rc_via[MAX_VIA_ALEN];
  391. u32 rc_label;
  392. u8 rc_ttl_propagate;
  393. u8 rc_output_labels;
  394. u32 rc_output_label[MAX_NEW_LABELS];
  395. u32 rc_nlflags;
  396. enum mpls_payload_type rc_payload_type;
  397. struct nl_info rc_nlinfo;
  398. struct rtnexthop *rc_mp;
  399. int rc_mp_len;
  400. };
  401. /* all nexthops within a route have the same size based on max
  402. * number of labels and max via length for a hop
  403. */
  404. static struct mpls_route *mpls_rt_alloc(u8 num_nh, u8 max_alen, u8 max_labels)
  405. {
  406. u8 nh_size = MPLS_NH_SIZE(max_labels, max_alen);
  407. struct mpls_route *rt;
  408. size_t size;
  409. size = sizeof(*rt) + num_nh * nh_size;
  410. if (size > MAX_MPLS_ROUTE_MEM)
  411. return ERR_PTR(-EINVAL);
  412. rt = kzalloc(size, GFP_KERNEL);
  413. if (!rt)
  414. return ERR_PTR(-ENOMEM);
  415. rt->rt_nhn = num_nh;
  416. rt->rt_nhn_alive = num_nh;
  417. rt->rt_nh_size = nh_size;
  418. rt->rt_via_offset = MPLS_NH_VIA_OFF(max_labels);
  419. return rt;
  420. }
  421. static void mpls_rt_free(struct mpls_route *rt)
  422. {
  423. if (rt)
  424. kfree_rcu(rt, rt_rcu);
  425. }
  426. static void mpls_notify_route(struct net *net, unsigned index,
  427. struct mpls_route *old, struct mpls_route *new,
  428. const struct nl_info *info)
  429. {
  430. struct nlmsghdr *nlh = info ? info->nlh : NULL;
  431. unsigned portid = info ? info->portid : 0;
  432. int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
  433. struct mpls_route *rt = new ? new : old;
  434. unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
  435. /* Ignore reserved labels for now */
  436. if (rt && (index >= MPLS_LABEL_FIRST_UNRESERVED))
  437. rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
  438. }
  439. static void mpls_route_update(struct net *net, unsigned index,
  440. struct mpls_route *new,
  441. const struct nl_info *info)
  442. {
  443. struct mpls_route __rcu **platform_label;
  444. struct mpls_route *rt;
  445. ASSERT_RTNL();
  446. platform_label = rtnl_dereference(net->mpls.platform_label);
  447. rt = rtnl_dereference(platform_label[index]);
  448. rcu_assign_pointer(platform_label[index], new);
  449. mpls_notify_route(net, index, rt, new, info);
  450. /* If we removed a route free it now */
  451. mpls_rt_free(rt);
  452. }
  453. static unsigned find_free_label(struct net *net)
  454. {
  455. struct mpls_route __rcu **platform_label;
  456. size_t platform_labels;
  457. unsigned index;
  458. platform_label = rtnl_dereference(net->mpls.platform_label);
  459. platform_labels = net->mpls.platform_labels;
  460. for (index = MPLS_LABEL_FIRST_UNRESERVED; index < platform_labels;
  461. index++) {
  462. if (!rtnl_dereference(platform_label[index]))
  463. return index;
  464. }
  465. return LABEL_NOT_SPECIFIED;
  466. }
  467. #if IS_ENABLED(CONFIG_INET)
  468. static struct net_device *inet_fib_lookup_dev(struct net *net,
  469. const void *addr)
  470. {
  471. struct net_device *dev;
  472. struct rtable *rt;
  473. struct in_addr daddr;
  474. memcpy(&daddr, addr, sizeof(struct in_addr));
  475. rt = ip_route_output(net, daddr.s_addr, 0, 0, 0);
  476. if (IS_ERR(rt))
  477. return ERR_CAST(rt);
  478. dev = rt->dst.dev;
  479. dev_hold(dev);
  480. ip_rt_put(rt);
  481. return dev;
  482. }
  483. #else
  484. static struct net_device *inet_fib_lookup_dev(struct net *net,
  485. const void *addr)
  486. {
  487. return ERR_PTR(-EAFNOSUPPORT);
  488. }
  489. #endif
  490. #if IS_ENABLED(CONFIG_IPV6)
  491. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  492. const void *addr)
  493. {
  494. struct net_device *dev;
  495. struct dst_entry *dst;
  496. struct flowi6 fl6;
  497. int err;
  498. if (!ipv6_stub)
  499. return ERR_PTR(-EAFNOSUPPORT);
  500. memset(&fl6, 0, sizeof(fl6));
  501. memcpy(&fl6.daddr, addr, sizeof(struct in6_addr));
  502. err = ipv6_stub->ipv6_dst_lookup(net, NULL, &dst, &fl6);
  503. if (err)
  504. return ERR_PTR(err);
  505. dev = dst->dev;
  506. dev_hold(dev);
  507. dst_release(dst);
  508. return dev;
  509. }
  510. #else
  511. static struct net_device *inet6_fib_lookup_dev(struct net *net,
  512. const void *addr)
  513. {
  514. return ERR_PTR(-EAFNOSUPPORT);
  515. }
  516. #endif
  517. static struct net_device *find_outdev(struct net *net,
  518. struct mpls_route *rt,
  519. struct mpls_nh *nh, int oif)
  520. {
  521. struct net_device *dev = NULL;
  522. if (!oif) {
  523. switch (nh->nh_via_table) {
  524. case NEIGH_ARP_TABLE:
  525. dev = inet_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  526. break;
  527. case NEIGH_ND_TABLE:
  528. dev = inet6_fib_lookup_dev(net, mpls_nh_via(rt, nh));
  529. break;
  530. case NEIGH_LINK_TABLE:
  531. break;
  532. }
  533. } else {
  534. dev = dev_get_by_index(net, oif);
  535. }
  536. if (!dev)
  537. return ERR_PTR(-ENODEV);
  538. if (IS_ERR(dev))
  539. return dev;
  540. /* The caller is holding rtnl anyways, so release the dev reference */
  541. dev_put(dev);
  542. return dev;
  543. }
  544. static int mpls_nh_assign_dev(struct net *net, struct mpls_route *rt,
  545. struct mpls_nh *nh, int oif)
  546. {
  547. struct net_device *dev = NULL;
  548. int err = -ENODEV;
  549. dev = find_outdev(net, rt, nh, oif);
  550. if (IS_ERR(dev)) {
  551. err = PTR_ERR(dev);
  552. dev = NULL;
  553. goto errout;
  554. }
  555. /* Ensure this is a supported device */
  556. err = -EINVAL;
  557. if (!mpls_dev_get(dev))
  558. goto errout;
  559. if ((nh->nh_via_table == NEIGH_LINK_TABLE) &&
  560. (dev->addr_len != nh->nh_via_alen))
  561. goto errout;
  562. RCU_INIT_POINTER(nh->nh_dev, dev);
  563. if (!(dev->flags & IFF_UP)) {
  564. nh->nh_flags |= RTNH_F_DEAD;
  565. } else {
  566. unsigned int flags;
  567. flags = dev_get_flags(dev);
  568. if (!(flags & (IFF_RUNNING | IFF_LOWER_UP)))
  569. nh->nh_flags |= RTNH_F_LINKDOWN;
  570. }
  571. return 0;
  572. errout:
  573. return err;
  574. }
  575. static int mpls_nh_build_from_cfg(struct mpls_route_config *cfg,
  576. struct mpls_route *rt)
  577. {
  578. struct net *net = cfg->rc_nlinfo.nl_net;
  579. struct mpls_nh *nh = rt->rt_nh;
  580. int err;
  581. int i;
  582. if (!nh)
  583. return -ENOMEM;
  584. err = -EINVAL;
  585. nh->nh_labels = cfg->rc_output_labels;
  586. for (i = 0; i < nh->nh_labels; i++)
  587. nh->nh_label[i] = cfg->rc_output_label[i];
  588. nh->nh_via_table = cfg->rc_via_table;
  589. memcpy(__mpls_nh_via(rt, nh), cfg->rc_via, cfg->rc_via_alen);
  590. nh->nh_via_alen = cfg->rc_via_alen;
  591. err = mpls_nh_assign_dev(net, rt, nh, cfg->rc_ifindex);
  592. if (err)
  593. goto errout;
  594. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  595. rt->rt_nhn_alive--;
  596. return 0;
  597. errout:
  598. return err;
  599. }
  600. static int mpls_nh_build(struct net *net, struct mpls_route *rt,
  601. struct mpls_nh *nh, int oif, struct nlattr *via,
  602. struct nlattr *newdst, u8 max_labels)
  603. {
  604. int err = -ENOMEM;
  605. if (!nh)
  606. goto errout;
  607. if (newdst) {
  608. err = nla_get_labels(newdst, max_labels,
  609. &nh->nh_labels, nh->nh_label);
  610. if (err)
  611. goto errout;
  612. }
  613. if (via) {
  614. err = nla_get_via(via, &nh->nh_via_alen, &nh->nh_via_table,
  615. __mpls_nh_via(rt, nh));
  616. if (err)
  617. goto errout;
  618. } else {
  619. nh->nh_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  620. }
  621. err = mpls_nh_assign_dev(net, rt, nh, oif);
  622. if (err)
  623. goto errout;
  624. return 0;
  625. errout:
  626. return err;
  627. }
  628. static u8 mpls_count_nexthops(struct rtnexthop *rtnh, int len,
  629. u8 cfg_via_alen, u8 *max_via_alen,
  630. u8 *max_labels)
  631. {
  632. int remaining = len;
  633. u8 nhs = 0;
  634. *max_via_alen = 0;
  635. *max_labels = 0;
  636. while (rtnh_ok(rtnh, remaining)) {
  637. struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
  638. int attrlen;
  639. u8 n_labels = 0;
  640. attrlen = rtnh_attrlen(rtnh);
  641. nla = nla_find(attrs, attrlen, RTA_VIA);
  642. if (nla && nla_len(nla) >=
  643. offsetof(struct rtvia, rtvia_addr)) {
  644. int via_alen = nla_len(nla) -
  645. offsetof(struct rtvia, rtvia_addr);
  646. if (via_alen <= MAX_VIA_ALEN)
  647. *max_via_alen = max_t(u16, *max_via_alen,
  648. via_alen);
  649. }
  650. nla = nla_find(attrs, attrlen, RTA_NEWDST);
  651. if (nla &&
  652. nla_get_labels(nla, MAX_NEW_LABELS, &n_labels, NULL) != 0)
  653. return 0;
  654. *max_labels = max_t(u8, *max_labels, n_labels);
  655. /* number of nexthops is tracked by a u8.
  656. * Check for overflow.
  657. */
  658. if (nhs == 255)
  659. return 0;
  660. nhs++;
  661. rtnh = rtnh_next(rtnh, &remaining);
  662. }
  663. /* leftover implies invalid nexthop configuration, discard it */
  664. return remaining > 0 ? 0 : nhs;
  665. }
  666. static int mpls_nh_build_multi(struct mpls_route_config *cfg,
  667. struct mpls_route *rt, u8 max_labels)
  668. {
  669. struct rtnexthop *rtnh = cfg->rc_mp;
  670. struct nlattr *nla_via, *nla_newdst;
  671. int remaining = cfg->rc_mp_len;
  672. int err = 0;
  673. u8 nhs = 0;
  674. change_nexthops(rt) {
  675. int attrlen;
  676. nla_via = NULL;
  677. nla_newdst = NULL;
  678. err = -EINVAL;
  679. if (!rtnh_ok(rtnh, remaining))
  680. goto errout;
  681. /* neither weighted multipath nor any flags
  682. * are supported
  683. */
  684. if (rtnh->rtnh_hops || rtnh->rtnh_flags)
  685. goto errout;
  686. attrlen = rtnh_attrlen(rtnh);
  687. if (attrlen > 0) {
  688. struct nlattr *attrs = rtnh_attrs(rtnh);
  689. nla_via = nla_find(attrs, attrlen, RTA_VIA);
  690. nla_newdst = nla_find(attrs, attrlen, RTA_NEWDST);
  691. }
  692. err = mpls_nh_build(cfg->rc_nlinfo.nl_net, rt, nh,
  693. rtnh->rtnh_ifindex, nla_via, nla_newdst,
  694. max_labels);
  695. if (err)
  696. goto errout;
  697. if (nh->nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN))
  698. rt->rt_nhn_alive--;
  699. rtnh = rtnh_next(rtnh, &remaining);
  700. nhs++;
  701. } endfor_nexthops(rt);
  702. rt->rt_nhn = nhs;
  703. return 0;
  704. errout:
  705. return err;
  706. }
  707. static int mpls_route_add(struct mpls_route_config *cfg)
  708. {
  709. struct mpls_route __rcu **platform_label;
  710. struct net *net = cfg->rc_nlinfo.nl_net;
  711. struct mpls_route *rt, *old;
  712. int err = -EINVAL;
  713. u8 max_via_alen;
  714. unsigned index;
  715. u8 max_labels;
  716. u8 nhs;
  717. index = cfg->rc_label;
  718. /* If a label was not specified during insert pick one */
  719. if ((index == LABEL_NOT_SPECIFIED) &&
  720. (cfg->rc_nlflags & NLM_F_CREATE)) {
  721. index = find_free_label(net);
  722. }
  723. /* Reserved labels may not be set */
  724. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  725. goto errout;
  726. /* The full 20 bit range may not be supported. */
  727. if (index >= net->mpls.platform_labels)
  728. goto errout;
  729. /* Append makes no sense with mpls */
  730. err = -EOPNOTSUPP;
  731. if (cfg->rc_nlflags & NLM_F_APPEND)
  732. goto errout;
  733. err = -EEXIST;
  734. platform_label = rtnl_dereference(net->mpls.platform_label);
  735. old = rtnl_dereference(platform_label[index]);
  736. if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
  737. goto errout;
  738. err = -EEXIST;
  739. if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
  740. goto errout;
  741. err = -ENOENT;
  742. if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
  743. goto errout;
  744. err = -EINVAL;
  745. if (cfg->rc_mp) {
  746. nhs = mpls_count_nexthops(cfg->rc_mp, cfg->rc_mp_len,
  747. cfg->rc_via_alen, &max_via_alen,
  748. &max_labels);
  749. } else {
  750. max_via_alen = cfg->rc_via_alen;
  751. max_labels = cfg->rc_output_labels;
  752. nhs = 1;
  753. }
  754. if (nhs == 0)
  755. goto errout;
  756. err = -ENOMEM;
  757. rt = mpls_rt_alloc(nhs, max_via_alen, max_labels);
  758. if (IS_ERR(rt)) {
  759. err = PTR_ERR(rt);
  760. goto errout;
  761. }
  762. rt->rt_protocol = cfg->rc_protocol;
  763. rt->rt_payload_type = cfg->rc_payload_type;
  764. rt->rt_ttl_propagate = cfg->rc_ttl_propagate;
  765. if (cfg->rc_mp)
  766. err = mpls_nh_build_multi(cfg, rt, max_labels);
  767. else
  768. err = mpls_nh_build_from_cfg(cfg, rt);
  769. if (err)
  770. goto freert;
  771. mpls_route_update(net, index, rt, &cfg->rc_nlinfo);
  772. return 0;
  773. freert:
  774. mpls_rt_free(rt);
  775. errout:
  776. return err;
  777. }
  778. static int mpls_route_del(struct mpls_route_config *cfg)
  779. {
  780. struct net *net = cfg->rc_nlinfo.nl_net;
  781. unsigned index;
  782. int err = -EINVAL;
  783. index = cfg->rc_label;
  784. /* Reserved labels may not be removed */
  785. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  786. goto errout;
  787. /* The full 20 bit range may not be supported */
  788. if (index >= net->mpls.platform_labels)
  789. goto errout;
  790. mpls_route_update(net, index, NULL, &cfg->rc_nlinfo);
  791. err = 0;
  792. errout:
  793. return err;
  794. }
  795. static void mpls_get_stats(struct mpls_dev *mdev,
  796. struct mpls_link_stats *stats)
  797. {
  798. struct mpls_pcpu_stats *p;
  799. int i;
  800. memset(stats, 0, sizeof(*stats));
  801. for_each_possible_cpu(i) {
  802. struct mpls_link_stats local;
  803. unsigned int start;
  804. p = per_cpu_ptr(mdev->stats, i);
  805. do {
  806. start = u64_stats_fetch_begin(&p->syncp);
  807. local = p->stats;
  808. } while (u64_stats_fetch_retry(&p->syncp, start));
  809. stats->rx_packets += local.rx_packets;
  810. stats->rx_bytes += local.rx_bytes;
  811. stats->tx_packets += local.tx_packets;
  812. stats->tx_bytes += local.tx_bytes;
  813. stats->rx_errors += local.rx_errors;
  814. stats->tx_errors += local.tx_errors;
  815. stats->rx_dropped += local.rx_dropped;
  816. stats->tx_dropped += local.tx_dropped;
  817. stats->rx_noroute += local.rx_noroute;
  818. }
  819. }
  820. static int mpls_fill_stats_af(struct sk_buff *skb,
  821. const struct net_device *dev)
  822. {
  823. struct mpls_link_stats *stats;
  824. struct mpls_dev *mdev;
  825. struct nlattr *nla;
  826. mdev = mpls_dev_get(dev);
  827. if (!mdev)
  828. return -ENODATA;
  829. nla = nla_reserve_64bit(skb, MPLS_STATS_LINK,
  830. sizeof(struct mpls_link_stats),
  831. MPLS_STATS_UNSPEC);
  832. if (!nla)
  833. return -EMSGSIZE;
  834. stats = nla_data(nla);
  835. mpls_get_stats(mdev, stats);
  836. return 0;
  837. }
  838. static size_t mpls_get_stats_af_size(const struct net_device *dev)
  839. {
  840. struct mpls_dev *mdev;
  841. mdev = mpls_dev_get(dev);
  842. if (!mdev)
  843. return 0;
  844. return nla_total_size_64bit(sizeof(struct mpls_link_stats));
  845. }
  846. static int mpls_netconf_fill_devconf(struct sk_buff *skb, struct mpls_dev *mdev,
  847. u32 portid, u32 seq, int event,
  848. unsigned int flags, int type)
  849. {
  850. struct nlmsghdr *nlh;
  851. struct netconfmsg *ncm;
  852. bool all = false;
  853. nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
  854. flags);
  855. if (!nlh)
  856. return -EMSGSIZE;
  857. if (type == NETCONFA_ALL)
  858. all = true;
  859. ncm = nlmsg_data(nlh);
  860. ncm->ncm_family = AF_MPLS;
  861. if (nla_put_s32(skb, NETCONFA_IFINDEX, mdev->dev->ifindex) < 0)
  862. goto nla_put_failure;
  863. if ((all || type == NETCONFA_INPUT) &&
  864. nla_put_s32(skb, NETCONFA_INPUT,
  865. mdev->input_enabled) < 0)
  866. goto nla_put_failure;
  867. nlmsg_end(skb, nlh);
  868. return 0;
  869. nla_put_failure:
  870. nlmsg_cancel(skb, nlh);
  871. return -EMSGSIZE;
  872. }
  873. static int mpls_netconf_msgsize_devconf(int type)
  874. {
  875. int size = NLMSG_ALIGN(sizeof(struct netconfmsg))
  876. + nla_total_size(4); /* NETCONFA_IFINDEX */
  877. bool all = false;
  878. if (type == NETCONFA_ALL)
  879. all = true;
  880. if (all || type == NETCONFA_INPUT)
  881. size += nla_total_size(4);
  882. return size;
  883. }
  884. static void mpls_netconf_notify_devconf(struct net *net, int event,
  885. int type, struct mpls_dev *mdev)
  886. {
  887. struct sk_buff *skb;
  888. int err = -ENOBUFS;
  889. skb = nlmsg_new(mpls_netconf_msgsize_devconf(type), GFP_KERNEL);
  890. if (!skb)
  891. goto errout;
  892. err = mpls_netconf_fill_devconf(skb, mdev, 0, 0, event, 0, type);
  893. if (err < 0) {
  894. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  895. WARN_ON(err == -EMSGSIZE);
  896. kfree_skb(skb);
  897. goto errout;
  898. }
  899. rtnl_notify(skb, net, 0, RTNLGRP_MPLS_NETCONF, NULL, GFP_KERNEL);
  900. return;
  901. errout:
  902. if (err < 0)
  903. rtnl_set_sk_err(net, RTNLGRP_MPLS_NETCONF, err);
  904. }
  905. static const struct nla_policy devconf_mpls_policy[NETCONFA_MAX + 1] = {
  906. [NETCONFA_IFINDEX] = { .len = sizeof(int) },
  907. };
  908. static int mpls_netconf_get_devconf(struct sk_buff *in_skb,
  909. struct nlmsghdr *nlh,
  910. struct netlink_ext_ack *extack)
  911. {
  912. struct net *net = sock_net(in_skb->sk);
  913. struct nlattr *tb[NETCONFA_MAX + 1];
  914. struct netconfmsg *ncm;
  915. struct net_device *dev;
  916. struct mpls_dev *mdev;
  917. struct sk_buff *skb;
  918. int ifindex;
  919. int err;
  920. err = nlmsg_parse(nlh, sizeof(*ncm), tb, NETCONFA_MAX,
  921. devconf_mpls_policy, NULL);
  922. if (err < 0)
  923. goto errout;
  924. err = -EINVAL;
  925. if (!tb[NETCONFA_IFINDEX])
  926. goto errout;
  927. ifindex = nla_get_s32(tb[NETCONFA_IFINDEX]);
  928. dev = __dev_get_by_index(net, ifindex);
  929. if (!dev)
  930. goto errout;
  931. mdev = mpls_dev_get(dev);
  932. if (!mdev)
  933. goto errout;
  934. err = -ENOBUFS;
  935. skb = nlmsg_new(mpls_netconf_msgsize_devconf(NETCONFA_ALL), GFP_KERNEL);
  936. if (!skb)
  937. goto errout;
  938. err = mpls_netconf_fill_devconf(skb, mdev,
  939. NETLINK_CB(in_skb).portid,
  940. nlh->nlmsg_seq, RTM_NEWNETCONF, 0,
  941. NETCONFA_ALL);
  942. if (err < 0) {
  943. /* -EMSGSIZE implies BUG in mpls_netconf_msgsize_devconf() */
  944. WARN_ON(err == -EMSGSIZE);
  945. kfree_skb(skb);
  946. goto errout;
  947. }
  948. err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
  949. errout:
  950. return err;
  951. }
  952. static int mpls_netconf_dump_devconf(struct sk_buff *skb,
  953. struct netlink_callback *cb)
  954. {
  955. struct net *net = sock_net(skb->sk);
  956. struct hlist_head *head;
  957. struct net_device *dev;
  958. struct mpls_dev *mdev;
  959. int idx, s_idx;
  960. int h, s_h;
  961. s_h = cb->args[0];
  962. s_idx = idx = cb->args[1];
  963. for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
  964. idx = 0;
  965. head = &net->dev_index_head[h];
  966. rcu_read_lock();
  967. cb->seq = net->dev_base_seq;
  968. hlist_for_each_entry_rcu(dev, head, index_hlist) {
  969. if (idx < s_idx)
  970. goto cont;
  971. mdev = mpls_dev_get(dev);
  972. if (!mdev)
  973. goto cont;
  974. if (mpls_netconf_fill_devconf(skb, mdev,
  975. NETLINK_CB(cb->skb).portid,
  976. cb->nlh->nlmsg_seq,
  977. RTM_NEWNETCONF,
  978. NLM_F_MULTI,
  979. NETCONFA_ALL) < 0) {
  980. rcu_read_unlock();
  981. goto done;
  982. }
  983. nl_dump_check_consistent(cb, nlmsg_hdr(skb));
  984. cont:
  985. idx++;
  986. }
  987. rcu_read_unlock();
  988. }
  989. done:
  990. cb->args[0] = h;
  991. cb->args[1] = idx;
  992. return skb->len;
  993. }
  994. #define MPLS_PERDEV_SYSCTL_OFFSET(field) \
  995. (&((struct mpls_dev *)0)->field)
  996. static int mpls_conf_proc(struct ctl_table *ctl, int write,
  997. void __user *buffer,
  998. size_t *lenp, loff_t *ppos)
  999. {
  1000. int oval = *(int *)ctl->data;
  1001. int ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
  1002. if (write) {
  1003. struct mpls_dev *mdev = ctl->extra1;
  1004. int i = (int *)ctl->data - (int *)mdev;
  1005. struct net *net = ctl->extra2;
  1006. int val = *(int *)ctl->data;
  1007. if (i == offsetof(struct mpls_dev, input_enabled) &&
  1008. val != oval) {
  1009. mpls_netconf_notify_devconf(net, RTM_NEWNETCONF,
  1010. NETCONFA_INPUT, mdev);
  1011. }
  1012. }
  1013. return ret;
  1014. }
  1015. static const struct ctl_table mpls_dev_table[] = {
  1016. {
  1017. .procname = "input",
  1018. .maxlen = sizeof(int),
  1019. .mode = 0644,
  1020. .proc_handler = mpls_conf_proc,
  1021. .data = MPLS_PERDEV_SYSCTL_OFFSET(input_enabled),
  1022. },
  1023. { }
  1024. };
  1025. static int mpls_dev_sysctl_register(struct net_device *dev,
  1026. struct mpls_dev *mdev)
  1027. {
  1028. char path[sizeof("net/mpls/conf/") + IFNAMSIZ];
  1029. struct net *net = dev_net(dev);
  1030. struct ctl_table *table;
  1031. int i;
  1032. table = kmemdup(&mpls_dev_table, sizeof(mpls_dev_table), GFP_KERNEL);
  1033. if (!table)
  1034. goto out;
  1035. /* Table data contains only offsets relative to the base of
  1036. * the mdev at this point, so make them absolute.
  1037. */
  1038. for (i = 0; i < ARRAY_SIZE(mpls_dev_table); i++) {
  1039. table[i].data = (char *)mdev + (uintptr_t)table[i].data;
  1040. table[i].extra1 = mdev;
  1041. table[i].extra2 = net;
  1042. }
  1043. snprintf(path, sizeof(path), "net/mpls/conf/%s", dev->name);
  1044. mdev->sysctl = register_net_sysctl(net, path, table);
  1045. if (!mdev->sysctl)
  1046. goto free;
  1047. mpls_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_ALL, mdev);
  1048. return 0;
  1049. free:
  1050. kfree(table);
  1051. out:
  1052. return -ENOBUFS;
  1053. }
  1054. static void mpls_dev_sysctl_unregister(struct net_device *dev,
  1055. struct mpls_dev *mdev)
  1056. {
  1057. struct net *net = dev_net(dev);
  1058. struct ctl_table *table;
  1059. table = mdev->sysctl->ctl_table_arg;
  1060. unregister_net_sysctl_table(mdev->sysctl);
  1061. kfree(table);
  1062. mpls_netconf_notify_devconf(net, RTM_DELNETCONF, 0, mdev);
  1063. }
  1064. static struct mpls_dev *mpls_add_dev(struct net_device *dev)
  1065. {
  1066. struct mpls_dev *mdev;
  1067. int err = -ENOMEM;
  1068. int i;
  1069. ASSERT_RTNL();
  1070. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  1071. if (!mdev)
  1072. return ERR_PTR(err);
  1073. mdev->stats = alloc_percpu(struct mpls_pcpu_stats);
  1074. if (!mdev->stats)
  1075. goto free;
  1076. for_each_possible_cpu(i) {
  1077. struct mpls_pcpu_stats *mpls_stats;
  1078. mpls_stats = per_cpu_ptr(mdev->stats, i);
  1079. u64_stats_init(&mpls_stats->syncp);
  1080. }
  1081. mdev->dev = dev;
  1082. err = mpls_dev_sysctl_register(dev, mdev);
  1083. if (err)
  1084. goto free;
  1085. rcu_assign_pointer(dev->mpls_ptr, mdev);
  1086. return mdev;
  1087. free:
  1088. free_percpu(mdev->stats);
  1089. kfree(mdev);
  1090. return ERR_PTR(err);
  1091. }
  1092. static void mpls_dev_destroy_rcu(struct rcu_head *head)
  1093. {
  1094. struct mpls_dev *mdev = container_of(head, struct mpls_dev, rcu);
  1095. free_percpu(mdev->stats);
  1096. kfree(mdev);
  1097. }
  1098. static void mpls_ifdown(struct net_device *dev, int event)
  1099. {
  1100. struct mpls_route __rcu **platform_label;
  1101. struct net *net = dev_net(dev);
  1102. u8 alive, deleted;
  1103. unsigned index;
  1104. platform_label = rtnl_dereference(net->mpls.platform_label);
  1105. for (index = 0; index < net->mpls.platform_labels; index++) {
  1106. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1107. if (!rt)
  1108. continue;
  1109. alive = 0;
  1110. deleted = 0;
  1111. change_nexthops(rt) {
  1112. unsigned int nh_flags = nh->nh_flags;
  1113. if (rtnl_dereference(nh->nh_dev) != dev)
  1114. goto next;
  1115. switch (event) {
  1116. case NETDEV_DOWN:
  1117. case NETDEV_UNREGISTER:
  1118. nh_flags |= RTNH_F_DEAD;
  1119. /* fall through */
  1120. case NETDEV_CHANGE:
  1121. nh_flags |= RTNH_F_LINKDOWN;
  1122. break;
  1123. }
  1124. if (event == NETDEV_UNREGISTER)
  1125. RCU_INIT_POINTER(nh->nh_dev, NULL);
  1126. if (nh->nh_flags != nh_flags)
  1127. WRITE_ONCE(nh->nh_flags, nh_flags);
  1128. next:
  1129. if (!(nh_flags & (RTNH_F_DEAD | RTNH_F_LINKDOWN)))
  1130. alive++;
  1131. if (!rtnl_dereference(nh->nh_dev))
  1132. deleted++;
  1133. } endfor_nexthops(rt);
  1134. WRITE_ONCE(rt->rt_nhn_alive, alive);
  1135. /* if there are no more nexthops, delete the route */
  1136. if (event == NETDEV_UNREGISTER && deleted == rt->rt_nhn)
  1137. mpls_route_update(net, index, NULL, NULL);
  1138. }
  1139. }
  1140. static void mpls_ifup(struct net_device *dev, unsigned int flags)
  1141. {
  1142. struct mpls_route __rcu **platform_label;
  1143. struct net *net = dev_net(dev);
  1144. unsigned index;
  1145. u8 alive;
  1146. platform_label = rtnl_dereference(net->mpls.platform_label);
  1147. for (index = 0; index < net->mpls.platform_labels; index++) {
  1148. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1149. if (!rt)
  1150. continue;
  1151. alive = 0;
  1152. change_nexthops(rt) {
  1153. unsigned int nh_flags = nh->nh_flags;
  1154. struct net_device *nh_dev =
  1155. rtnl_dereference(nh->nh_dev);
  1156. if (!(nh_flags & flags)) {
  1157. alive++;
  1158. continue;
  1159. }
  1160. if (nh_dev != dev)
  1161. continue;
  1162. alive++;
  1163. nh_flags &= ~flags;
  1164. WRITE_ONCE(nh->nh_flags, flags);
  1165. } endfor_nexthops(rt);
  1166. WRITE_ONCE(rt->rt_nhn_alive, alive);
  1167. }
  1168. }
  1169. static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
  1170. void *ptr)
  1171. {
  1172. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1173. struct mpls_dev *mdev;
  1174. unsigned int flags;
  1175. if (event == NETDEV_REGISTER) {
  1176. /* For now just support Ethernet, IPGRE, SIT and IPIP devices */
  1177. if (dev->type == ARPHRD_ETHER ||
  1178. dev->type == ARPHRD_LOOPBACK ||
  1179. dev->type == ARPHRD_IPGRE ||
  1180. dev->type == ARPHRD_SIT ||
  1181. dev->type == ARPHRD_TUNNEL) {
  1182. mdev = mpls_add_dev(dev);
  1183. if (IS_ERR(mdev))
  1184. return notifier_from_errno(PTR_ERR(mdev));
  1185. }
  1186. return NOTIFY_OK;
  1187. }
  1188. mdev = mpls_dev_get(dev);
  1189. if (!mdev)
  1190. return NOTIFY_OK;
  1191. switch (event) {
  1192. case NETDEV_DOWN:
  1193. mpls_ifdown(dev, event);
  1194. break;
  1195. case NETDEV_UP:
  1196. flags = dev_get_flags(dev);
  1197. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1198. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1199. else
  1200. mpls_ifup(dev, RTNH_F_DEAD);
  1201. break;
  1202. case NETDEV_CHANGE:
  1203. flags = dev_get_flags(dev);
  1204. if (flags & (IFF_RUNNING | IFF_LOWER_UP))
  1205. mpls_ifup(dev, RTNH_F_DEAD | RTNH_F_LINKDOWN);
  1206. else
  1207. mpls_ifdown(dev, event);
  1208. break;
  1209. case NETDEV_UNREGISTER:
  1210. mpls_ifdown(dev, event);
  1211. mdev = mpls_dev_get(dev);
  1212. if (mdev) {
  1213. mpls_dev_sysctl_unregister(dev, mdev);
  1214. RCU_INIT_POINTER(dev->mpls_ptr, NULL);
  1215. call_rcu(&mdev->rcu, mpls_dev_destroy_rcu);
  1216. }
  1217. break;
  1218. case NETDEV_CHANGENAME:
  1219. mdev = mpls_dev_get(dev);
  1220. if (mdev) {
  1221. int err;
  1222. mpls_dev_sysctl_unregister(dev, mdev);
  1223. err = mpls_dev_sysctl_register(dev, mdev);
  1224. if (err)
  1225. return notifier_from_errno(err);
  1226. }
  1227. break;
  1228. }
  1229. return NOTIFY_OK;
  1230. }
  1231. static struct notifier_block mpls_dev_notifier = {
  1232. .notifier_call = mpls_dev_notify,
  1233. };
  1234. static int nla_put_via(struct sk_buff *skb,
  1235. u8 table, const void *addr, int alen)
  1236. {
  1237. static const int table_to_family[NEIGH_NR_TABLES + 1] = {
  1238. AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
  1239. };
  1240. struct nlattr *nla;
  1241. struct rtvia *via;
  1242. int family = AF_UNSPEC;
  1243. nla = nla_reserve(skb, RTA_VIA, alen + 2);
  1244. if (!nla)
  1245. return -EMSGSIZE;
  1246. if (table <= NEIGH_NR_TABLES)
  1247. family = table_to_family[table];
  1248. via = nla_data(nla);
  1249. via->rtvia_family = family;
  1250. memcpy(via->rtvia_addr, addr, alen);
  1251. return 0;
  1252. }
  1253. int nla_put_labels(struct sk_buff *skb, int attrtype,
  1254. u8 labels, const u32 label[])
  1255. {
  1256. struct nlattr *nla;
  1257. struct mpls_shim_hdr *nla_label;
  1258. bool bos;
  1259. int i;
  1260. nla = nla_reserve(skb, attrtype, labels*4);
  1261. if (!nla)
  1262. return -EMSGSIZE;
  1263. nla_label = nla_data(nla);
  1264. bos = true;
  1265. for (i = labels - 1; i >= 0; i--) {
  1266. nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
  1267. bos = false;
  1268. }
  1269. return 0;
  1270. }
  1271. EXPORT_SYMBOL_GPL(nla_put_labels);
  1272. int nla_get_labels(const struct nlattr *nla,
  1273. u8 max_labels, u8 *labels, u32 label[])
  1274. {
  1275. unsigned len = nla_len(nla);
  1276. struct mpls_shim_hdr *nla_label;
  1277. u8 nla_labels;
  1278. bool bos;
  1279. int i;
  1280. /* len needs to be an even multiple of 4 (the label size). Number
  1281. * of labels is a u8 so check for overflow.
  1282. */
  1283. if (len & 3 || len / 4 > 255)
  1284. return -EINVAL;
  1285. /* Limit the number of new labels allowed */
  1286. nla_labels = len/4;
  1287. if (nla_labels > max_labels)
  1288. return -EINVAL;
  1289. /* when label == NULL, caller wants number of labels */
  1290. if (!label)
  1291. goto out;
  1292. nla_label = nla_data(nla);
  1293. bos = true;
  1294. for (i = nla_labels - 1; i >= 0; i--, bos = false) {
  1295. struct mpls_entry_decoded dec;
  1296. dec = mpls_entry_decode(nla_label + i);
  1297. /* Ensure the bottom of stack flag is properly set
  1298. * and ttl and tc are both clear.
  1299. */
  1300. if ((dec.bos != bos) || dec.ttl || dec.tc)
  1301. return -EINVAL;
  1302. switch (dec.label) {
  1303. case MPLS_LABEL_IMPLNULL:
  1304. /* RFC3032: This is a label that an LSR may
  1305. * assign and distribute, but which never
  1306. * actually appears in the encapsulation.
  1307. */
  1308. return -EINVAL;
  1309. }
  1310. label[i] = dec.label;
  1311. }
  1312. out:
  1313. *labels = nla_labels;
  1314. return 0;
  1315. }
  1316. EXPORT_SYMBOL_GPL(nla_get_labels);
  1317. int nla_get_via(const struct nlattr *nla, u8 *via_alen,
  1318. u8 *via_table, u8 via_addr[])
  1319. {
  1320. struct rtvia *via = nla_data(nla);
  1321. int err = -EINVAL;
  1322. int alen;
  1323. if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
  1324. goto errout;
  1325. alen = nla_len(nla) -
  1326. offsetof(struct rtvia, rtvia_addr);
  1327. if (alen > MAX_VIA_ALEN)
  1328. goto errout;
  1329. /* Validate the address family */
  1330. switch (via->rtvia_family) {
  1331. case AF_PACKET:
  1332. *via_table = NEIGH_LINK_TABLE;
  1333. break;
  1334. case AF_INET:
  1335. *via_table = NEIGH_ARP_TABLE;
  1336. if (alen != 4)
  1337. goto errout;
  1338. break;
  1339. case AF_INET6:
  1340. *via_table = NEIGH_ND_TABLE;
  1341. if (alen != 16)
  1342. goto errout;
  1343. break;
  1344. default:
  1345. /* Unsupported address family */
  1346. goto errout;
  1347. }
  1348. memcpy(via_addr, via->rtvia_addr, alen);
  1349. *via_alen = alen;
  1350. err = 0;
  1351. errout:
  1352. return err;
  1353. }
  1354. static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
  1355. struct mpls_route_config *cfg)
  1356. {
  1357. struct rtmsg *rtm;
  1358. struct nlattr *tb[RTA_MAX+1];
  1359. int index;
  1360. int err;
  1361. err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy,
  1362. NULL);
  1363. if (err < 0)
  1364. goto errout;
  1365. err = -EINVAL;
  1366. rtm = nlmsg_data(nlh);
  1367. if (rtm->rtm_family != AF_MPLS)
  1368. goto errout;
  1369. if (rtm->rtm_dst_len != 20)
  1370. goto errout;
  1371. if (rtm->rtm_src_len != 0)
  1372. goto errout;
  1373. if (rtm->rtm_tos != 0)
  1374. goto errout;
  1375. if (rtm->rtm_table != RT_TABLE_MAIN)
  1376. goto errout;
  1377. /* Any value is acceptable for rtm_protocol */
  1378. /* As mpls uses destination specific addresses
  1379. * (or source specific address in the case of multicast)
  1380. * all addresses have universal scope.
  1381. */
  1382. if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
  1383. goto errout;
  1384. if (rtm->rtm_type != RTN_UNICAST)
  1385. goto errout;
  1386. if (rtm->rtm_flags != 0)
  1387. goto errout;
  1388. cfg->rc_label = LABEL_NOT_SPECIFIED;
  1389. cfg->rc_protocol = rtm->rtm_protocol;
  1390. cfg->rc_via_table = MPLS_NEIGH_TABLE_UNSPEC;
  1391. cfg->rc_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  1392. cfg->rc_nlflags = nlh->nlmsg_flags;
  1393. cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
  1394. cfg->rc_nlinfo.nlh = nlh;
  1395. cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
  1396. for (index = 0; index <= RTA_MAX; index++) {
  1397. struct nlattr *nla = tb[index];
  1398. if (!nla)
  1399. continue;
  1400. switch (index) {
  1401. case RTA_OIF:
  1402. cfg->rc_ifindex = nla_get_u32(nla);
  1403. break;
  1404. case RTA_NEWDST:
  1405. if (nla_get_labels(nla, MAX_NEW_LABELS,
  1406. &cfg->rc_output_labels,
  1407. cfg->rc_output_label))
  1408. goto errout;
  1409. break;
  1410. case RTA_DST:
  1411. {
  1412. u8 label_count;
  1413. if (nla_get_labels(nla, 1, &label_count,
  1414. &cfg->rc_label))
  1415. goto errout;
  1416. /* Reserved labels may not be set */
  1417. if (cfg->rc_label < MPLS_LABEL_FIRST_UNRESERVED)
  1418. goto errout;
  1419. break;
  1420. }
  1421. case RTA_VIA:
  1422. {
  1423. if (nla_get_via(nla, &cfg->rc_via_alen,
  1424. &cfg->rc_via_table, cfg->rc_via))
  1425. goto errout;
  1426. break;
  1427. }
  1428. case RTA_MULTIPATH:
  1429. {
  1430. cfg->rc_mp = nla_data(nla);
  1431. cfg->rc_mp_len = nla_len(nla);
  1432. break;
  1433. }
  1434. case RTA_TTL_PROPAGATE:
  1435. {
  1436. u8 ttl_propagate = nla_get_u8(nla);
  1437. if (ttl_propagate > 1)
  1438. goto errout;
  1439. cfg->rc_ttl_propagate = ttl_propagate ?
  1440. MPLS_TTL_PROP_ENABLED :
  1441. MPLS_TTL_PROP_DISABLED;
  1442. break;
  1443. }
  1444. default:
  1445. /* Unsupported attribute */
  1446. goto errout;
  1447. }
  1448. }
  1449. err = 0;
  1450. errout:
  1451. return err;
  1452. }
  1453. static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  1454. struct netlink_ext_ack *extack)
  1455. {
  1456. struct mpls_route_config *cfg;
  1457. int err;
  1458. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  1459. if (!cfg)
  1460. return -ENOMEM;
  1461. err = rtm_to_route_config(skb, nlh, cfg);
  1462. if (err < 0)
  1463. goto out;
  1464. err = mpls_route_del(cfg);
  1465. out:
  1466. kfree(cfg);
  1467. return err;
  1468. }
  1469. static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh,
  1470. struct netlink_ext_ack *extack)
  1471. {
  1472. struct mpls_route_config *cfg;
  1473. int err;
  1474. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  1475. if (!cfg)
  1476. return -ENOMEM;
  1477. err = rtm_to_route_config(skb, nlh, cfg);
  1478. if (err < 0)
  1479. goto out;
  1480. err = mpls_route_add(cfg);
  1481. out:
  1482. kfree(cfg);
  1483. return err;
  1484. }
  1485. static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
  1486. u32 label, struct mpls_route *rt, int flags)
  1487. {
  1488. struct net_device *dev;
  1489. struct nlmsghdr *nlh;
  1490. struct rtmsg *rtm;
  1491. nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
  1492. if (nlh == NULL)
  1493. return -EMSGSIZE;
  1494. rtm = nlmsg_data(nlh);
  1495. rtm->rtm_family = AF_MPLS;
  1496. rtm->rtm_dst_len = 20;
  1497. rtm->rtm_src_len = 0;
  1498. rtm->rtm_tos = 0;
  1499. rtm->rtm_table = RT_TABLE_MAIN;
  1500. rtm->rtm_protocol = rt->rt_protocol;
  1501. rtm->rtm_scope = RT_SCOPE_UNIVERSE;
  1502. rtm->rtm_type = RTN_UNICAST;
  1503. rtm->rtm_flags = 0;
  1504. if (nla_put_labels(skb, RTA_DST, 1, &label))
  1505. goto nla_put_failure;
  1506. if (rt->rt_ttl_propagate != MPLS_TTL_PROP_DEFAULT) {
  1507. bool ttl_propagate =
  1508. rt->rt_ttl_propagate == MPLS_TTL_PROP_ENABLED;
  1509. if (nla_put_u8(skb, RTA_TTL_PROPAGATE,
  1510. ttl_propagate))
  1511. goto nla_put_failure;
  1512. }
  1513. if (rt->rt_nhn == 1) {
  1514. const struct mpls_nh *nh = rt->rt_nh;
  1515. if (nh->nh_labels &&
  1516. nla_put_labels(skb, RTA_NEWDST, nh->nh_labels,
  1517. nh->nh_label))
  1518. goto nla_put_failure;
  1519. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1520. nla_put_via(skb, nh->nh_via_table, mpls_nh_via(rt, nh),
  1521. nh->nh_via_alen))
  1522. goto nla_put_failure;
  1523. dev = rtnl_dereference(nh->nh_dev);
  1524. if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
  1525. goto nla_put_failure;
  1526. if (nh->nh_flags & RTNH_F_LINKDOWN)
  1527. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1528. if (nh->nh_flags & RTNH_F_DEAD)
  1529. rtm->rtm_flags |= RTNH_F_DEAD;
  1530. } else {
  1531. struct rtnexthop *rtnh;
  1532. struct nlattr *mp;
  1533. u8 linkdown = 0;
  1534. u8 dead = 0;
  1535. mp = nla_nest_start(skb, RTA_MULTIPATH);
  1536. if (!mp)
  1537. goto nla_put_failure;
  1538. for_nexthops(rt) {
  1539. dev = rtnl_dereference(nh->nh_dev);
  1540. if (!dev)
  1541. continue;
  1542. rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
  1543. if (!rtnh)
  1544. goto nla_put_failure;
  1545. rtnh->rtnh_ifindex = dev->ifindex;
  1546. if (nh->nh_flags & RTNH_F_LINKDOWN) {
  1547. rtnh->rtnh_flags |= RTNH_F_LINKDOWN;
  1548. linkdown++;
  1549. }
  1550. if (nh->nh_flags & RTNH_F_DEAD) {
  1551. rtnh->rtnh_flags |= RTNH_F_DEAD;
  1552. dead++;
  1553. }
  1554. if (nh->nh_labels && nla_put_labels(skb, RTA_NEWDST,
  1555. nh->nh_labels,
  1556. nh->nh_label))
  1557. goto nla_put_failure;
  1558. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC &&
  1559. nla_put_via(skb, nh->nh_via_table,
  1560. mpls_nh_via(rt, nh),
  1561. nh->nh_via_alen))
  1562. goto nla_put_failure;
  1563. /* length of rtnetlink header + attributes */
  1564. rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *)rtnh;
  1565. } endfor_nexthops(rt);
  1566. if (linkdown == rt->rt_nhn)
  1567. rtm->rtm_flags |= RTNH_F_LINKDOWN;
  1568. if (dead == rt->rt_nhn)
  1569. rtm->rtm_flags |= RTNH_F_DEAD;
  1570. nla_nest_end(skb, mp);
  1571. }
  1572. nlmsg_end(skb, nlh);
  1573. return 0;
  1574. nla_put_failure:
  1575. nlmsg_cancel(skb, nlh);
  1576. return -EMSGSIZE;
  1577. }
  1578. static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
  1579. {
  1580. struct net *net = sock_net(skb->sk);
  1581. struct mpls_route __rcu **platform_label;
  1582. size_t platform_labels;
  1583. unsigned int index;
  1584. ASSERT_RTNL();
  1585. index = cb->args[0];
  1586. if (index < MPLS_LABEL_FIRST_UNRESERVED)
  1587. index = MPLS_LABEL_FIRST_UNRESERVED;
  1588. platform_label = rtnl_dereference(net->mpls.platform_label);
  1589. platform_labels = net->mpls.platform_labels;
  1590. for (; index < platform_labels; index++) {
  1591. struct mpls_route *rt;
  1592. rt = rtnl_dereference(platform_label[index]);
  1593. if (!rt)
  1594. continue;
  1595. if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
  1596. cb->nlh->nlmsg_seq, RTM_NEWROUTE,
  1597. index, rt, NLM_F_MULTI) < 0)
  1598. break;
  1599. }
  1600. cb->args[0] = index;
  1601. return skb->len;
  1602. }
  1603. static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
  1604. {
  1605. size_t payload =
  1606. NLMSG_ALIGN(sizeof(struct rtmsg))
  1607. + nla_total_size(4) /* RTA_DST */
  1608. + nla_total_size(1); /* RTA_TTL_PROPAGATE */
  1609. if (rt->rt_nhn == 1) {
  1610. struct mpls_nh *nh = rt->rt_nh;
  1611. if (nh->nh_dev)
  1612. payload += nla_total_size(4); /* RTA_OIF */
  1613. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC) /* RTA_VIA */
  1614. payload += nla_total_size(2 + nh->nh_via_alen);
  1615. if (nh->nh_labels) /* RTA_NEWDST */
  1616. payload += nla_total_size(nh->nh_labels * 4);
  1617. } else {
  1618. /* each nexthop is packed in an attribute */
  1619. size_t nhsize = 0;
  1620. for_nexthops(rt) {
  1621. if (!rtnl_dereference(nh->nh_dev))
  1622. continue;
  1623. nhsize += nla_total_size(sizeof(struct rtnexthop));
  1624. /* RTA_VIA */
  1625. if (nh->nh_via_table != MPLS_NEIGH_TABLE_UNSPEC)
  1626. nhsize += nla_total_size(2 + nh->nh_via_alen);
  1627. if (nh->nh_labels)
  1628. nhsize += nla_total_size(nh->nh_labels * 4);
  1629. } endfor_nexthops(rt);
  1630. /* nested attribute */
  1631. payload += nla_total_size(nhsize);
  1632. }
  1633. return payload;
  1634. }
  1635. static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
  1636. struct nlmsghdr *nlh, struct net *net, u32 portid,
  1637. unsigned int nlm_flags)
  1638. {
  1639. struct sk_buff *skb;
  1640. u32 seq = nlh ? nlh->nlmsg_seq : 0;
  1641. int err = -ENOBUFS;
  1642. skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
  1643. if (skb == NULL)
  1644. goto errout;
  1645. err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
  1646. if (err < 0) {
  1647. /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
  1648. WARN_ON(err == -EMSGSIZE);
  1649. kfree_skb(skb);
  1650. goto errout;
  1651. }
  1652. rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
  1653. return;
  1654. errout:
  1655. if (err < 0)
  1656. rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
  1657. }
  1658. static int resize_platform_label_table(struct net *net, size_t limit)
  1659. {
  1660. size_t size = sizeof(struct mpls_route *) * limit;
  1661. size_t old_limit;
  1662. size_t cp_size;
  1663. struct mpls_route __rcu **labels = NULL, **old;
  1664. struct mpls_route *rt0 = NULL, *rt2 = NULL;
  1665. unsigned index;
  1666. if (size) {
  1667. labels = kvzalloc(size, GFP_KERNEL);
  1668. if (!labels)
  1669. goto nolabels;
  1670. }
  1671. /* In case the predefined labels need to be populated */
  1672. if (limit > MPLS_LABEL_IPV4NULL) {
  1673. struct net_device *lo = net->loopback_dev;
  1674. rt0 = mpls_rt_alloc(1, lo->addr_len, 0);
  1675. if (IS_ERR(rt0))
  1676. goto nort0;
  1677. RCU_INIT_POINTER(rt0->rt_nh->nh_dev, lo);
  1678. rt0->rt_protocol = RTPROT_KERNEL;
  1679. rt0->rt_payload_type = MPT_IPV4;
  1680. rt0->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  1681. rt0->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1682. rt0->rt_nh->nh_via_alen = lo->addr_len;
  1683. memcpy(__mpls_nh_via(rt0, rt0->rt_nh), lo->dev_addr,
  1684. lo->addr_len);
  1685. }
  1686. if (limit > MPLS_LABEL_IPV6NULL) {
  1687. struct net_device *lo = net->loopback_dev;
  1688. rt2 = mpls_rt_alloc(1, lo->addr_len, 0);
  1689. if (IS_ERR(rt2))
  1690. goto nort2;
  1691. RCU_INIT_POINTER(rt2->rt_nh->nh_dev, lo);
  1692. rt2->rt_protocol = RTPROT_KERNEL;
  1693. rt2->rt_payload_type = MPT_IPV6;
  1694. rt2->rt_ttl_propagate = MPLS_TTL_PROP_DEFAULT;
  1695. rt2->rt_nh->nh_via_table = NEIGH_LINK_TABLE;
  1696. rt2->rt_nh->nh_via_alen = lo->addr_len;
  1697. memcpy(__mpls_nh_via(rt2, rt2->rt_nh), lo->dev_addr,
  1698. lo->addr_len);
  1699. }
  1700. rtnl_lock();
  1701. /* Remember the original table */
  1702. old = rtnl_dereference(net->mpls.platform_label);
  1703. old_limit = net->mpls.platform_labels;
  1704. /* Free any labels beyond the new table */
  1705. for (index = limit; index < old_limit; index++)
  1706. mpls_route_update(net, index, NULL, NULL);
  1707. /* Copy over the old labels */
  1708. cp_size = size;
  1709. if (old_limit < limit)
  1710. cp_size = old_limit * sizeof(struct mpls_route *);
  1711. memcpy(labels, old, cp_size);
  1712. /* If needed set the predefined labels */
  1713. if ((old_limit <= MPLS_LABEL_IPV6NULL) &&
  1714. (limit > MPLS_LABEL_IPV6NULL)) {
  1715. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV6NULL], rt2);
  1716. rt2 = NULL;
  1717. }
  1718. if ((old_limit <= MPLS_LABEL_IPV4NULL) &&
  1719. (limit > MPLS_LABEL_IPV4NULL)) {
  1720. RCU_INIT_POINTER(labels[MPLS_LABEL_IPV4NULL], rt0);
  1721. rt0 = NULL;
  1722. }
  1723. /* Update the global pointers */
  1724. net->mpls.platform_labels = limit;
  1725. rcu_assign_pointer(net->mpls.platform_label, labels);
  1726. rtnl_unlock();
  1727. mpls_rt_free(rt2);
  1728. mpls_rt_free(rt0);
  1729. if (old) {
  1730. synchronize_rcu();
  1731. kvfree(old);
  1732. }
  1733. return 0;
  1734. nort2:
  1735. mpls_rt_free(rt0);
  1736. nort0:
  1737. kvfree(labels);
  1738. nolabels:
  1739. return -ENOMEM;
  1740. }
  1741. static int mpls_platform_labels(struct ctl_table *table, int write,
  1742. void __user *buffer, size_t *lenp, loff_t *ppos)
  1743. {
  1744. struct net *net = table->data;
  1745. int platform_labels = net->mpls.platform_labels;
  1746. int ret;
  1747. struct ctl_table tmp = {
  1748. .procname = table->procname,
  1749. .data = &platform_labels,
  1750. .maxlen = sizeof(int),
  1751. .mode = table->mode,
  1752. .extra1 = &zero,
  1753. .extra2 = &label_limit,
  1754. };
  1755. ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
  1756. if (write && ret == 0)
  1757. ret = resize_platform_label_table(net, platform_labels);
  1758. return ret;
  1759. }
  1760. #define MPLS_NS_SYSCTL_OFFSET(field) \
  1761. (&((struct net *)0)->field)
  1762. static const struct ctl_table mpls_table[] = {
  1763. {
  1764. .procname = "platform_labels",
  1765. .data = NULL,
  1766. .maxlen = sizeof(int),
  1767. .mode = 0644,
  1768. .proc_handler = mpls_platform_labels,
  1769. },
  1770. {
  1771. .procname = "ip_ttl_propagate",
  1772. .data = MPLS_NS_SYSCTL_OFFSET(mpls.ip_ttl_propagate),
  1773. .maxlen = sizeof(int),
  1774. .mode = 0644,
  1775. .proc_handler = proc_dointvec_minmax,
  1776. .extra1 = &zero,
  1777. .extra2 = &one,
  1778. },
  1779. {
  1780. .procname = "default_ttl",
  1781. .data = MPLS_NS_SYSCTL_OFFSET(mpls.default_ttl),
  1782. .maxlen = sizeof(int),
  1783. .mode = 0644,
  1784. .proc_handler = proc_dointvec_minmax,
  1785. .extra1 = &one,
  1786. .extra2 = &ttl_max,
  1787. },
  1788. { }
  1789. };
  1790. static int mpls_net_init(struct net *net)
  1791. {
  1792. struct ctl_table *table;
  1793. int i;
  1794. net->mpls.platform_labels = 0;
  1795. net->mpls.platform_label = NULL;
  1796. net->mpls.ip_ttl_propagate = 1;
  1797. net->mpls.default_ttl = 255;
  1798. table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
  1799. if (table == NULL)
  1800. return -ENOMEM;
  1801. /* Table data contains only offsets relative to the base of
  1802. * the mdev at this point, so make them absolute.
  1803. */
  1804. for (i = 0; i < ARRAY_SIZE(mpls_table) - 1; i++)
  1805. table[i].data = (char *)net + (uintptr_t)table[i].data;
  1806. net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
  1807. if (net->mpls.ctl == NULL) {
  1808. kfree(table);
  1809. return -ENOMEM;
  1810. }
  1811. return 0;
  1812. }
  1813. static void mpls_net_exit(struct net *net)
  1814. {
  1815. struct mpls_route __rcu **platform_label;
  1816. size_t platform_labels;
  1817. struct ctl_table *table;
  1818. unsigned int index;
  1819. table = net->mpls.ctl->ctl_table_arg;
  1820. unregister_net_sysctl_table(net->mpls.ctl);
  1821. kfree(table);
  1822. /* An rcu grace period has passed since there was a device in
  1823. * the network namespace (and thus the last in flight packet)
  1824. * left this network namespace. This is because
  1825. * unregister_netdevice_many and netdev_run_todo has completed
  1826. * for each network device that was in this network namespace.
  1827. *
  1828. * As such no additional rcu synchronization is necessary when
  1829. * freeing the platform_label table.
  1830. */
  1831. rtnl_lock();
  1832. platform_label = rtnl_dereference(net->mpls.platform_label);
  1833. platform_labels = net->mpls.platform_labels;
  1834. for (index = 0; index < platform_labels; index++) {
  1835. struct mpls_route *rt = rtnl_dereference(platform_label[index]);
  1836. RCU_INIT_POINTER(platform_label[index], NULL);
  1837. mpls_notify_route(net, index, rt, NULL, NULL);
  1838. mpls_rt_free(rt);
  1839. }
  1840. rtnl_unlock();
  1841. kvfree(platform_label);
  1842. }
  1843. static struct pernet_operations mpls_net_ops = {
  1844. .init = mpls_net_init,
  1845. .exit = mpls_net_exit,
  1846. };
  1847. static struct rtnl_af_ops mpls_af_ops __read_mostly = {
  1848. .family = AF_MPLS,
  1849. .fill_stats_af = mpls_fill_stats_af,
  1850. .get_stats_af_size = mpls_get_stats_af_size,
  1851. };
  1852. static int __init mpls_init(void)
  1853. {
  1854. int err;
  1855. BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
  1856. err = register_pernet_subsys(&mpls_net_ops);
  1857. if (err)
  1858. goto out;
  1859. err = register_netdevice_notifier(&mpls_dev_notifier);
  1860. if (err)
  1861. goto out_unregister_pernet;
  1862. dev_add_pack(&mpls_packet_type);
  1863. rtnl_af_register(&mpls_af_ops);
  1864. rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
  1865. rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
  1866. rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
  1867. rtnl_register(PF_MPLS, RTM_GETNETCONF, mpls_netconf_get_devconf,
  1868. mpls_netconf_dump_devconf, NULL);
  1869. err = 0;
  1870. out:
  1871. return err;
  1872. out_unregister_pernet:
  1873. unregister_pernet_subsys(&mpls_net_ops);
  1874. goto out;
  1875. }
  1876. module_init(mpls_init);
  1877. static void __exit mpls_exit(void)
  1878. {
  1879. rtnl_unregister_all(PF_MPLS);
  1880. rtnl_af_unregister(&mpls_af_ops);
  1881. dev_remove_pack(&mpls_packet_type);
  1882. unregister_netdevice_notifier(&mpls_dev_notifier);
  1883. unregister_pernet_subsys(&mpls_net_ops);
  1884. }
  1885. module_exit(mpls_exit);
  1886. MODULE_DESCRIPTION("MultiProtocol Label Switching");
  1887. MODULE_LICENSE("GPL v2");
  1888. MODULE_ALIAS_NETPROTO(PF_MPLS);